Example code in std.logger.core doesn't even work
Danilo
codedan at aol.com
Thu Jul 13 05:13:52 UTC 2023
Nice explanation. Thanks, Jonathan M Davis!
After reading https://dlang.org/phobos/std_logger.html
very exactly, we understand that `sharedLog` is the default
internal logger. `sharedLog` can be used to change the default
logger:
```d
//auto file = File("logFile.log", "w");
auto file = stderr; // stdout
sharedLog = cast(shared)new FileLogger(file);
```
After that, `sharedLog` is not used directly (maybe the docs
could make this more clear).
Instead, we use:
- log / logf
- trace
- info
- warning / warningf
- critical
- error / errorf
- fatal
Those functions use `sharedLog` internally.
If you look at the implementations, the provided default `Logger`
and `FileLogger` are thread-safe already.
They use mutexes to manage access to the global logger.
So the correct way is to use the global functions or create
local loggers using `auto logger = new WhateverLogger():
logger.info("msg");`
A complete example using threads was already provided at:
-
https://forum.dlang.org/post/lhewpbfniwozsqdvsudc@forum.dlang.org
More information about the Digitalmars-d
mailing list