-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Multiple loggers
PostPosted: Wed Apr 14, 2010 6:08 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
At the moment I'm using the logger like this:
Code:
public static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("net.sf.hibernate");
logger.error("test");
This way I use the same logger as Hibernate.
My logs and the logs from Hibernate are mixed into 1 file.

Can I split this? So log files are created for the hibernate logs and different files are created for my logs.

My log4j.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%5p - (%F:%L) - %m%n" />
      </layout>
      <filter class="org.apache.log4j.varia.LevelRangeFilter">
         <param name="LevelMin" value="WARN" />
      </filter>
   </appender>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="file" value="logs/log4j.log" />
      <param name="MaxFileSize" value="5MB" />
      <param name="MaxBackupIndex" value="10" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%5p - (%F:%L) - %m%n" />
      </layout>
   </appender>

   <root>
      <priority value="ALL" />
      <appender-ref ref="console" />
      <appender-ref ref="fileAppender" />
   </root>
</log4j:configuration>


Top
 Profile  
 
 Post subject: Re: Multiple loggers
PostPosted: Wed Apr 14, 2010 6:34 am 
Newbie

Joined: Tue Apr 06, 2010 8:46 am
Posts: 7
You have to define two different appenders, one for each file, and also separate your loggers as to each one direct its output to the proper appender.
In your code then you pick the correct logger as:

Code:
private Logger logger = LoggerFactory.getLogger(CurrentClass.class);


I 've switched to logback so the following sample is only to give you the hint:
Code:
<appender name="hbFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>${logfilesLocation}/myapp-hibernate.log</File>
  .......
</appender>

<appender name="myAppFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>${logfilesLocation}/myapp-application.log</File>
  ......
</appender>

<logger name="org.hibernate" level="error">
    <appender-ref ref="hbFile"/>
</logger>

<logger name="my.app.base.package" level="error">
    <appender-ref ref="myAppFile"/>
  </logger>


Top
 Profile  
 
 Post subject: Re: Multiple loggers
PostPosted: Thu Apr 15, 2010 3:31 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
It's working like I want to, but logger.trace() isn't working.

When I do logger.trace(), it's doesn't get in my log file. If I use any higher level it does work. So logger.debug() works.

I really don't understand why, does anyone has an idea?

My log4j.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%5p - (%F:%L) - %m%n" />
      </layout>
      <filter class="org.apache.log4j.varia.LevelRangeFilter">
         <param name="LevelMin" value="WARN" />
      </filter>
   </appender>

   <appender name="hibernateFileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="file" value="logs/log4jHibernate.log" />
      <param name="MaxFileSize" value="5MB" />
      <param name="MaxBackupIndex" value="10" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%5p - (%F:%L) - %m%n" />
      </layout>
   </appender>

   <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="file" value="logs/log4j.log" />
      <param name="MaxFileSize" value="5MB" />
      <param name="MaxBackupIndex" value="10" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%5p - (%F:%L) - %m%n" />
      </layout>
   </appender>

   <logger name="org.hibernate">
      <appender-ref ref="console" />
      <appender-ref ref="hibernateFileAppender" />
   </logger>

   <logger name="util.Logger">
      <appender-ref ref="console" />
      <appender-ref ref="fileAppender" />
   </logger>
</log4j:configuration>


Top
 Profile  
 
 Post subject: Re: Multiple loggers
PostPosted: Thu Apr 15, 2010 4:58 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
this is an issue easily solved by reading the manual of log4j, has nothing to do with hibernate:

Quote:
<category name="org.hibernate">
<priority value="TRACE"/>
</category>


and make sure you don't set a global filter like you did, which is removing all log statements below WARN:
Quote:
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
</filter>


to control levels, it's recommended to use categories

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.