Logging best practices

Arun Chandrasekaran aruncxy at gmail.com
Mon Apr 29 16:02:25 UTC 2019


On Sat, Apr 27, 2019 at 2:55 AM Andre Pany via Digitalmars-d-learn
<digitalmars-d-learn at 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


More information about the Digitalmars-d-learn mailing list