atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
Public Types | Public Member Functions | List of all members
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.

Public Types

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

Public Member Functions

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)
 
void setRollingIntervalSeconds (int seconds)
 
int getRollingIntervalSeconds () const
 
void setRollingEnabled (bool enabled)
 
bool isRollingEnabled () const
 
void setLogLevel (Logger::LogLevel level)
 
Logger::LogLevel getLogLevel () const
 
void flush ()
 
void logDebug (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 
void void logInfo (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 
void void void logError (const char *fmt,...) ATSCPPAPI_PRINTFLIKE(2
 

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

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)

Member Function Documentation

void 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 Logger::getLogLevel ( ) const
Returns
The current log level.
See Also
LogLevel
int Logger::getRollingIntervalSeconds ( ) const
Returns
the number of seconds between log rolls.
bool 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
fileThe name of the file to create in the logging directory, if you do not specify an extension .log will be used.
add_timestampPrepend a timestamp to the log lines, the default value is true.
rename_fileIf 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.
levelthe default log level to use when creating the logger, this is set to LOG_LEVEL_INFO by default.
rolling_enabledif set to true this will enable log rolling on a periodic basis, this is enabled by default.
rolling_interval_secondshow 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 Logger::isRollingEnabled ( ) const
Returns
A boolean value which represents whether rolling is enabled or disabled.
void 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 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 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 Logger::setLogLevel ( Logger::LogLevel  level)

Change the log level

Parameters
levelthe new log level to use
See Also
LogLevel
void Logger::setRollingEnabled ( bool  enabled)

Enables or disables log rolling

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

Allows you to change the rolling interval in seconds

Parameters
secondsthe number of seconds between rolls

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