Richie
Richie

Reputation: 5199

log4j.xml configuration for debugging only

I am new to log4j and was just wondering if someone could help me with a configuration that I am currently guessing. I'm trying to test it but it doesn't seem to be working.

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

    <appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="/usr/local/apache-tomcat-7.0.35/logs/sample.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p  [%c{1}] %m %n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="levelMax" value="DEBUG" />
        </filter>      
    </appender>

    <logger name="com.webservice" additivity="false">
        <priority value="DEBUG" />
        <appender-ref ref="debugAppender" />
    </logger> 

  <root> 
    <priority value="ERROR" />
    <appender-ref ref="fileAppender" /> 
  </root> 

</log4j:configuration>

What I am trying to do here is set the root logger to ERROR so that it does not pick up anything unless there is an error. But then I have a logger named com.webservice that I would like to have pick up DEBUG statements from my packages(and not other libraries) only for development purposes. When I go to production I want to change the com.webservice priority back to INFO so that it does not pick up any logging.

Have I set this file up correctly?

thanks

Upvotes: 0

Views: 6593

Answers (2)

zatenzu
zatenzu

Reputation: 347

Try to set debug attribut in log4j:configuration node to true.

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

It prints out information as the configuration file is read and used to configure the log4j environment. You may be got more details to resolve your problem.

Upvotes: 3

Vineet Kasat
Vineet Kasat

Reputation: 1014

I havent worked on xml but I use log4j.properties which works perfectly fine. Just place this in your classpath and add jar for log4j. It should work. You can change log levels as you want.

Also there is no need to specify ERROR level as by default ERROR level gets logged.

Sample :

log4j.debug=TRUE

log4j.logger.com.XXX.RemoteCacheManager=FATAL, Logs
log4j.logger.com.XXX.RedisCacheImpl=FATAL, Logs
log4j.logger.com.XXX.utils=DEBUG, TimeItLogs
log4j.logger.com.XXX=DEBUG, Logs

log4j.logger.org.hibernate=INFO, Logs
log4j.logger.org.springframework=INFO, Logs
log4j.logger.org.apache.velocity=ERROR, Logs
log4j.logger.org.apache.commons=ERROR, Logs
log4j.logger.org.apache.tiles=ERROR, Logs

log4j.appender.Logs=org.apache.log4j.RollingFileAppender
log4j.appender.Logs.File=${catalina.base}/logs/XXX.log
log4j.appender.Logs.MaxFileSize=10MB
log4j.appender.Logs.MaxBackupIndex=50
log4j.appender.Logs.layout=org.apache.log4j.PatternLayout
log4j.appender.Logs.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c %m %n

Upvotes: 0

Related Questions