Public Types | Public Member Functions

atscppapi::Logger Class Reference

Create log files that are automatically rolled and cleaned up as space is required. More...

#include <Logger.h>

Inherits atscppapi::noncopyable.

Collaboration diagram for atscppapi::Logger:
Collaboration graph
[legend]

Public Types

enum  LogLevel { LOG_LEVEL_NO_LOG = 128, LOG_LEVEL_DEBUG = 1, LOG_LEVEL_INFO = 2, LOG_LEVEL_ERROR = 4 }
 

The available log levels.

More...

Public Member Functions

 Logger ()
 ~Logger ()
bool init (const std::string &file, bool add_timestamp=true, bool rename_file=true, LogLevel level=LOG_LEVEL_INFO, bool rolling_enabled=true, int rolling_interval_seconds=3600)
 You must always init() a Logger before you begin logging.
void setRollingIntervalSeconds (int seconds)
 Allows you to change the rolling interval in seconds.
int getRollingIntervalSeconds () const
void setRollingEnabled (bool enabled)
 Enables or disables log rolling.
bool isRollingEnabled () const
void setLogLevel (Logger::LogLevel level)
 Change the log level.
Logger::LogLevel getLogLevel () const
void flush ()
 This method allows you to flush any log lines that might have been buffered.
void logDebug (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 This method writes a DEBUG level message to the log file, the LOG_DEBUG macro in Logger.h should be used in favor of these when possible because it will produce a much more rich debug message.
void void logInfo (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 This method writes an INFO level message to the log file, the LOG_INFO macro in Logger.h should be used in favor of these when possible because it will produce a much more rich info message.
void void void logError (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 This method writes an ERROR level message to the log file, the LOG_ERROR macro in Logger.h should be used in favor of these when possible because it will produce a much more rich error message.

Detailed Description

Create log files that are automatically rolled and cleaned up as space is required.

Log files created using the Logger class will be placed in the same directory as other log files, that directory is specified in records.config. All of the logging configuration such as max space available for all logs includes any logs created using the Logger class.

Loggers are very easy to use and a full example is available in examples/logger_example/, a simple example is:

 // See Logger::init() for an explanation of the init() parameters.
 log.init("logger_example", true, true, Logger::LOG_LEVEL_DEBUG);
 // You have two ways to log to a logger, you can log directly on the object itself:
 log.logInfo("Hello World from: %s", argv[0]);
 // Alternatively you can take advantage of the super helper macros for logging
 // that will include the file, function, and line number automatically as part
 // of the log message:
 LOG_INFO(log, "Hello World with more info from: %s", argv[0]);
Warning:
Log rolling doesn't work correctly in 3.2.x see: https://issues.apache.org/jira/browse/TS-1813 Apply the patch in TS-1813 to correct log rolling in 3.2.x

Definition at line 158 of file Logger.h.


Member Enumeration Documentation

The available log levels.

Enumerator:
LOG_LEVEL_NO_LOG 

This log level is used to disable all logging.

LOG_LEVEL_DEBUG 

This log level is used for DEBUG level logging (DEBUG + INFO + ERROR).

LOG_LEVEL_INFO 

This log level is used for INFO level logging (INFO + ERROR).

LOG_LEVEL_ERROR 

This log level is used for ERROR level logging (ERROR ONLY).

Definition at line 164 of file Logger.h.


Constructor & Destructor Documentation

atscppapi::Logger::Logger (  ) 
atscppapi::Logger::~Logger (  ) 

Member Function Documentation

void atscppapi::Logger::flush (  ) 

This method allows you to flush any log lines that might have been buffered.

Warning:
This method can cause serious performance degredation so you should only use it when absolutely necessary.
Logger::LogLevel atscppapi::Logger::getLogLevel (  )  const
Returns:
The current log level.
See also:
LogLevel
int atscppapi::Logger::getRollingIntervalSeconds (  )  const
Returns:
the number of seconds between log rolls.
bool atscppapi::Logger::init ( const std::string &  file,
bool  add_timestamp = true,
bool  rename_file = true,
LogLevel  level = LOG_LEVEL_INFO,
bool  rolling_enabled = true,
int  rolling_interval_seconds = 3600 
)

You must always init() a Logger before you begin logging.

If you do not call init() nothing will happen.

Parameters:
file The name of the file to create in the logging directory, if you do not specify an extension .log will be used.
add_timestamp Prepend a timestamp to the log lines, the default value is true.
rename_file If a file already exists by the same name it will attempt to rename using a scheme that appends .1, .2, and so on, the default value for this argument is true.
level the default log level to use when creating the logger, this is set to LOG_LEVEL_INFO by default.
rolling_enabled if set to true this will enable log rolling on a periodic basis, this is enabled by default.
rolling_interval_seconds how frequently to roll the longs in seconds, this is set to 3600 by default (one hour).
Returns:
returns true if the logger was successfully created and initialized.
See also:
LogLevel
bool atscppapi::Logger::isRollingEnabled (  )  const
Returns:
A boolean value which represents whether rolling is enabled or disabled.
void atscppapi::Logger::logDebug ( const char *  fmt,
  ... 
)

This method writes a DEBUG level message to the log file, the LOG_DEBUG macro in Logger.h should be used in favor of these when possible because it will produce a much more rich debug message.

Sample usage:

 log.logDebug("Hello you are %d years old", 27);
void void void atscppapi::Logger::logError ( const char *  fmt,
  ... 
)

This method writes an ERROR level message to the log file, the LOG_ERROR macro in Logger.h should be used in favor of these when possible because it will produce a much more rich error message.

void void atscppapi::Logger::logInfo ( const char *  fmt,
  ... 
)

This method writes an INFO level message to the log file, the LOG_INFO macro in Logger.h should be used in favor of these when possible because it will produce a much more rich info message.

void atscppapi::Logger::setLogLevel ( Logger::LogLevel  level  ) 

Change the log level.

Parameters:
level the new log level to use
See also:
LogLevel
void atscppapi::Logger::setRollingEnabled ( bool  enabled  ) 

Enables or disables log rolling.

Parameters:
enabled true to enable log rolling, false to disable it.
void atscppapi::Logger::setRollingIntervalSeconds ( int  seconds  ) 

Allows you to change the rolling interval in seconds.

Parameters:
seconds the number of seconds between rolls

The documentation for this class was generated from the following file: