Sam
Sam

Reputation: 1328

How to set log level in log4j from properties file in runtime

How do I set log level in log4j.xml using properties file in runtime time?

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

<logger name="com.mypackage" additivity="false">
    <level value="${level}" />
    <appender-ref ref="ConsoleAppender" />
</logger>

I set the value of $level from my vm arguments by passing the value as -Dlevel="warn" so that all messages with level warn and above got logged.

Now my problem is I have to set the value of log level from a properties file instead of vm arguments ? How can I do this?

P.S : I am using common.logging in the java code with log4j as a configuration mechanism.

Upvotes: 1

Views: 18870

Answers (3)

Dheeraj Kumar
Dheeraj Kumar

Reputation: 1

You can set log level in System.setProperty by reading from your dynamic properties file and it will be reflected in log4j.

Cheers!

Upvotes: 0

tcb
tcb

Reputation: 2824

Here's the way to change log level dynamically:

LogManager.getRootLogger().setLevel(Level.DEBUG);

To use a property from custom file use Properties.loadFromXML to read a file with properties, then determine a level using these properties.

Upvotes: 3

Neeraj
Neeraj

Reputation: 8532

Sam,

I am not sure what you are asking for is the default behaviour for log4j. What you can do is, add properties like log4j.rootLogger=DEBUG in your application properties file and point log4j to use your application properties file as the log4j properties file.

You can force log4j to take its configuration file by specifying the following vm argument:

java -Dlog4j.configuration=resources/log4j_dev.properties

OR

java -Dlog4j.configuration=file:/resources/log4j_dev.properties

An example of log4j properties file is as follows:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

You can also use xml format for the properties file.

If you want to add loggers for log4j programmatically (on rumtime), this is what you would need to do:

 ConsoleAppender console = new ConsoleAppender(); //create appender
  //configure the appender
  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
  console.setLayout(new PatternLayout(PATTERN)); 
  console.setThreshold(Level.FATAL);
  console.activateOptions();
  //add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(console);

  FileAppender fa = new FileAppender();
  fa.setName("FileLogger");
  fa.setFile("mylog.log");
  fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
  fa.setThreshold(Level.DEBUG);
  fa.setAppend(true);
  fa.activateOptions();

  /add appender to any Logger (here is root)
  Logger.getRootLogger().addAppender(fa)
  //repeat with all other desired appenders

Hope this helps.

Upvotes: 2

Related Questions