views:

632

answers:

4

Hi, I am using JBOSS 4.2.2 server to deploy multiple web applications. Each application uses Hibernate and for each application there are saperate log files and saparate appendar. Now for Hibernate logging statements of one application should go in log file of that particular application. Does anybody has idea how to configure log4j.xml file to achieve this?

Thanks and regards, Milind

A: 

If you have multiple applications each running hibernate just create an appender for hibernate in the application log4j.xml file with the classpath of the hibernate classes i.e org.hibernate and this should direct the output to the specified log file.

If that does not work check that hibernate is not picking up a log4j properties file that has been bundled into any of your library jars. If hibernate fails to find the log4j.xml file then you should get a warning message in system.out

karl

Karl
I guess the question is how to split org.hibernate log messages between different log files for each web application.
Peter Hilton
A: 

I am not 100% sure if I understand your problem correctly. I assume you want to direct output from the same logger to different files depending on the application that issues the log statement.

Log4J provides a Mapped Diagnostic context. This context can be set to a different value (depending on your application) before the hibernate code runs. Each log-statement issued by hibernate then also contains the contents of the MDC.

With that you can write a special appender that inspects the contents of the MDC and writes the log message to different files.

I have used this approach in a heavy multi-threaded application where every thread produces its own log-file and it works very well.

Wolfgang
A: 
  1. Adding an NDC. The logs still go to the same file, but the file will be lot easier to grep.
  2. Change the JBoss classloading such that each application can have its own log4j.xml
binil
+1  A: 

In JBoss you can use the TCL filter as described here. Just add the filter to the part of the configuration you want to manipulate.

Example (slightly adapted from above source, look at section Deploy Application As .war File):

<appender name="App1Log" class="org.apache.log4j.FileAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler" />
    <param name="Append" value="false"/>
    <param name="File" value="${jboss.server.log.dir}/app1.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%8.8t][%20.20c] %m%n"/>
    </layout>
    <filter class="org.jboss.logging.filter.TCLFilter">
        <param name="AcceptOnMatch" value="true"/>
        <param name="DeployURL" value="app1-exp.war"/>
    </filter>
</appender>
Kariem