Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

This is such a common problem. You want to have one file that you deploy everywhere, but you want it to behave differently based upon where you deploy it. An example is you want logback to log debug in dev, but info in production. This is easy to do with the conditional processing. Check out logback conditional processing for other options.

<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"/>
    </root>
</configuration>

December 6th, 2010

Posted In: logback

Leave a Reply

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

WP to LinkedIn Auto Publish Powered By : XYZScripts.com