You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Nandan S <na...@hp.com> on 2014/04/03 12:20:41 UTC

clearing Loggers from logger repository

I’m creating an application that requires the above feature.

In a loop, executing following code
1)      I’m reading xml configuration file.
2)      Logging messages after reading the configuration file 
3)      Also reading the Logger Repository to check all the loggers and
print the same on the console
4)      Sleep(3000)


       log4cxx::LoggerList loggerlst;
       log4cxx::spi::LoggerRepositoryPtr repo;
LoggerPtr logger = Logger::getRootLogger();

                for ( int ii=0 ; ii<5 ; ii++)
       {                    
             
DOMConfigurator::configureAndWatch("C:\\log_config\\Log4cxxConfig.xml",1000);
              repo = logger->getLoggerRepository();
              logger = repo->exists(L"customLogger1");
              
              if( NULL == logger)
              {
                     logger = Logger::getRootLogger();
              }
              
              loggerlst = repo->getCurrentLoggers();
              
              for(int jj=0;jj<loggerlst.size();jj++)
              {
                     wcout &lt;&lt;&quot; logger
&quot;&lt;&lt;jj&lt;&lt;&quot;:&quot;&lt;&lt;loggerlst.at(jj)->getName()
<<endl;
                     log4cxx::AppenderList list =
loggerlst.at(jj)->getAllAppenders();
                     cout <<"       number of appenders : "<< list.size()
<<endl;
                     for( int kk=0;kk&lt;list.size();kk++)
                     {
                            wcout &lt;&lt; &quot;          :: list:  &quot;
&lt;&lt; kk &lt;&lt; list.at(kk)->getName() << endl;
                     }
              }
              
              LOG4CXX_TRACE(logger, "Custom Logger :this is a debug message
for detailed code discovery. Value=" << value);
              LOG4CXX_DEBUG(logger, "Custom Logger :this is a debug
message.");
              LOG4CXX_INFO (logger, "Custom Logger :this is a info message,
ignore. Value=" << value);
              LOG4CXX_WARN (logger, "Custom Logger :this is a warn message,
not too bad.");
              LOG4CXX_ERROR(logger, "Custom Logger :this is a error message,
something serious is happening.");
              LOG4CXX_FATAL(logger, "Custom Logger :this is a fatal
message!!!");

              Sleep(3000);
       }

My XML configuration is shown below

<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

                <appender name="NormalAppender"
class="org.apache.log4j.RollingFileAppender">
                                
                                
                                
                                
                                <layout
class="org.apache.log4j.PatternLayout">
                                                
                                </layout>
                </appender>

                <logger name="customLogger1">
                                <level value="FATAL"/>
                                <appender-ref ref="NormalAppender"/>
                </logger>

                <logger name="customLogger2">
                                <level value="TRACE"/>
                                <appender-ref ref="NormalAppender"/>
                </logger>

                <logger name="customLogger5">
                                <level value="TRACE"/>
                                <appender-ref ref="NormalAppender"/>
                </logger>

                <root>
                <priority value="INFO" />
                                <appender-ref ref="NormalAppender"/>
                </root>
                
</log4j:configuration>

My purpose is as follows,

In the above code I’m checking whether “customLogger1” exists or not. If it
exists I’ll be getting a pointer of “customLogger1” else NULL.
If “customLogger1” is not NULL then I’ll be using the properties associated
with that “customLogger1”.  In other case if “customLogger1” is NULL then
I’m Making Logger Pointer to point to default root Logger.

Output is as follows
---------------------
logger 0:customLogger1
       number of appenders : 1
         :: list:  0 NormalAppender
logger 1:customLogger2
       number of appenders :  1
         :: list:  0 NormalAppender
logger 2:customLogger5
       number of appenders :  1
         :: list:  0 NormalAppender
---------------------

In the next iteration suppose I’ll rename “customLogger1” to
“customLogger20” in configuration file, Logger Repository will be having
both “customLogger1” and “customLogger20”.

But My requirement is if “customLogger1” is deleted or modified in
configuration file, it must by default set to root logger.

Output is as follows
---------------------
logger 0:customLogger1
       number of appenders :  1
         :: list:  0 NormalAppender
logger 1:customLogger20
       number of appenders :  1
         :: list:  0 NormalAppender
logger 2:customLogger2
       number of appenders :  1
         :: list:  0 NormalAppender
logger 3:customLogger5
       number of appenders :  1
         :: list:  0 NormalAppender
---------------------

where in i expect the output to be
Output is as follows
---------------------
logger 0:customLogger20
       number of appenders :  1
         :: list:  0 NormalAppender
logger 1:customLogger2
       number of appenders :  1
         :: list:  0 NormalAppender
logger 2:customLogger5
       number of appenders :  1
         :: list:  0 NormalAppender
---------------------
Do let me know, how can i achieve this ?




--
View this message in context: http://apache-logging.6191.n7.nabble.com/clearing-Loggers-from-logger-repository-tp45893.html
Sent from the Log4cxx - Users mailing list archive at Nabble.com.

Re: clearing Loggers from logger repository

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag Nandan S,
am Donnerstag, 3. April 2014 um 12:20 schrieben Sie:

> But My requirement is if “customLogger1” is deleted or modified in
> configuration file, it must by default set to root logger.
[...]
> Do let me know, how can i achieve this ?

As with your(?) previous question some weeks ago, I would suggest
overriding XMLWatchdog::doOnChange in domconfigurator.cpp and
reset/clean the whole configuration on each iteration using
LogManager::resetConfiguration.

Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning       E-Mail:Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme      http://www.AM-SoFT.de/

Telefon...........05151-  9468- 55
Fax...............05151-  9468- 88
Mobil..............0178-8 9468- 04

AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hannover HRB 207 694 - Geschäftsführer: Andreas Muchow