#include <Pt/System/Logger.h>
Writes log records to a target. More...
Inherits NonCopyable.
Public Member Functions | |
Logger (const std::string &name) | |
Constructs a new logger for a target and log-level. More... | |
Logger (const char *name) | |
Constructs a new logger for a target and log-level. More... | |
~Logger () | |
Destructor. | |
bool | enabled (LogLevel level) const |
Returns true if the log level is enabled for the target. | |
bool | enabled (const LogRecord &record) const |
Returns true if the log level is enabled for the target. | |
void | log (const LogRecord &record, bool always=false) |
Write a log record to the target. More... | |
Static Public Member Functions | |
static void | init (const std::string &file) |
Initialize logging targets with a settings file. More... | |
static void | init (const Settings &settings) |
Initialize logging targets with a settings. More... | |
static void | setChannel (const std::string &target, const std::string &url) |
Sets the channel to be used by the target and its children. More... | |
static void | setLogLevel (const std::string &target, LogLevel level) |
Sets the log-level of the target and its children. More... | |
static void | setPattern (const std::string &pattern) |
Set the pattern for log records. More... | |
The Logger is the central class of the logging framework on the client side. It is used to write log records to a logging target maintained by the logging framework. A logger is created with a category string that identifies it's target to log to. The category string is in dot syntax, so the category string "app.module" refers to the target named "module", which is a child of the target named "app", which is a child of the root target. See LogTarget for more information.
If the target of a logger does not exist yet, it will be created. If several loggers are created with the same target string they will indeed use the same target. The creation of a logger requires a target lookup in the logging manager, so it is beneficial to keep created loggers at the class level for as long as they are needed. The logger provides a set of static methods to configure targets. The following code configures a log level and a channel for a target named "app.module":
Channels and log levels can either be assigned by the API or in the settings file that is loaded by calling Pt::System::Logger::init. Here is an example of a settings file:
In the example, pong would write messages with a log level of Trace or higher to the console channel, which it inherited from its parent. The target ping writes messages with a log level of Error of higher to a file.
The main purpose of the logger is to route log records to its target. If the log level of the record is less severe than the current log level of the target, the logger will discard the record. If a target has a log level of Info, the logger will reject records with the levels Trace or Debug. See LogRecord and LogMessage for more information.
Logger | ( | const std::string & | name | ) |
The constructed logger will log to the target with the given name. If the target does not exist yet within the loggin framework it will be created.
Logger | ( | const char * | name | ) |
The constructed logger will log to the target with the given name. If the target does not exist yet within the loggin framework it will be created.
|
static |
The given settings file is parsed and all listed targets are created and initialized. If a target exists already, it is reinitialized.
file | Path to a settings file |
|
static |
All targets listed in the given settings are created and initialized. If a target exists already, it is reinitialized.
settings | Settings to apply to target list |
|
static |
This determines how the logging records are formatted to the channels. The format pattern can contain text and specifiers, which are placeholders for the various elements of the log records. Specifiers are escaped with a percent sign in the format pattern string. For example, the following pattern would write out the time and the message for each log record:
Here is a list of possible specifiers:
|
static |
This method is thread-safe. The log-level can also be set in the settings file of the used for initialization. All children of this target inherit the given LogLevel unless they are already set to a log level explicitly i.e. this method has een called on a child before.
|
static |
Throws a invalid_argument exception if the channel can not be created. This function might block until the channel could be opened. This method is thread-safe. The channel can also be set in the properties file of the logging-manager.
void log | ( | const LogRecord & | record, |
bool | always = false |
||
) |
The log record record will be written to the target of this logger if the records log level allows it, or if always is set to true.