huntlabs / hunt

A refined core library for D programming language. The module has concurrency / collections / event / io / logging / text / serialization and more.
Apache License 2.0
95 stars 15 forks source link

Documentation on Hut logging #85

Open vinoakash opened 4 years ago

vinoakash commented 4 years ago

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

hunt.log.level=all
hunt.log.path="/dhunt/LDC/temp/"
hunt.log.file="hunt.log"       **\\ how to set the file name with timestamp eg: hunt_YYYYMMDD.log**
hunt.log.maxSize = 8M
hunt.log.maxNum=10

File: settings.d

module common.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 conf;
}

  ConfigBuilder manager;
  manager = new ConfigBuilder(convertConfigPathToRelative("source/common/logging.conf"));
  return (!manager.hunt.log.path.value().empty);

}

Now we need your help on how to use the logging functionality using the configuration in logging.conf file.

import std.stdio;
import hunt.logging.Logger;
import hunt.util.DateTime;
import common.settings;

void main() {
string log = validateLogConfiguration();
if(log != "true") { writeln("Log Path does not exist"); }
else {
         DateTime.startClock();
         logInfo("info");    **\\ how to load the setting that are defined in logging.conf file**

        function1();
        function2();
}
}
Heromyth commented 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);
    }
}
Heromyth commented 4 years ago

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.

vinoakash commented 4 years ago

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);  }
        }
}