Bug 15536 – [std.experimental.logger] More detailed example for custom logger implementation

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-01-09T22:57:50Z
Last change time
2022-07-18T14:50:32Z
Assigned to
No Owner
Creator
Bastiaan Veelo

Comments

Comment #0 by Bastiaan — 2016-01-09T22:57:50Z
Please give a more detailed working example of a custom logger with a different header, e.g., omitting the time stamp, file name and line number. I keep getting link errors or segmentation faults trying this...
Comment #1 by schveiguy — 2017-03-31T23:40:59Z
This bug needs more information. Please include a program (or link to one) which exhibits the problem.
Comment #2 by Bastiaan — 2017-04-14T10:09:05Z
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."); } ```
Comment #3 by aruncxy — 2019-04-26T19:25:01Z
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.
Comment #4 by atila.neves — 2022-07-18T14:23:24Z
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"); } ------------------------------
Comment #5 by Bastiaan — 2022-07-18T14:50:32Z
Then we can conclude that the issue is fixed. Thanks.