Example of using DailyRollingFileAppender and RollingFileAppender in Log4j.xml

Posted on by Kim


# Root logger option
# log4j.rootLogger=DEBUG, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.base}/logs/rejseportal.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.logger.as.moes=debug

log4j.logger.org.codehaus.groovy=warn
log4j.logger.org.springframework=warn
log4j.logger.grails=warn
log4j.logger.grails.util.GrailsUtil=warn
log4j.logger.org.hibernate=warn



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="%-4r [%t] %-5p %c %x - %m%n" />
        layout>
    appender>
    <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="${catalina.home}/logs/rejseportal.log" />
        <param name="DatePattern" value="'.'yyyy-MM-dd" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />
        layout>
    appender>
    
    <logger name="as.moes">
        <level value="DEBUG" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="org.codehaus.groovy">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="org.springframework">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="grails.util.GrailsUtil">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="grails">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="org.hibernate">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>
    <logger name="waffle">
        <level value="WARN" />
        <appender-ref ref="FILE" />
    logger>

log4j:configuration>

1 comments:

Gonzalo Oviedo L. says:

This first example use MaxBackupIndex not supported by Daily