Skip to content
Gautier Lefebvre edited this page Feb 13, 2018 · 3 revisions

LoggerManager

The framework allows you to define several loggers which will write logs in the format hh:mm:ss:µµµµµµ -- [logger name] -- message.

You can decide to make the Logger write to a file or to the terminal.

Logger level

There are 5 levels of logging. In ascending order:

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

DEBUG to WARNING logs are printed on the standard output (except if the Logger logs to a file). ERROR and CRITICAL logs are printed on the error output.

Create a Logger

To create a Logger, you must always use the LoggerManager:

// create a Logger named "foo" which prints any log with level DEBUG and above.
fwk::Logger& foo = fwk::LoggerManager::get().init("foo", fwk::Logger::Level::DEBUG);

// create a Logger named "bar" which prints any ERROR and CRITICAL log into a file.
fwk::Logger& bar = fwk::LoggerManager::get().init("bar", fwk::Logger::Level::ERROR, "/home/foobar/logs/logs.txt");

Get a Logger

Once created, you can get your Logger anywhere using:

fwk::Logger& foo = fwk::LoggerManager::get().getLogger("foo");

Delete a Logger

This isn't mandatory, since any logger will be destroyed with the framework, but you can remove a Logger using:

fwk::LoggerManager::get().endLogger("foo");

Log messages

fwk::Logger& foo = fwk::LoggerManager::get().init("foo", fwk::Logger::Level::INFO);

// nothing is written, as DEBUG < INFO
foo.log("debug message", fwk::Logger::Level::DEBUG);

// message written:
// 15:28:32:468413 -- foo -- info message
foo.log("info message", fwk::Logger::Level::INFO);

Ignore Logger messages

You can ignore any message from one of your logger by initializing it with the level fwk::Logger::Level::IGNORE. Any log, even with this level, will be ignored.

Default Logger

The logger with the name "cppframework" is already used. As long as you don't initialize it, any log from the framework will be ignored.