Search code examples
c++loggingverbosity

Verbosity level in simple Logger implemenation C++


Im currently implementing a simple Logger for a project due my studies in C++.

I already got a Logger base class, as some different appenders and the next step is to implement verbosity levels. The problem is that I´m not sure if i understood the concept of verbosity levels correctly and so I wanted to get some feebback before I start to implement them. So from my understanding a verbosity level in general works as following:

User creates two Logger first: f.e.:

FileLogger fl; 
VSLogger vl;

afterwards he can set the verbosity level like:

fl.setLoggerLevel(DEBUG);
vl.setLoggerLevel(FATAL_ERROR);

afterwards he can logg as he wants, like:

fl.logg("New Object of Type .... created");
ASSERT(1,2, "1==2");

while assert writes into the VSLogger with fatal error level

and in the Ouput file it would probably look like this:

13:36 Msg: New Object of Type .... created (LEVEL:DEBUG);

and in Visual Studio it would probably look like this:

13:36 Msg: Assert (1==2) failed (LEVEL:FATAL_ERROR)

Is that the sence of a verbosity level or have i missunderstood the concepts of verbosity level in general?


Solution

  • I don't see why the user should have to work with two loggers. The consuming code should not care about logging targets.

    Another issue is that your consuming code doesn't pass in the severity anywhere. Whenever you call a logging function you pass in the severity. The consuming code doesn't care what the current verbosity is, it just passes in a severity and relies on the implementation of the logger to know if the severity exceeds the verbosity level. (There is an exception to this rule, where you check against the verbosity level to avoid the overhead of creating the log message in high performance code)

    I'd rather have one logger as the user, with two attached outputs, which might have a different verbosity level.

    In the simplest case I'd create a global function Logger& GetLogger() which leads to user code like GetLogger().LogDebug("New Object of Type .... created");

    First create an interface:

    public class ILogger
    {
      public:
        virtual LogDebug(string message)=0;
        ...
    }
    

    Then create one instance that supports subscription:

    public class DispatchingLogger:ILogger
    {
      private:
        vector<ILogger*> loggers;
      public:
        override LogDebug(string message)
        {
          foreach(ILogger logger in loggers)
          {
            logger.LogDebug(message);
          }
        }
    
        void Subscribe(ILogger* logger)
        {
          loggers.add(logger);
        }
    }
    

    Then the global GetLogger() function returns a single instance of DispatchingLogger. And you can subscribe several implementations with different verbosity levels to it. And user classes that implement ILogger can registered too.

    (I know my C++ syntax is incorrect, has been a bit since I worked with C++)