NanoLog

October 17, 2016 ยท View on GitHub

  • Low Latency C++11 Logging Library.
  • It's fast. Very fast. See Latency benchmark
  • NanoLog only uses standard headers so it should work with any C++11 compliant compiler.
  • Supports typical logger features namely multiple log levels, log file rolling and asynchronous writing to file.

Design highlights

  • Zero copying of string literals.
  • Lazy conversion of integers and doubles to ascii.
  • No heap memory allocation for log lines representable in less than ~256 bytes.
  • Minimalistic header includes. Avoids common pattern of header only library. Helps in compilation times of projects.

Guaranteed and Non Guaranteed logging

  • Nanolog supports Guaranteed logging i.e. log messages are never dropped even at extreme logging rates.
  • Nanolog also supports Non Guaranteed logging. Uses a ring buffer to hold log lines. In case of extreme logging rate when the ring gets full (i.e. the consumer thread cannot pop items fast enough), the previous log line in the slot will be dropped. Does not block producer even if the ring buffer is full.

Usage

#include "NanoLog.hpp"

int main()
{
  // Ensure initialize is called once prior to logging.
  // This will create log files like /tmp/nanolog1.txt, /tmp/nanolog2.txt etc.
  // Log will roll to the next file after every 1MB.
  // This will initialize the guaranteed logger.
  nanolog::initialize(nanolog::GuaranteedLogger(), "/tmp/", "nanolog", 1);
  
  // Or if you want to use the non guaranteed logger -
  // ring_buffer_size_mb - LogLines are pushed into a mpsc ring buffer whose size
  // is determined by this parameter. Since each LogLine is 256 bytes,
  // ring_buffer_size = ring_buffer_size_mb * 1024 * 1024 / 256
  // In this example ring_buffer_size_mb = 3.
  // nanolog::initialize(nanolog::NonGuaranteedLogger(3), "/tmp/", "nanolog", 1);
  
  for (int i = 0; i < 5; ++i)
  {
    LOG_INFO << "Sample NanoLog: " << i;
  }
  
  // Change log level at run-time.
  nanolog::set_log_level(nanolog::LogLevel::CRIT);
  LOG_WARN << "This log line will not be logged since we are at loglevel = CRIT";
  
  return 0;
}

Latency benchmark of Guaranteed logger

Thread Count 1 - percentile latency numbers in microseconds (lower number means better performance)

Logger50th75th90th99th99.9thWorstAverage
nanolog_guaranteed01148680.347930
spdlog3335111292.588590
g3log56610191865.206230
reckless001117518611.829760

Thread Count 2 - percentile latency numbers in microseconds (lower number means better performance)

Logger50th75th90th99th99.9thWorstAverage
nanolog_guaranteed01125550.457240
nanolog_guaranteed01125810.459090
spdlog23335252.449580
spdlog23336212.457150
g3log4561218644.574850
g3log4561220844.586590
reckless0111141715924.412750
reckless0111241721384.427810

Thread Count 3 - percentile latency numbers in microseconds (lower number means better performance)

Logger50th75th90th99th99.9thWorstAverage
nanolog_guaranteed01136910.450700
nanolog_guaranteed01237900.676050
nanolog_guaranteed012372620.680430
spdlog2224667291.803570
spdlog33358252.679420
spdlog333510502.685230
g3log4461727534.385530
g3log4461626554.435680
g3log678192910315.896250
reckless1112981643307011.208420
reckless1113822266300612.310360
reckless1111672839324912.754520

Thread Count 4 - percentile latency numbers in microseconds (lower number means better performance)

Logger50th75th90th99th99.9thWorstAverage
nanolog_guaranteed01236530.582140
nanolog_guaranteed01237700.608980
nanolog_guaranteed01237620.803630
nanolog_guaranteed01237610.797270
spdlog22235401.767930
spdlog22236211.768640
spdlog33348242.676170
spdlog333510312.698580
g3log445173077664.620760
g3log679213584786.368940
g3log678223213277.023880
g3log789233684707.831750
reckless1115063477922418.959310
reckless1114793636847119.181160
reckless11153029901165819.245110
reckless1114363641862619.342780

Latency benchmark of Non guaranteed logger

  • Take a look at non_guaranteed_nanolog_benchmark.cpp for the code used to generate the latency numbers.
  • Benchmark was compiled with g++ 4.8.4 running Linux Mint 17 on Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
Thread count: 1
	Average NanoLog Latency = 131 nanoseconds
Thread count: 2
	Average NanoLog Latency = 182 nanoseconds
	Average NanoLog Latency = 272 nanoseconds
Thread count: 3
	Average NanoLog Latency = 216 nanoseconds
	Average NanoLog Latency = 209 nanoseconds
	Average NanoLog Latency = 315 nanoseconds
Thread count: 4
	Average NanoLog Latency = 229 nanoseconds
	Average NanoLog Latency = 221 nanoseconds
	Average NanoLog Latency = 233 nanoseconds
	Average NanoLog Latency = 332 nanoseconds
Thread count: 5
	Average NanoLog Latency = 247 nanoseconds
	Average NanoLog Latency = 240 nanoseconds
	Average NanoLog Latency = 320 nanoseconds
	Average NanoLog Latency = 345 nanoseconds
	Average NanoLog Latency = 383 nanoseconds

Crash handling

  • g3log has support for crash handling. I do not see the point in re-inventing the wheel. Have a look at that what's done there and if it works for you, give Kjell credit and use his crash handling code.

Tips to make it faster!

  • NanoLog uses standard library chrono timestamps. Your platform / os may have non-standard but faster timestamps. Use them!