Logger Class Reference

#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...
 

Detailed Description

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:

foobar.channel = console://
foobar.pong.logLevel = Trace
foobar.ping.channel = file:
foobar.ping.logLevel = Error

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.

Constructor & Destructor Documentation

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.

Member Function Documentation

static void init ( const std::string &  file)
static

The given settings file is parsed and all listed targets are created and initialized. If a target exists already, it is reinitialized.

Parameters
filePath to a settings file
static void init ( const Settings settings)
static

All targets listed in the given settings are created and initialized. If a target exists already, it is reinitialized.

Parameters
settingsSettings to apply to target list
static void setPattern ( const std::string &  pattern)
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:

olib::log::Logger::setPattern("%t - %m");

Here is a list of possible specifiers:

  • c logging category
  • d current date
  • l log level (severity)
  • m message text
  • t current time
  • F file where the record was logged
  • L line number where the record was logged
  • M method/function where the record was logged
static void setLogLevel ( const std::string &  target,
LogLevel  level 
)
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 void setChannel ( const std::string &  target,
const std::string &  url 
)
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.