Thread overview
std.logger issue
Jan 26, 2023
o3o
Jan 26, 2023
Ali Çehreli
Jan 27, 2023
o3o
January 26, 2023
// 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

January 26, 2023

On Thursday, 26 January 2023 at 17:17:28 UTC, o3o wrote:

>

how can I enable trace level?

Set sharedLog.logLevel instead of globalLogLevel.

// 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.

January 26, 2023
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

January 27, 2023

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.

// 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.