std.experimental.logger
Implements logging facilities.
- License:
- Boost License 1.0.
- Authors:
-  Robert burner Schadek  Basic LoggingMessage logging is a common approach to expose runtime information of a program. Logging should be easy, but also flexible and powerful, thereforeDprovides 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 thestderrdevice. 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 newFileLoggeris created. IndividualLoggerand 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
 Loggerwill by default log tostderrand has a defaultLogLevelofLogLevel.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");AdditionalLoggercan be created by creating a new instance of the requiredLogger.Logging FundamentalsLogLevelTheLogLevelof a log call can be defined in two ways. The first is by callinglogand passing theLogLevelexplicitly as the first argument. The second way of setting theLogLevelof a log call, is by calling eithertrace,info,warning,critical, orfatal. The log call will then have the respectiveLogLevel. If noLogLevelis defined the log call will use the currentLogLevelof the usedLogger. If data is logged withLogLevelfatalby default anErrorwill be thrown. This behaviour can be modified by using the memberfatalHandlerto assign a custom delegate to handle log call withLogLevelfatal.Conditional LoggingConditional logging can be achieved be passing aboolas first argument to a log function. If conditional logging is used the condition must betruein order to have the log message logged. In order to combine an explicitLogLevelpassing with conditional logging, theLogLevelhas to be passed as first argument followed by thebool.Filtering Log MessagesMessages are logged if theLogLevelof the log message is greater than or equal to theLogLevelof the usedLoggerand additionally if theLogLevelof 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 globalLogLevelis accessible by usingglobalLogLevel. To assign aLogLevelof aLoggeruse thelogLevelproperty of the logger.Printf Style LoggingIfprintf-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 explicitLogLeveland conditional logging functions and methods.Thread Local RedirectionCalls to the free standing log functions are not directly forwarded to the globalLoggersharedLog. Actually, a thread localLoggerof typeStdForwardLoggerprocesses the log call and then, by default, forwards the createdLogger.LogEntryto thesharedLogLogger. The thread localLoggeris accessible by thestdThreadLocalLogproperty. This property allows to assign user definedLogger. The defaultLogLevelof thestdThreadLocalLogLoggerisLogLevel.alland it will therefore forward all messages to thesharedLogLogger. TheLogLevelof thestdThreadLocalLogcan be used to filter log calls before they reach thesharedLogLogger.User Defined LoggerTo customize theLoggerbehavior, create a newclassthat inherits from the abstractLoggerclass, and implements thewriteLogMsgmethod.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 thewriteLogMsgmethod the methodsbeginLogMsg,logMsgPartandfinishLogMsgcan be overridden.Compile Time Disabling ofIn order to disable logging at compile time, passLoggerStdLoggerDisableLoggingas a version argument to theDcompiler when compiling your program code. This will disable all logging functionality. SpecificLogLevelcan be disabled at compile time as well. In order to disable logging with thetraceLogLevelpassStdLoggerDisableTraceas a version. The following table shows which version statement disables whichLogLevel.
 Such a version statement will only disable logging in the associated compile unit.LogLevel.traceStdLoggerDisableTrace LogLevel.infoStdLoggerDisableInfo LogLevel.warningStdLoggerDisableWarning LogLevel.errorStdLoggerDisableError LogLevel.criticalStdLoggerDisableCritical LogLevel.fatalStdLoggerDisableFatal Provided LoggerBy default fourLoggerimplementations are given. TheFileLoggerlogs data to files. It can also be used to log tostdoutandstderras these devices are files as well. ALoggerthat logs tostdoutcan therefore be created bynew FileLogger(stdout). TheMultiLoggeris basically an associative array ofstrings toLogger. It propagates log calls to its storedLogger. TheArrayLoggercontains an array ofLoggerand also propagates log calls to its storedLogger. TheNullLoggerdoes not do anything. It will never log a message and will never throw on a log call withLogLevelerror.
    © 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
    https://dlang.org/phobos/std_experimental_logger.html