Log4j code snippets

A few log4j-related code snippets that I tend to re-use from time to time… All pretty self-explanatory.

There is a faq at http://logging.apache.org/log4j/ which already touches on the subjects below,
but in a fairly light manner, and without the support of any code.


1- how to change the log level dynamically



        Level debugLogLevel = Level.toLevel("DEBUG");
        Logger.getLogger(MyClass.class).setLevel(debugLogLevel);


2- add an appender at runtime

ConsoleAppender appender = new ConsoleAppender( new PatternLayout("%-5p [%t]: %m%n"));
Logger.getRootLogger().addAppender(appender);


3- how to reload the log4j configuration file at runtime

String configDir ="/path/to/config/directory";
LogManager.resetConfiguration();
String log4jConfigFile = configDir + java.io.File.separator +"log4j.xml";
DOMConfigurator.configure(log4jConfigFile);
logger.info("log4j initialized from " + log4jConfigFile);



4- how to direct the log output to different files.

In the configuration below the log statements originating from the “com.firstpackage” package
will be directed to FirstFile.log, and the also to the console.
Log statements from “com.secondpackage” will go to SecondFile.log, and to the console.


 <appender name="FIRST_FILE" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="FirstFile.log"/>
       <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] %-5p %c{1}: %m%n"/>
       </layout>
    </appender>

 <appender name="SECOND_FILE" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="SecondFile.log"/>
       <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] %-5p %c{1}: %m%n"/>
       </layout>
    </appender>

 <appender name="CONSOLE_APPENDER" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} [%t] %-5p %c{1}: %m%n"/>
        </layout>
    </appender>

<appender name="ASYNC_APPENDER_1" class="org.apache.log4j.AsyncAppender">
        <appender-ref ref="CONSOLE_APPENDER"/>
        <appender-ref ref="FIRST_FILE"/>
    </appender>
   
    <appender name="ASYNC_APPENDER_2" class="org.apache.log4j.AsyncAppender">
        <appender-ref ref="CONSOLE_APPENDER"/>
        <appender-ref ref="SECOND_FILE"/>
    </appender>

 <logger name="com.firstpackage" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="ASYNC_APPENDER_1" />       
    </logger>
   
    <logger name="com.secondpackage" additivity="false">
        <level value="DEBUG" />
        <appender-ref ref="ASYNC_APPENDER_2" />       
    </logger>


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s