
 a reference to the
current default Logger. This reference can be used to assign a new
default Logger.
Basic LoggingMessage logging is a common approach to expose runtime information of a program. Logging should be easy, but also flexible and powerful, therefore D 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 the stderr device. is created. Individual Logger 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
sharedLog = new FileLogger("New_Default_Log_File.log");Additional Logger can be created by creating a new instance of the required Logger.
Logging Fundamentals
LogLevelThe LogLevel of a log call can be defined in two ways. The first is by calling log and passing the LogLevel explicitly as the first argument. The second way of setting the LogLevel of a log call, is by calling either trace, info, warning, critical, or fatal. The log call will then have the respective LogLevel. If no LogLevel is defined the log call will use the current LogLevel of the used Logger. If data is logged with LogLevel fatal by default an Error will be thrown. This behaviour can be modified by using the member fatalHandler to assign a custom delegate to handle log call with LogLevel fatal.
Conditional LoggingConditional logging can be achieved be passing a bool as first argument to a log function. If conditional logging is used the condition must be
truein order to have the log message logged. In order to combine an explicit LogLevel passing with conditional logging, the LogLevel has to be passed as first argument followed by the bool.
Filtering Log MessagesMessages are logged if the LogLevel of the log message is greater than or equal to a LogLevel of a Logger use the logLevel property of the
logger.
Printf Style LoggingIfvel and conditional logging functions and methods.
Thread Local RedirectionCalls to the free standing log functions are not directly forwarded to the global Logger sharedLog. Actually, a thread local Logger of type StdForwardLogger to the sharedLog Logger. The LogLevel of the stdThreadLocalLog can be used to filter log calls before they reach the sharedLog Logger.
User Defined LoggerTo customize the Logger behavior, create a new class that inherits from the abstract Logger class, and implements the writeLogMsg method. method the methods beginLogMsg, logMsgPart and finishLogMsg can be overridden.