Overview
Features
Download
Documentation
Community
Add-Ons & Services

Using multiple loggers in an application

Please post support and help requests here.

Using multiple loggers in an application

Postby sampsont » 13 May 2009, 20:30

In the ServerApplication framework, I created a new logger called fLog. The following code produces the following results:
Code: Select all
      LOG.names( loggerNames );
      for (std::vector<std::string>::iterator it = loggerNames.begin(); it != loggerNames.end(); ++it)
         LogStream(LOG).trace() << "Logger: " << *it << std::endl;


    Application.Trace - Logger:
    Application.Trace - Logger: Application
    Application.Trace - Logger: ApplicationStartup
    Application.Trace - Logger: fLog

To use the new logger, I wrote this code:
Code: Select all
   Logger* pfLog = Logger::has("fLog");
   if( pfLog ) pfLog->trace("New message");

Application::instance().logger() returns THE installed logger - even though there are multiple loggers installed. I had to use the code above to access my new logger.

Am I going about this in the right way? Using the new logger seems a little awkward.

Thanks
sampsont
 
Posts: 31
Joined: 05 Mar 2009, 17:08

Re: Using multiple loggers in an application

Postby alex » 14 May 2009, 19:21

sampsont wrote:Am I going about this in the right way? Using the new logger seems a little awkward.


Documentation wrote:After the logging subsystem has been initialized, which usually happens as the first action in Application::initialize(), the application's logger is the one specified by the "application.logger" configuration property. If that property is not specified, the logger is "Application".
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Using multiple loggers in an application

Postby sampsont » 14 May 2009, 23:19

That's true, and I can use the Application.logger() OK. But I have another logger called fLog. Why would I want to do this? Because fLog goes to a specific file. I have some specific things that I want to log in this file.

The problem is that there seems to be a concept of 'THE APPLICATIONS LOGGER' but I have multiple loggers. Can't I have multiple loggers active and then use which ever one I want to use depending on what I'm logging?

When I want to use fLog, (the NON-application logger), the only way I can figure out how to use it is like this:
Code: Select all
   Logger* pfLog = Logger::has("fLog");
   if( pfLog ) pfLog->trace("New message");

Am I trying to do something unusual? If so, why are you able to create multiple loggers if you can't easily use them? I guess the question is: Is there an easier way to use multiple loggers in a ServerApplication than the way I'm doing it?
Thanks
sampsont
 
Posts: 31
Joined: 05 Mar 2009, 17:08

Re: Using multiple loggers in an application

Postby alex » 15 May 2009, 00:00

sampsont wrote:Is there an easier way to use multiple loggers in a ServerApplication than the way I'm doing it?


For one thing, you could use get(name) instead of has(name). Logger::get(name) will create logger on first attempt if it does not exist and it returns the reference, so your code would be simplified somewhat - no need for null pointer check. This is all documented.

Alternatively, you can have your own logger(s), independent from Application. Look at Logger sample to learn how to do that.

POCO logging system is very sophisticated and you can do pretty much anything you can think of with it. You can do everything through config files, without having to tinker with code. You can look into SampleApp.properties for an example.
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest