Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation on Hut logging #85

Open
vinoakash opened this issue Nov 10, 2020 · 3 comments
Open

Documentation on Hut logging #85

vinoakash opened this issue Nov 10, 2020 · 3 comments

Comments

@vinoakash
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants