john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

log4j dynamic configuration

package net.kittyandbear;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.leapfile.common.OxygenLogger;
import com.leapfile.common.TestLoggingUtil;


public class ExampleLogging {

    private static final int FALSE_IN_DB = 0;
    private static final int TRUE_IN_DB = 1;

    private static final Logger logger = Logger.getLogger( ExampleLogging.class.getName() );

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        TestLoggingUtil.configureLogging( ExampleLogging.class );

    }


    @Before
    public void setUp() throws Exception {
        logger.debug( "setup");
    }


    @Test
    public void test_Example() throws Exception {
        logger.debug( "hi");
    }

public class TestLoggingUtil {

    private static final String ROOT_LOGGER_ALIAS = "R";

    public static void configureLogging( Class<?>... loggedClasses ) {
        Properties props = new Properties();
        props.put( "log4j.rootLogger", Level.INFO.toString() + ", " + ROOT_LOGGER_ALIAS );
        props.put( "log4j.appender." + ROOT_LOGGER_ALIAS, ConsoleAppender.class.getName() );
        props.put( "log4j.appender." + ROOT_LOGGER_ALIAS + ".encoding", "UTF-8" );
        props.put( "log4j.appender." + ROOT_LOGGER_ALIAS + ".layout", PatternLayout.class.getName() );
        props.put( "log4j.appender." + ROOT_LOGGER_ALIAS + ".layout.ConversionPattern", "%5p [%-10.10t] (%F:%L) - %m%n" );
        for( Class<?> clazz : loggedClasses ) {
            props.put( "log4j.logger." + clazz.getName(), Level.DEBUG.toString() );
        }
        PropertyConfigurator.configure( props );
    }
}


} // end class

  • « junit easymock
  • junit category slow test exclusion »

Published

Jan 10, 2013

Category

java

~118 words

Tags

  • configuration 8
  • dynamic 3
  • java 252
  • log4j 3