AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
AnCH logger library documentation

Table of Contents

Introduction

AnCH logger library aims to provide facilities for applications logging.

Prerequisites

Installation

TODO fill this section

Usage

The library is used in a similar way that log4j. So the classes which will be used are LoggerFactory to initialize and retrieve logger instances and Logger to log messages.
Messages are logged with level which can be (from the lowest to the highest):

Each level has a method in Logger class which is named like level in lower case. These methods take a variable number of arguments which have to have stream injection operator definition.

User can define several writers to log messages in separate files and console. Three writer types are available:

Loggers are defined by category. They are retrieved if the logger name starts with category. Categories are associated with writers and minimum logging level.

AnCH logger library has to be initialized by calling ANCH_LOGGER_INIT macro.

Configuration file

AnCH logger configuration file is anch-logger.conf in the execution directory. You can defined another configuration file by defining _ANCH_LOGGER_CONFIG_FILE_ variable.

The file has to defined some writers and loggers (see sections below).
Global configuration variables which can be set are:

Writers

Writers are declared in [WRITER::<writer>] section.
[WRITER::console] is a predefined writer which log messages in std::cout . The only configuration value for console is writer.pattern .

Configuration values for writters are:

Categories

Categories are declared in [CATEGORY::<category>] section.
[CATEGORY::default] is a predefined category which will be used if no other category match the wanted logger.

Configuration values for categories are:

Example

Configuration file:

# Use low priority writers
low.priority=1
# std::cout writer
[WRITER::console]
writer.pattern=$d{%Y-%m-%d %H:%M:%S} - $c: $p - $m
# A file writer
[WRITER::test]
writer.filepath=./test.log
writer.pattern=$d{%Y-%m-%d %H:%M:%S} - $c [Thread $t] - $p - $m
writer.max.size=10M
writer.max.rotate.index=9
# Default logger configuration
[CATEGORY::default]
logger.writers=console
logger.level=trace
# Logger configuration for my application
[CATEGORY::org::myapp]
logger.writers=console,test
logger.level=debug

Application code:

#include <iostream>
#include "logger/loggerFactory.hpp"
using std::cout;
using std::endl;
ANCH_LOGGER_INIT;
int
main(void) {
const Logger& LOG = LoggerFactory::getLogger("org::myapp::Test"); // Use application logger
const Logger& DEF_LOG = LoggerFactory::getLogger("XYZ"); // Use default logger
DEF_LOG.info(_ANCH_LOGGER_CONFIG_FILE_); // Log only in console
for(int i = 0 ; i < 10 ; i++) {
LOG.info("Message n° ", i); // Log in console and file
}
return 0;
}