Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable spdlog logging in code?

Tags:

c++

spdlog

I am creating c++ library modules in my application. To do logging, I use spdlog. But in a production environment, I don't want my lib modules to do any logging. One way to achieve turning on/off would be to litter my code with #ifdef conditionals like...

#ifdef logging
  // call the logger here.
#endif

I am looking for a way to avoid writing these conditionals. May be a wrapper function that does the #ifdef checking and write it. But the problem with this approach is that I have to write wrappers for every logging method (such as info, trace, warn, error, ...)

Is there a better way?

like image 879
Mopparthy Ravindranath Avatar asked Aug 10 '17 19:08

Mopparthy Ravindranath


People also ask

How to disable a particular logger in logging?

Logging has the following process (represented by a flowchart): Therefore to disable a particular logger you can adopt one of the following strategies: Set the level of the logger to logging.CRITICAL + 1. import logging logger = logging.getLogger ("foo") logger.setLevel (logging.CRITICAL + 1)

Does spdlog support Fluentd or Logstash?

One of the popular and fast logging libraries for C++ is spdlog. It's built using C++11 and can be used either as a header-only library or as a static library (which reduces compile time). One feature that might be missing from spdlog is the direct support for Logstash or Fluentd.

What is spdlog used for?

It's built using C++11 and can be used either as a header-only library or as a static library (which reduces compile time). One feature that might be missing from spdlog is the direct support for Logstash or Fluentd.

What is missing from spdlog?

One feature that might be missing from spdlog is the direct support for Logstash or Fluentd. If you want to use one of these aggregators, it is still possible to configure spdlog with file sink output and use Filebeat or Fluent Bit to forward the file contents to the appropriate aggregator.


3 Answers

You can disable logging with set_level():

auto my_logger = spdlog::basic_logger_mt("basic_logger", "logs/basic.txt");

#if defined(PRODUCTION)
    my_logger->set_level(spdlog::level::off);
#else
    my_logger->set_level(spdlog::level::trace);
#endif

spdlog::register_logger(my_logger);
like image 125
iamantony Avatar answered Sep 20 '22 08:09

iamantony


You can disable all logging before you compile the code by adding the following macro (before including spdlog.h):

#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF
#include<spdlog.h>

It is explained as a comment in the file https://github.com/gabime/spdlog/blob/v1.x/include/spdlog/spdlog.h :

//
// enable/disable log calls at compile time according to global level.
//
// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h):
// SPDLOG_LEVEL_TRACE,
// SPDLOG_LEVEL_DEBUG,
// SPDLOG_LEVEL_INFO,
// SPDLOG_LEVEL_WARN,
// SPDLOG_LEVEL_ERROR,
// SPDLOG_LEVEL_CRITICAL,
// SPDLOG_LEVEL_OFF
//

Using this macro will also speed up your productive code because the logging calls are completely erased from your code. Therefore this approach may be better than using my_logger->set_level(spdlog::level::off);

However, in order for the complete code removal to work you need to use either of the macros when logging:

SPDLOG_LOGGER_###(logger, ...)

SPDLOG_###(...)

where ### is one of TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL.

The latter macro uses the default logger spdlog::default_logger_raw(), the former can be used with your custom loggers. The variadic arguments ... stand for the regular arguments to your logging invocation: the fmt string, followed by some values to splice into the message.

like image 25
user74696c Avatar answered Sep 21 '22 08:09

user74696c


I don't know spdlog.

However, you may define a macro in one of your common used include file, to replace the logcall by nothing, or a call to an empty inline function which the compiler optimizer will eliminate.

in "app.h"

#ifndef LOG

#ifdef logging
#define LOG spdlog
#endif

#ifndef logging
#define LOG noop
#endif

#endif

Did you get the idea?

This let most of your code untouched

like image 31
stefan bachert Avatar answered Sep 21 '22 08:09

stefan bachert