www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.logger issue

reply o3o <dlang orfeo.fastmail.com> writes:
```
// rdmd --main -unitest app.d
import std.stdio;
import std.logger;
unittest {
    globalLogLevel = LogLevel.all;
    infof("g: %s", globalLogLevel);
    trace("trace"); // NO output!
    info("info");
    warning("warn");
    error("error");
}
```

output is:
```
2023-01-26T18:16:13.546 [info] log.d:6:main g: all
2023-01-26T18:16:13.546 [info] log.d:8:main info
2023-01-26T18:16:13.546 [warning] log.d:9:main warn
2023-01-26T18:16:13.546 [error] log.d:10:main error
```
how can I enable `trace` level?
Thank
Jan 26 2023
parent reply Krzysztof =?UTF-8?B?SmFqZcWbbmljYQ==?= <krzysztof.jajesnica gmail.com> writes:
On Thursday, 26 January 2023 at 17:17:28 UTC, o3o wrote:
 how can I enable `trace` level?
Set `sharedLog.logLevel` instead of `globalLogLevel`. ```d // Note: the cast is needed because sharedLog is shared (cast()sharedLog).logLevel = LogLevel.all; ``` Explanation: logging functions (`trace`, `log`, etc.) called without a logger perform the logging using a global logger called `sharedLog`. `sharedLog` uses `LogLevel.info` by default, which is why your trace messages were not showing.
Jan 26 2023
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/26/23 12:08, Krzysztof Jajeśnica wrote:
 On Thursday, 26 January 2023 at 17:17:28 UTC, o3o wrote:
 how can I enable `trace` level?
Set `sharedLog.logLevel` instead of `globalLogLevel`.
Good catch. I had tried the following without success: stdThreadLocalLog.logLevel = LogLevel.all;
 // Note: the cast is needed because sharedLog is shared
 (cast()sharedLog).logLevel = LogLevel.all;
I did not think casting that way would be the right thing to do. Although I've never used std.logger, and without remembering who implemented it (sincere aplogies), given how simple the use cases of logging are, I found its implementation very complicated. For example, the following function is one I stumbled upon while debugging the OP's issue: bool isLoggingEnabled()(LogLevel ll, LogLevel loggerLL, LogLevel globalLL, lazy bool condition = true) safe { return ll >= globalLL && ll >= loggerLL && ll != LogLevel.off && globalLL != LogLevel.off && loggerLL != LogLevel.off && condition; } I don't think it is possible to entagle usage issues with functions with that semantic complexity. Ali
Jan 26 2023
prev sibling parent o3o <dlang orfeo.fastmail.com> writes:
On Thursday, 26 January 2023 at 20:08:51 UTC, Krzysztof Jajeśnica 
wrote:
 On Thursday, 26 January 2023 at 17:17:28 UTC, o3o wrote:
 how can I enable `trace` level?
Set `sharedLog.logLevel` instead of `globalLogLevel`. ```d // Note: the cast is needed because sharedLog is shared (cast()sharedLog).logLevel = LogLevel.all; ``` Explanation: logging functions (`trace`, `log`, etc.) called without a logger perform the logging using a global logger called `sharedLog`. `sharedLog` uses `LogLevel.info` by default, which is why your trace messages were not showing.
Thank you very much Krzysztof, it works.
Jan 26 2023