std.experimental.logger
Implements logging facilities.
- License:
- Boost License 1.0.
- Authors:
- Robert burner Schadek
Basic Logging
Message logging is a common approach to expose runtime information of a program. Logging should be easy, but also flexible and powerful, thereforeD
provides a standard interface for logging. The easiest way to create a log message is to write:import std.experimental.logger; void main() { log("Hello World"); }
This will print a message to thestderr
device. The message will contain the filename, the line number, the name of the surrounding function, the time and the message. More complex log call can go along the lines like:log("Logging to the sharedLog with its default LogLevel"); logf(LogLevel.info, 5 < 6, "%s to the sharedLog with its LogLevel.info", "Logging"); info("Logging to the sharedLog with its info LogLevel"); warning(5 < 6, "Logging to the sharedLog with its LogLevel.warning if 5 is less than 6"); error("Logging to the sharedLog with its error LogLevel"); errorf("Logging %s the sharedLog %s its error LogLevel", "to", "with"); critical("Logging to the"," sharedLog with its error LogLevel"); fatal("Logging to the sharedLog with its fatal LogLevel"); auto fLogger = new FileLogger("NameOfTheLogFile"); fLogger.log("Logging to the fileLogger with its default LogLevel"); fLogger.info("Logging to the fileLogger with its default LogLevel"); fLogger.warning(5 < 6, "Logging to the fileLogger with its LogLevel.warning if 5 is less than 6"); fLogger.warningf(5 < 6, "Logging to the fileLogger with its LogLevel.warning if %s is %s than 6", 5, "less"); fLogger.critical("Logging to the fileLogger with its info LogLevel"); fLogger.log(LogLevel.trace, 5 < 6, "Logging to the fileLogger"," with its default LogLevel if 5 is less than 6"); fLogger.fatal("Logging to the fileLogger with its warning LogLevel");
Additionally, this example shows how a newFileLogger
is created. IndividualLogger
and the global log functions share commonly named functions to log data. The names of the functions are as follows:log
trace
info
warning
critical
fatal
Logger
will by default log tostderr
and has a defaultLogLevel
ofLogLevel.all
. The default Logger can be accessed by using the property calledsharedLog
. This property is a reference to the current defaultLogger
. This reference can be used to assign a new defaultLogger
.sharedLog = new FileLogger("New_Default_Log_File.log");
AdditionalLogger
can be created by creating a new instance of the requiredLogger
.Logging Fundamentals
LogLevel
TheLogLevel
of a log call can be defined in two ways. The first is by callinglog
and passing theLogLevel
explicitly as the first argument. The second way of setting theLogLevel
of a log call, is by calling eithertrace
,info
,warning
,critical
, orfatal
. The log call will then have the respectiveLogLevel
. If noLogLevel
is defined the log call will use the currentLogLevel
of the usedLogger
. If data is logged withLogLevel
fatal
by default anError
will be thrown. This behaviour can be modified by using the memberfatalHandler
to assign a custom delegate to handle log call withLogLevel
fatal
.Conditional Logging
Conditional logging can be achieved be passing abool
as first argument to a log function. If conditional logging is used the condition must betrue
in order to have the log message logged. In order to combine an explicitLogLevel
passing with conditional logging, theLogLevel
has to be passed as first argument followed by thebool
.Filtering Log Messages
Messages are logged if theLogLevel
of the log message is greater than or equal to theLogLevel
of the usedLogger
and additionally if theLogLevel
of the log message is greater than or equal to the globalLogLevel
. If a condition is passed into the log call, this condition must be true. The globalLogLevel
is accessible by usingglobalLogLevel
. To assign aLogLevel
of aLogger
use thelogLevel
property of the logger.Printf Style Logging
Ifprintf
-style logging is needed add a f to the logging call, such asmyLogger.infof("Hello %s", "world");
orfatalf("errno %d", 1337)
. The additional f appended to the function name enablesprintf
-style logging for all combinations of explicitLogLevel
and conditional logging functions and methods.Thread Local Redirection
Calls to the free standing log functions are not directly forwarded to the globalLogger
sharedLog
. Actually, a thread localLogger
of typeStdForwardLogger
processes the log call and then, by default, forwards the createdLogger.LogEntry
to thesharedLog
Logger
. The thread localLogger
is accessible by thestdThreadLocalLog
property. This property allows to assign user definedLogger
. The defaultLogLevel
of thestdThreadLocalLog
Logger
isLogLevel.all
and it will therefore forward all messages to thesharedLog
Logger
. TheLogLevel
of thestdThreadLocalLog
can be used to filter log calls before they reach thesharedLog
Logger
.User Defined Logger
To customize theLogger
behavior, create a newclass
that inherits from the abstractLogger
class
, and implements thewriteLogMsg
method.class MyCustomLogger : Logger { this(LogLevel lv) @safe { super(lv); } override void writeLogMsg(ref LogEntry payload) { // log message in my custom way } } auto logger = new MyCustomLogger(LogLevel.info); logger.log("Awesome log message with LogLevel.info");
To gain more precise control over the logging process, additionally to overriding thewriteLogMsg
method the methodsbeginLogMsg
,logMsgPart
andfinishLogMsg
can be overridden.Compile Time Disabling of
In order to disable logging at compile time, passLogger
StdLoggerDisableLogging
as a version argument to theD
compiler when compiling your program code. This will disable all logging functionality. SpecificLogLevel
can be disabled at compile time as well. In order to disable logging with thetrace
LogLevel
passStdLoggerDisableTrace
as a version. The following table shows which version statement disables whichLogLevel
.LogLevel.trace
StdLoggerDisableTrace LogLevel.info
StdLoggerDisableInfo LogLevel.warning
StdLoggerDisableWarning LogLevel.error
StdLoggerDisableError LogLevel.critical
StdLoggerDisableCritical LogLevel.fatal
StdLoggerDisableFatal Provided Logger
By default fourLogger
implementations are given. TheFileLogger
logs data to files. It can also be used to log tostdout
andstderr
as these devices are files as well. ALogger
that logs tostdout
can therefore be created bynew FileLogger(stdout)
. TheMultiLogger
is basically an associative array ofstring
s toLogger
. It propagates log calls to its storedLogger
. TheArrayLogger
contains an array ofLogger
and also propagates log calls to its storedLogger
. TheNullLogger
does not do anything. It will never log a message and will never throw on a log call withLogLevel
error
.
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_experimental_logger.html