Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I set the log4j conversionpattern at runtime

Tags:

java

log4j

I have a Java app while parses input from a spreadsheet. I've added the ability to run just the parser part standalone from an ant task. However the normal log4j pattern I'm using can make the output hard to read, I'd like to set a simple pattern at runtime.

So something like log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n

        parseDebug = new Boolean(System.getProperty("ptpunit.parseDebug")).booleanValue();

        if(parseDebug){

            // SET CONVERSION PATTERN HERE

            log.setLevel((Level) Level.DEBUG);

        }
like image 828
Wiretap Avatar asked Sep 10 '09 07:09

Wiretap


1 Answers

I think you could do something like this:

ConsoleAppender a = (ConsoleAppender) Logger.getRootLogger().getAppender("stdout");
a.setLayout(new PatternLayout("%d{HH:mm:ss}  %-5.5p  %t %m%n"));

Of course you have to change the Appender type according to the one you are using and you may have to replace "Logger.getRootLogger()" by the a call to fetch the logger your appender is actually attached to.

like image 58
Wolfgang Avatar answered Oct 21 '22 11:10

Wolfgang