www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Logging best practices

reply Vladimirs Nordholm <v vladde.net> writes:
Hello.

Is there a current "Best Practices" for logging in D?

For the actual logging, I know of `std.experimental.logger`. 
However, the `experimental` has kept me away from it.

Is it good, or are there any better alternatives?
Apr 25 2019
next sibling parent reply dangbinghoo <dangbinghoo gmail.com> writes:
On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
for the latest alpha version of D release, all std.experimental has been already moved to std. so, just don't be afraid! ~_^ ----- binghoo dang
Apr 25 2019
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Thursday, 25 April 2019 at 15:51:43 UTC, dangbinghoo wrote:
 On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
 wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
for the latest alpha version of D release, all std.experimental has been already moved to std.
Are you sure about that? https://github.com/dlang/phobos/tree/master/std I think you are confusing the package std.experimental.all that moved to std. It means you can now import all of Phobos by doing `import std;` instead of `import std.experimental.all;`. It does not mean that everything below std.experimental moved to std and thereby lost its experimental status. Bastiaan.
Apr 26 2019
parent dangbinghoo <dangbinghoo gmail.com> writes:
On Friday, 26 April 2019 at 08:10:33 UTC, Bastiaan Veelo wrote:
 std.experimental has been already moved to std.
Are you sure about that? https://github.com/dlang/phobos/tree/master/std I think you are confusing the package std.experimental.all that moved to std. It means you can now import all of Phobos by doing `import std;` instead of `import std.experimental.all;`. It does not mean that everything below std.experimental moved to std and thereby lost its experimental status. Bastiaan.
yes, you're right, I just made a misunderstanding. Thanks!
Apr 26 2019
prev sibling next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
Apart from an open issue I have, https://issues.dlang.org/show_bug.cgi?id=15536, I have used std.experimental.logger successfully here: https://github.com/PhilippeSigaud/Pegged/wiki/Grammar-Debugging Bastiaan.
Apr 26 2019
prev sibling next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
I also use it for all of my applications. But I really miss a RotatedTimeFileLogger. One big log file which will grow and grow is not that usuable in cloud environment. Also I haven't found out wheter it is safe / how to write from different threads to the same log file. Kind regards Andre
Apr 27 2019
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
On Sat, Apr 27, 2019 at 2:55 AM Andre Pany via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:
 On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm
 wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`.
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
I also use it for all of my applications. But I really miss a RotatedTimeFileLogger. One big log file which will grow and grow is not that usuable in cloud environment.
std.experimenetal.logger is very limited with the feature set it offers. You can't set custom log patterns, etc. If you use AWS (docker), you could log to stdout and let the cloudwatch do all the magic. Based on a config parameter, you can enable/disable logging to a file. module my.logger; import std.experimental.logger; __gshared MultiLogger log; private __gshared FileLogger fileLog; private __gshared FileLogger onScreen; shared static this() { import std.file; import std.stdio : stdout, File; auto file = File("LMS.log", "a"); fileLog = new FileLogger(file); onScreen = new FileLogger(stdout); log = new MultiLogger(); if (cfg.logToFile) log.insertLogger("file", fileLog); log.insertLogger("stdout", onScreen); }
 Also I haven't found out wheter it is safe / how to write from
 different threads to the same log file.
std.experimental.logger is perfectly thread safe. However printing the logging thread ID is still pending with this PR https://github.com/dlang/phobos/pull/6978
Apr 29 2019
parent mw <mingwu gmail.com> writes:
On Monday, 29 April 2019 at 16:02:25 UTC, Arun Chandrasekaran 
wrote:
 std.experimental.logger is perfectly thread safe. However 
 printing the logging thread ID is still pending with this PR 
 https://github.com/dlang/phobos/pull/6978
Also is any file logger thread safe?
May 30 2020
prev sibling parent reply mw <mingwu gmail.com> writes:
On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
A related question: how to log to multiple destinations? e.g. both console & log file? any examples? Thanks.
May 30 2020
next sibling parent Andre Pany <andre s-e-a-p.de> writes:
On Saturday, 30 May 2020 at 18:17:21 UTC, mw wrote:
 On Thursday, 25 April 2019 at 10:33:00 UTC, Vladimirs Nordholm 
 wrote:
 Hello.

 Is there a current "Best Practices" for logging in D?

 For the actual logging, I know of `std.experimental.logger`. 
 However, the `experimental` has kept me away from it.

 Is it good, or are there any better alternatives?
A related question: how to log to multiple destinations? e.g. both console & log file? any examples? Thanks.
Please have a look at the unittest in line 108 here https://www.github.com/dlang/phobos/tree/384d0427936fc8846b633ac08e212e5107c37d16/std%2Fexperimental%2Flogger%2Fmultilogger.d Kind regards Andre
May 30 2020
prev sibling parent reply Luis <luis.panadero gmail.com> writes:
On Saturday, 30 May 2020 at 18:17:21 UTC, mw wrote:
 A related question: how to log to multiple destinations? e.g. 
 both console & log file? any examples?

 Thanks.
```D auto multiLogger = new MultiLogger(); multiLogger.insertLogger("console", new FileLogger(stdout, LogLevel.all)); multiLogger.insertLogger("file", new FileLogger(outputFile, LogLevel.all)); sharedLog = multiLogger; ``` If you are missing color output for log level , you can use this ConsoleLogger ```D import std.experimental.logger; /// Extends FileLogger to log only to stdout and colorize log level class ConsoleLogger : FileLogger { import std.concurrency : Tid; import std.datetime.systime : SysTime; import std.format : formattedWrite; this(const LogLevel lv = LogLevel.all) { import std.stdio : stdout; super(stdout, lv); } /* This method overrides the base class method in order to log to a file without requiring heap allocated memory. Additionally, the `FileLogger` local mutex is logged to serialize the log calls. */ override protected void beginLogMsg(string file, int line, string funcName, string prettyFuncName, string moduleName, LogLevel logLevel, Tid threadId, SysTime timestamp, Logger logger) safe { import std.string : lastIndexOf; ptrdiff_t fnIdx = file.lastIndexOf('/') + 1; ptrdiff_t funIdx = funcName.lastIndexOf('.') + 1; auto lt = this.file_.lockingTextWriter(); systimeToISOString(lt, timestamp); colorizeLogLevel(lt, logLevel); formattedWrite(lt, " %s:%u:%s ", file[fnIdx .. $], line, funcName[funIdx .. $]); } private void colorizeLogLevel(Writer)(auto ref Writer lt, LogLevel logLevel) safe { import std.conv : to; auto logLevelStr = logLevel.to!string; switch (logLevel) { case LogLevel.all: formattedWrite(lt, " [\033[1;37;40m%s\033[0m] ", logLevelStr); break; case LogLevel.trace: formattedWrite(lt, " [\033[1;37;40m%s\033[0m] ", logLevelStr); break; case LogLevel.info: formattedWrite(lt, " [\033[1;32;40m%s\033[0m] ", logLevelStr); break; case LogLevel.warning: formattedWrite(lt, " [\033[1;33;40m%s\033[0m] ", logLevelStr); break; case LogLevel.error: formattedWrite(lt, " [\033[1;31;40m%s\033[0m] ", logLevelStr); break; case LogLevel.critical: formattedWrite(lt, " [\033[1;37;41m%s\033[0m]", logLevelStr); break; case LogLevel.fatal: formattedWrite(lt, " [\033[1;25;37;41m%s\033[0m] ", logLevelStr); break; default: formattedWrite(lt, " [\033[1;37;40m%s\033[0m] ", logLevelStr); } } } ```
May 31 2020
parent reply mw <mingwu gmail.com> writes:
On Sunday, 31 May 2020 at 07:49:01 UTC, Luis wrote:
 On Saturday, 30 May 2020 at 18:17:21 UTC, mw wrote:
 A related question: how to log to multiple destinations? e.g. 
 both console & log file? any examples?

 Thanks.
```D auto multiLogger = new MultiLogger(); multiLogger.insertLogger("console", new FileLogger(stdout, LogLevel.all)); multiLogger.insertLogger("file", new FileLogger(outputFile, LogLevel.all)); sharedLog = multiLogger; ``` If you are missing color output for log level , you can use this ConsoleLogger
Thanks. For the colored console logger, I'm using this one: https://code.dlang.org/packages/colored-logger so far so good. (I prefer to use library, instead of reinvent my own wheels :-).
May 31 2020
parent Luis <luis.panadero gmail.com> writes:
On Sunday, 31 May 2020 at 20:04:11 UTC, mw wrote:
 Thanks.

 For the colored console logger, I'm using this one:

 https://code.dlang.org/packages/colored-logger

 so far so good. (I prefer to use library, instead of reinvent 
 my own wheels :-).
I dind't know that exists. 👍
May 31 2020