Pergunta

I am trying to get log4j 2.0 running on my application. I my application, I'm setting two loggers:

  1. One to output stuff to the console so you can see what's going on.

  2. Another to output test run results to a DB2 table. This is a completely different log to just track when a test was run and what the outcome was.

My trouble is with the DB2 logger. I can't seem to write records to the table. I know I'm nearly there because:

  • If I use a dummy library or table name, log4j errors during configuration.
  • If I use a bogus userid or password, log4j errors during configuration.
  • If I use a valid userid/password with no authority to the file, log4j errors during configuration.
  • BUT... if all those things are right, no errors, but also no records written.

In the console, I can see that log4j reconfiguration finishes correctly:

2013-10-02 11:31:06,261 DEBUG Reconfiguration completed

So.... what am I missing? There is not much doc on this on the log4j site.

Here is the code in the application:

public static void testExists(String myProcedure) {
    final Logger console = LogManager.getLogger(myProcedure);
    final Logger mytestlog = LogManager.getLogger(myProcedure);

.... yadda, yadda, yadda ....
.... and then finally, call the loggers with ....

    console.info("== write this to the console ==");
    mytestlog.info("== write this to the db2 log ==");
}

Here is my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%date{HH:mm:ss.SSS} %-5level{lowerCase=false} %logger{36} | %msg%n"/>
    </Console>
    <JDBC name="jdbc" tableName="vqccommon.sptestlog">
        <ConnectionFactory class="foo.bar.chew.Log4jConnect" method="log4jConnect" />
        <Column name="timestamp" isEventTimestamp="true" />
        <Column name="testclass" pattern="%class" />
... some other columns ....
       </JDBC>
     </Appenders>
     <Loggers>
       <Logger name="sptestlog" level="trace">
           <AppenderRef ref="jdbc" ></AppenderRef>
       </Logger>
       <Root level="trace">
         <AppenderRef ref="Console" ></AppenderRef>
       </Root>
     </Loggers>

   </Configuration>
Foi útil?

Solução

As porfiriopartida mentioned in the comments, I think you are nearly there but you only need to change the way you create the Loggers in your (test) application:

public static void testExists() {
    Logger console = LogManager.getLogger(getClass()); 
    Logger dbLog = LogManager.getLogger("sptestlog"); // match config Logger name!

    console.info("== write this to the console ==");
    dbLog.info("== write this to the db2 log ==");
}

Only loggers created with LogManager.getLogger("sptestlog") will be appending to the JDBC appender you configured. Loggers with any other name will log to the console.

The 2013-10-02 11:31:08,224 DEBUG ServletContext not present - WebLookup not added line is not related to JDBC logging. Log4J does some extra initialization in web containers. If it cannot find a web container it does not need to do this initialization.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top