hunt icon indicating copy to clipboard operation
hunt copied to clipboard

Documentation on Hut logging

Open vinoakash opened this issue 5 years ago • 3 comments

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

vinoakash avatar Nov 10 '20 15:11 vinoakash

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 avatar Nov 11 '20 06:11 Heromyth

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 .

Heromyth avatar Nov 11 '20 07:11 Heromyth

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:[email protected]:3910/testdb";
  this() {
           try {
                 this.db = new Database(constr);
               } catch(DatabaseException e) { writeln(e.msg); }  \\ catch(DatabaseException e) {  logInfo(e.msg);  }
        }
}

vinoakash avatar Nov 12 '20 07:11 vinoakash