LiMaL library logger class. More...
#include <Logger.hpp>
Public Types | |
typedef BLOCXX_NAMESPACE::ELogLevel | ELogLevel |
Log level constants. | |
Public Member Functions | |
Logger (const BLOCXX_NAMESPACE::String &component="") | |
~Logger () | |
void | logMessage (ELogLevel level, const BLOCXX_NAMESPACE::String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const |
void | logMessage (const BLOCXX_NAMESPACE::String &category, const BLOCXX_NAMESPACE::String &message, const char *filename=0, int fileline=-1, const char *methodname=0) const |
bool | isEnabledFor (const ELogLevel level) const |
bool | isEnabledFor (const BLOCXX_NAMESPACE::String &category) const |
Static Public Member Functions | |
static BLOCXX_NAMESPACE::LoggerRef | createCerrLogger (const BLOCXX_NAMESPACE::String &component, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &components, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &categories, const BLOCXX_NAMESPACE::String &messageFormat) |
Create a new logger reference to a CerrLogger object. | |
static BLOCXX_NAMESPACE::LoggerRef | createSyslogLogger (const BLOCXX_NAMESPACE::String &component, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &components, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &categories, const BLOCXX_NAMESPACE::String &messageFormat, const BLOCXX_NAMESPACE::String &identity, const BLOCXX_NAMESPACE::String &facility) |
Create a new logger reference to a SyslogLogger object. | |
static BLOCXX_NAMESPACE::LoggerRef | createFileLogger (const BLOCXX_NAMESPACE::String &component, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &components, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &categories, const BLOCXX_NAMESPACE::String &messageFormat, const BLOCXX_NAMESPACE::String &filename, BLOCXX_NAMESPACE::UInt64 maxLogFileSize=0, BLOCXX_NAMESPACE::UInt32 maxBackupIndex=0) |
Create a new logger reference to a FileLogger object. | |
static BLOCXX_NAMESPACE::LoggerRef | createNullLogger (const BLOCXX_NAMESPACE::String &component, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &components, const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > &categories, const BLOCXX_NAMESPACE::String &messageFormat) |
Create a new logger reference to a NullLogger object. | |
static bool | setDefaultLogger (const BLOCXX_NAMESPACE::LoggerRef &ref) |
static bool | setThreadLogger (const BLOCXX_NAMESPACE::LoggerRef &ref) |
static BLOCXX_NAMESPACE::LoggerRef | getDefaultLogger () |
static BLOCXX_NAMESPACE::LoggerRef | getCurrentLogger () |
Static Private Member Functions | |
static bool | setDefaultFromLoggerRef (const BLOCXX_NAMESPACE::LoggerRef &ref) |
static bool | setThreadFromLoggerRef (const BLOCXX_NAMESPACE::LoggerRef &ref) |
static BLOCXX_NAMESPACE::LoggerRef | getDefaultAsLoggerRef () |
static BLOCXX_NAMESPACE::LoggerRef | getCurrentAsLoggerRef () |
Private Attributes | |
BLOCXX_NAMESPACE::String | m_component |
LiMaL library logger class.
The LiMaL logger class provides several static functions allowing to create standard logger types (file, cerr, syslog), to set them as the global/default or per thread logger in the blocxx library as well as to retrieve them.
Each instance of the LiMaL logger contains a optional local component name, that is passed with every log message to the current logger and used instead of the default component name, that is set in the logger.
This allows to use different component names at different places of the application and/or library, e.g. each plugin logs messages using own component name.
The LiMaL logger instance can be used directly or with the LIMAL_LOG(), LIMAL_SLOG() and derived macros.
BloCxx supports configuration of the log message format using printf() style conversion specifiers.
Available conversion specifiers:
%c
The component (e.g. myapp) %d
The date. May be followed by a date format specifier enclosed between braces. Examples: "%d{%H:%M:%S}" "%d{%d %b %Y %H:%M:%S}"
%F
The file name %l
The filename and line number. e.g. file.cpp(100) %L
The line number %M
The method name where the logging request was issued (only works on C++ compilers which support __PRETTY_FUNCTION__
or C99's __func__
) %m
The message %e
The message as XML CDATA. This includes the "<![CDATA["
and ending "]]>"
%n
The platform dependent line separator character (\n) or characters (\r\n) %P
The process id %p
Category, aka level, aka priority %r
The number of milliseconds elapsed since the start of the application until the creation of the logging event %t
The thread id %%%%
The % character. \n
The newline character \t
The tab character \r
The linefeed character \\
The \ character \x<hexDigits>
The character represented in hexadecimal.It is possible to change the minimum field width, the maximum field width and justification. The optional format modifier is placed between the percent sign and the conversion character.
The first optional format modifier is the left justification flag which is the minus (-) character. The optional minimum field width modifier follows. It is an integer that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded with spaces on either the left or the right, according to the justification flag. If the data item is larger than the minimum field width, the field is expanded to accommodate the data.
The maximum field width modifier is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item (by default), or from the end if the left justification flag was specified.
Examples:
Log4j TTCC layout:
"%r [%t] %-5p %c - %m"
Similar to TTCC, but with some fixed size fields:
"%-6r [%15.15t] %-5p %30.30c - %m"
The TTCC message format is defined in the blocxx::LogAppender::STR_TTCC_MESSAGE_FORMAT
constant.
typedef BLOCXX_NAMESPACE ::ELogLevel limal::Logger::ELogLevel |
Log level constants.
Possible category names and their corresponding log levels are:
"FATAL" blocxx::E_FATAL_ERROR_LEVEL
"ERROR" blocxx::E_ERROR_LEVEL
"INFO" blocxx::E_INFO_LEVEL
"DEBUG" blocxx::E_DEBUG_LEVEL
limal::Logger::Logger | ( | const BLOCXX_NAMESPACE::String & | component = "" |
) |
Create a new Logger instance.
If no instance component is specified, then the default component of the registered logger is used instead.
component | instance local component name |
limal::Logger::~Logger | ( | ) |
Destroy a Logger instance
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::createCerrLogger | ( | const BLOCXX_NAMESPACE::String & | component, | |
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | components, | |||
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | categories, | |||
const BLOCXX_NAMESPACE::String & | messageFormat | |||
) | [static] |
Create a new logger reference to a CerrLogger object.
A application can use this function to create a logger reference pointing to a CerrLogger. The resulting reference object can be passed to the setDefaultLogger() or setThreadLogger() methods.
blocxx::Array<blocxx::String> components; components.push_back("*"); blocxx::Array<blocxx::String> categories; categories.push_back("*"); blocxx::LoggerRef l = limal::Logger::createCerrLogger( "main", components, categories, "%r [%d] %p %c - %m" ); limal::Logger::setDefaultLogger(l);
component | The default component name (application name). | |
components | A filter list with component names that should be logged or "*" to log all components. | |
categories | A filter list with category names that should be logged or "*" for all categories. Category names are "FATAL", "ERROR", "INFO", "DEBUG". | |
messageFormat | A log message format string or empty string for default format defined by the logger. |
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::createFileLogger | ( | const BLOCXX_NAMESPACE::String & | component, | |
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | components, | |||
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | categories, | |||
const BLOCXX_NAMESPACE::String & | messageFormat, | |||
const BLOCXX_NAMESPACE::String & | filename, | |||
BLOCXX_NAMESPACE::UInt64 | maxLogFileSize = 0 , |
|||
BLOCXX_NAMESPACE::UInt32 | maxBackupIndex = 0 | |||
) | [static] |
Create a new logger reference to a FileLogger object.
A application can use this function to create a logger reference pointing to a FileLogger. The resulting reference object can be passed to the setDefaultLogger() or setThreadLogger() methods.
blocxx::Array<blocxx::String> components(1, "*"); blocxx::Array<blocxx::String> categories(1, "*"); blocxx::LoggerRef l = limal::Logger::createFileLogger( "main", components, categories, "%r [%d] %p %c - %m", "/var/log/limal.log", 1024, 2 ); limal::Logger::setDefaultLogger(l);
component | The default component name (application name). | |
components | A filter list with component names that should be logged or "*" to log all components. | |
categories | A filter list with category names that should be logged or "*" for all categories. Category names are "FATAL", "ERROR", "INFO", "DEBUG". | |
messageFormat | A log message format string or empty string for default format defined by the logger. | |
filename | The name of the log file. | |
maxLogFileSize | The maximal file size in kb. 0 disables automatic log file rotation. | |
maxBackupIndex | Maximal count of backup log files. |
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::createNullLogger | ( | const BLOCXX_NAMESPACE::String & | component, | |
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | components, | |||
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | categories, | |||
const BLOCXX_NAMESPACE::String & | messageFormat | |||
) | [static] |
Create a new logger reference to a NullLogger object.
A application can use this function to create a logger reference pointing to a NullLogger. The resulting reference object can be passed to the setDefaultLogger() or setThreadLogger() methods.
blocxx::Array<blocxx::String> components(1, "*"); blocxx::Array<blocxx::String> categories(1, "*"); blocxx::LoggerRef l = limal::Logger::createNullLogger( "main", components, categories, "%r [%d] %p %c - %m" ); limal::Logger::setDefaultLogger(l);
component | The default component name (application name). | |
components | A filter list with component names that should be logged or "*" to log all components. | |
categories | A filter list with category names that should be logged or "*" for all categories. Category names are "FATAL", "ERROR", "INFO", "DEBUG". | |
messageFormat | A log message format string or empty string for default format defined by the logger. |
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::createSyslogLogger | ( | const BLOCXX_NAMESPACE::String & | component, | |
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | components, | |||
const BLOCXX_NAMESPACE::Array< BLOCXX_NAMESPACE::String > & | categories, | |||
const BLOCXX_NAMESPACE::String & | messageFormat, | |||
const BLOCXX_NAMESPACE::String & | identity, | |||
const BLOCXX_NAMESPACE::String & | facility | |||
) | [static] |
Create a new logger reference to a SyslogLogger object.
A application can use this function to create a logger reference pointing to a SyslogLogger. The resulting reference object can be passed to the setDefaultLogger() or setThreadLogger() methods.
blocxx::Array<blocxx::String> components(1, "*"); blocxx::Array<blocxx::String> categories(1, "*"); blocxx::LoggerRef l = limal::Logger::createSyslogLogger( "main", components, categories, "%r [%d] %p %c - %m", "myApp", "user" ); limal::Logger::setDefaultLogger(l);
component | The default component name (application name). | |
components | A filter list with component names that should be logged or "*" to log all components. | |
categories | A filter list with category names that should be logged or "*" for all categories. Category names are "FATAL", "ERROR", "INFO", "DEBUG". | |
messageFormat | A log message format string or empty string for default format defined by the logger. | |
identity | The syslog identity string. | |
facility | The syslog facility to use ("user", "daemon", ...) |
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::getCurrentAsLoggerRef | ( | ) | [static, private] |
Referenced by getCurrentLogger().
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::getCurrentLogger | ( | ) | [inline, static] |
Get a copy of the per thread logger (LoggerRef) or if not set, the default one.
References getCurrentAsLoggerRef().
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::getDefaultAsLoggerRef | ( | ) | [static, private] |
Referenced by getDefaultLogger().
static BLOCXX_NAMESPACE ::LoggerRef limal::Logger::getDefaultLogger | ( | ) | [inline, static] |
Returns a copy of default logger (LoggerRef).
References getDefaultAsLoggerRef().
bool limal::Logger::isEnabledFor | ( | const BLOCXX_NAMESPACE::String & | category | ) | const |
Check if the logger is enabled for given category (named LogLevel, but not limited to).
category | a log category |
bool limal::Logger::isEnabledFor | ( | const ELogLevel | level | ) | const |
Check if the logger is enabled for given level.
level | a log level |
void limal::Logger::logMessage | ( | const BLOCXX_NAMESPACE::String & | category, | |
const BLOCXX_NAMESPACE::String & | message, | |||
const char * | filename = 0 , |
|||
int | fileline = -1 , |
|||
const char * | methodname = 0 | |||
) | const |
log a message using the specified category (a log level name) inclusive component name associated with the instance if not empty.
category | a log category | |
message | a log message | |
filename | a file name (__FILE__) | |
fileline | a line number (__LINE__) | |
methodname | method name (__func__) |
void limal::Logger::logMessage | ( | ELogLevel | level, | |
const BLOCXX_NAMESPACE::String & | message, | |||
const char * | filename = 0 , |
|||
int | fileline = -1 , |
|||
const char * | methodname = 0 | |||
) | const |
log a message with the specified level inclusive the component name associated with the instance if not empty.
level | a log level | |
message | a log message | |
filename | a file name (__FILE__) | |
fileline | a line number (__LINE__) | |
methodname | method name (__func__) |
static bool limal::Logger::setDefaultFromLoggerRef | ( | const BLOCXX_NAMESPACE::LoggerRef & | ref | ) | [static, private] |
Referenced by setDefaultLogger().
static bool limal::Logger::setDefaultLogger | ( | const BLOCXX_NAMESPACE::LoggerRef & | ref | ) | [inline, static] |
Set the default logger.
ref | Reference to the new logger |
References setDefaultFromLoggerRef().
static bool limal::Logger::setThreadFromLoggerRef | ( | const BLOCXX_NAMESPACE::LoggerRef & | ref | ) | [static, private] |
Referenced by setThreadLogger().
static bool limal::Logger::setThreadLogger | ( | const BLOCXX_NAMESPACE::LoggerRef & | ref | ) | [inline, static] |
Set a per thread logger that overrides the default one.
ref | Reference to the new logger |
AssertException | if try to put the logger into a thread local storage area failed. |
References setThreadFromLoggerRef().
BLOCXX_NAMESPACE ::String limal::Logger::m_component [private] |