Thread overview
[Issue 15536] [std.experimental.logger] More detailed example for custom logger implementation
[Issue 15536] [std.experimental.logger] More detailed example
Dec 23, 2016
Lucia Cojocaru
Apr 14, 2017
Bastiaan Veelo
Jul 18, 2022
Atila Neves
Jul 18, 2022
Bastiaan Veelo
December 23, 2016
https://issues.dlang.org/show_bug.cgi?id=15536

Lucia Cojocaru <lucia.mcojocaru@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |trivial
                 CC|                            |lucia.mcojocaru@gmail.com

--
March 31, 2017
https://issues.dlang.org/show_bug.cgi?id=15536

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|trivial                     |
                 CC|                            |rburners@gmail.com,
                   |                            |schveiguy@yahoo.com

--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> ---
This bug needs more information. Please include a program (or link to one) which exhibits the problem.

--
April 14, 2017
https://issues.dlang.org/show_bug.cgi?id=15536

--- Comment #2 from Bastiaan Veelo <Bastiaan@Veelo.net> ---
It has been a while, and I cannot reproduce the link- and runtime errors. However, things still don't work as expected.

>From what I read from https://dlang.org/phobos/std_experimental_logger.html,
"User Defined Logger", I should be able to remove the time stamp etc. by doing this:

```
import std.experimental.logger;

/**
Overrides FileLogger to remove the time stamp.
*/
class AnonimousFileLogger : FileLogger
{
    this(in string fn) @safe
    {
        super(fn);
    }

   /* This method SHOULD override the base class method.
    */
    override protected void writeLogMsg(ref LogEntry payload)
    {
        //this.beginLogMsg(payload.file, payload.line, payload.funcName,
        //    payload.prettyFuncName, payload.moduleName, payload.logLevel,
        //    payload.threadId, payload.timestamp, payload.logger);
        this.logMsgPart(payload.msg);
        this.finishLogMsg();
    }

}

void main()
{
    auto logger = new AnonimousFileLogger("log.txt");
    logger.trace("I'm here.");
}
```

I'd expect `log.txt` to just contain "I'm here.", instead it contains "2017-04-14T11:54:00.405:app.d:main:38 I'm here.".

Surely there is an explanation for this, but it is not obvious to me and I'd like an example in the docs for how to do this properly.

I've managed to get what I want with the following, overriding `beginLogMsg` with an empty implementation, but it feels wrong and there should be a better way:

```
import std.experimental.logger;

/**
Overrides FileLogger to remove the time stamp.
*/
class AnonimousFileLogger : FileLogger
{
    this(in string fn) @safe
    {
        super(fn);
    }
    import std.concurrency : Tid;
    import std.datetime : SysTime;
    override protected void beginLogMsg(string file, int line, string funcName,
        string prettyFuncName, string moduleName, LogLevel logLevel,
        Tid threadId, SysTime timestamp, Logger logger)
        @safe
    {
    }
}

void main()
{
    auto logger = new AnonimousFileLogger("log.txt");
    logger.trace("I'm here.");
}
```

--
December 18, 2018
https://issues.dlang.org/show_bug.cgi?id=15536

Arun Chandrasekaran <aruncxy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aruncxy@gmail.com
            Summary|[std.experimental.logger]   |[std.experimental.logger]
                   |More detailed example       |More detailed example for
                   |                            |custom logger
                   |                            |implementation

--
April 26, 2019
https://issues.dlang.org/show_bug.cgi?id=15536

--- Comment #3 from Arun Chandrasekaran <aruncxy@gmail.com> ---
I'm not against custom logger implementation, however, I think this can be achieved with a function that accepts a log pattern, globally and per logger.

Something like this:

%t = thread-id
%T = timestamp
%l = log level
%m = log message

logger.setPattern("%t %T %l %m") would result in log message as follows.

2019-04-26 10:51:22.876 083892 info Some stuff.

This will make it very flexible.

--
July 18, 2022
https://issues.dlang.org/show_bug.cgi?id=15536

Atila Neves <atila.neves@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |atila.neves@gmail.com

--- Comment #4 from Atila Neves <atila.neves@gmail.com> ---
This worked as expected for me and only logged a string:

------------------------------
import std.experimental.logger.filelogger;
import std.experimental.logger.core;

class MyLogger : FileLogger {
    this(in string fn) @safe { super(fn); }
    override protected void writeLogMsg(ref LogEntry payload) {
        logMsgPart(payload.msg);
    }
}

void main() {
    sharedLog = new MyLogger("/tmp/logger.txt");
    log("foobar");
}
------------------------------

--
July 18, 2022
https://issues.dlang.org/show_bug.cgi?id=15536

Bastiaan Veelo <Bastiaan@Veelo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Bastiaan Veelo <Bastiaan@Veelo.net> ---
Then we can conclude that the issue is fixed. Thanks.

--