Problem

How do I change the name GlobalLogger or StaticMethodLogger within C++ logs?

Solution

A: There is more than one way to do this but only one is recommended.

To answer this question completely, a little background on how the ACS logging system in C++ works is needed. Basically all the non-static context logging macros defined within loggingACEMACROS.h are dependent upon a single function or method being available:

   Logging::Logger::LoggerSmartPtr
   getLogger();

which ACS has implemented in the logging library already. The name of the Logger object returned by this ACS-implemented function is GlobalLogger. The macros themselves then just make calls to getLogger()->log(...) which of course is completely hidden.

A positive consequence of this is that:

  • one can implement a member function within their own class named getLogger having a signature identical to the getLogger() function described above
  • the ACS logging macros will then use the Logger object returned by your class's getLogger method instead of the globally available getLogger() function
  • a trivial implementation of getLogger which just changes the name of the SourceObject (i.e., "GlobalLogger") could be something like:
protected:
   Logging::Logger::LoggerSmartPtr
   getLogger()
   {
     return ::getNamedLogger("whatever name I choose");
   }

In this case, all we do is use another ACS-provided function to create a Logger with a name of our choice.


As for changing the name of the Logger used by static logging macros:

  • implement a static method called getNamedLogger following the signature defined in loggingGetLogger.h
  • within your getNamedLogger implementation, ignore the name parameter being passed in entirely. Instead, use whatever name you want.
  • a sample implementation of getNamedLogger could be:
Logging::Logger::LoggerSmartPtr
getNamedLogger(const std::string& loggerName)
{
    return ::getNamedLogger("my name and not loggerName");
}