Open vinoakash opened 4 years ago
Not sure whether this is what you want:
@Configuration("hunt")
class AppConfig {
struct LogConfig {
string level = "all";
string path;
string file = "";
bool disableConsole = true;
string maxSize = "8M";
uint maxNum = 8;
}
LogConfig log;
}
AppConfig validateLogConfiguration() {
ConfigBuilder manager = new ConfigBuilder(convertConfigPathToRelative("logging.conf"));
return manager.build!(AppConfig)();
}
void main() {
AppConfig config = validateLogConfiguration();
if (config.log.path.empty) {
warning("Log Path does not exist");
} else {
logInfo(config.log.path);
}
}
As for how to set the name of the log file with a time field, the hunt.logging.Logger
can't do that automaticly.
See here, the g_logger
is a global variable and is initialized with LogConf
.
So when you want to log message to another file, you have to call logLoadConf
with a different LogConf
.
Hi Heromyth,
Below is a example we need the exception (catch(DatabaseException e) { writeln(e.msg); }) to be written to a log file using the settings defined in the logging.conf file.
Code : settings.d
module settings;
import std.stdio: writeln;
import std.file;
import std.path: relativePath, buildPath;
import std.array: join, empty;
import hunt.util.Configuration;
import hunt.logging.Logger;
string convertConfigPathToRelative(string configName) {
mixin("string confPrefix = \"@CONF_PREFIX@\";");
if (confPrefix == join(["@CONF", "_PREFIX@"])) {
return configName;
} else {
auto relConfPath = relativePath(confPrefix, std.file.getcwd);
return buildPath(relConfPath, configName);
}
}
auto validateLogConfiguration () {
@Configuration("hunt")
class AppConfig
{
struct LogConfig
{
string level = "all";
string path;
string file = "";
bool disableConsole = true;
string maxSize = "8M";
uint maxNum = 8;
}
LogConfig log;
}
ConfigBuilder manager;
manager = new ConfigBuilder(convertConfigPathToRelative("source/common/logging.conf"));
if(manager.hunt.log.path.empty) { warning("Log Path does not exist"); }
else { return logInfo(manager.hunt.log.path); }
}
Code: connection.d
import hunt.database;
import std.stdio: writeln;
import hunt.logging.Logger;
import settings;
@trusted class GetConnections
{
public Database db;
immutable constr = "mysql://server:password@127.0.0.1:3910/testdb";
this() {
try {
this.db = new Database(constr);
} catch(DatabaseException e) { writeln(e.msg); } \\ catch(DatabaseException e) { logInfo(e.msg); }
}
}
Hi All,
Request your help on hunt logging. We have tested the example provided on how to validate the configuration, and now we request your help on how to implement the same, eg:
File: logging.conf
File: settings.d
Now we need your help on how to use the logging functionality using the configuration in logging.conf file.