Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Maven has two places for configuration files that need to be deployed to the classpath. You need to place a logback configuration file in both.

The main logback configuration file is named logback.xml and should be in src/main/resources of your maven project.

<configuration>
    <!--
        WARN or higher goes to stdout (catalina.out)
    -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} intranet-templating:%X{userId}|%-5level-%msg %class.%method:%line %xException{full} %n</Pattern>
        </encoder>
    </appender>
    <!--
        INFO or higher goes to the log file that is in the tomcat logs directory.
        The files are rolled daily to the tomcat/logs/intranet-templating. 
    -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/opt/vignette/software/tomcat/logs/intranet-templating.log</file>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <if condition='property("HOSTNAME").contains("309")'>
                <then>
                    <level>DEBUG</level>
                </then>
                <else>
                    <level>INFO</level>
                </else>
            </if>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/opt/vignette/software/tomcat/logs/intranet-templating/intranet-templating.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss}|%X{userId}|%-5level-%msg %class.%method:%line %xException{full} %n</Pattern>
        </encoder>
    </appender>

    <logger name="org.hibernate" level="WARN"/>
    <!--<logger name="org.springframework" level="INFO"/>-->
    <logger name="org.apache.commons" level="INFO"/>
    <logger name="org.apache.velocity" level="INFO"/>
    <logger name="org.displaytag" level="INFO"/>
    <logger name="org.apache.axis" level="INFO"/>
    <root level="DEBUG">
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="NORRIS-EMAIL"/>
    </root>
</configuration>

The test logback configuration file is named logback-test.xml and should be in src/test/resources of your maven project. If logback sees both files (e.g. test build), then the -test file will be the logback configurations.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>%d{yyyy-MM-dd HH:mm:ss} %X{userId} %-5level - %msg %class.%method:%line %xException{full} %n </Pattern>
        </encoder>
    </appender>

    <!--default to logging info-->
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

December 6th, 2010

Posted In: logback, Maven

Leave a Reply

Your email address will not be published. Required fields are marked *

WP to LinkedIn Auto Publish Powered By : XYZScripts.com