[Issue 15536] [std.experimental.logger] More detailed example

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Apr 14 03:09:05 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=15536

--- Comment #2 from Bastiaan Veelo <Bastiaan at 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.");
}
```

--


More information about the Digitalmars-d-bugs mailing list