Skip to content
ZhreShold edited this page Sep 15, 2015 · 9 revisions

Quick start to logging

First log

#include "zupply.hpp" // as usual, include the header
using namespace zz;
...
int main(int argc, char** argv)
{
    auto logger = log::get_logger("default");
    logger->info("Welcome to zupply logger!");
}

Output

[15/09/14 19:26:56.983][T129][default][INFO] Welcome to zupply logger!

Log more in one line

Loggers support three logging styles for convenience.

  • c++ stream style
logger->info() << "This is " << "style " << 1;
[15-09-14 20:35:32.443][T1][default][INFO] This is style 1
  • c++ stream style variant
logger->info("This is ") << "style " << 2;
[15-09-14 20:35:32.443][T1][default][INFO] This is style 2
  • python style
logger->info("Style {}, python style formatter. With placeholder {}, {}", 3, 2.33f, -1.2f);
[15-09-14 20:35:32.443][T1][default][INFO] Style 3, python style formatter. With placeholder 2.33, -1.2
Note
  • You can chain(<<) or format({}) any type as long as std::stringstream support << it.
  • Logger will take care of new line characters, so don't bother add one by hand(life saver!).

Control importance levels

Zupply logger support six logging levels which is sufficient for most scenarios:

  • trace: verbose information that is useful but may slow down execution or messy.
  • debug: debug information
  • info: normal message
  • warn: detected warning information, but program could continue
  • error: error detected, but allowed to continue
  • fatal: fatal error, need to abort program
For example
logger->warn("Some warning here");
logger->info("Normal info");
logger->debug("By default, debug msg appear in debug mode only");
logger->trace("By default, trace msg will not be logged");

Story behind the logging module

Logging system is the fundamental module in each project as long as you want to log something, whatever it is. The more you use it, the stronger feeling you have that basic fprintf() or iostream is not enough.

What a logger should do

  • Filter logging levels. For instance, You don't want debug messages in release mode.
  • Formatting. Process various kind of data type internally, like int, float, etc...
  • Attributing. Add timestamps, logging level, application name, etc.
  • Buffering. Using internal buffering techniques to speed up.
  • Work as an agent. Should be able to control files, streams without specific instructions.

Principles in designing zupply logger in c++

  • Simple. Users should only care about what to log, loggers take care of the rest.
  • Convenient. Loggers should support formatting, as well as chain input.
  • Highly configurable. Easy to config by built-in functions and more importantly, configuration file, such that you can control log output without touching the code.
  • Hit the point. Loggers should support a basic feature which allow user to log message into multiple outputs simultaneously. Unfortunately, many logging libraries bind each logger to single output.
  • Always available. Treat loggers as resources, when you need it, go get it.
  • Flexible. We can have multiple loggers
  • Thread-safe. Either using lock or lock-free mechanics to synchronize among threads.
  • Concrete. Fulfill the useful functions only, ignore those complicated features, keep it light-weight.

Design

Clone this wiki locally