This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes How to change log level in log4j at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to change log level in log4j at runtime" Watch "How to change log level in log4j at runtime" New topic

How to change log level in log4j at runtime

Saagar Kappa

Joined: Nov 10, 2005
Posts: 23

I have a small requirement where the default log level is info, but if the user wants to invoke the Java program with debug level, the Debug should start off. I am trying to achieve this using this code..

When I do this, the debug statement at the end does not print.
Then I thought may be the root logger level is not propagated after the initialization at start up, so I tried this

The info statement prints "this is info" then the next info statement prints "log level is DEBUG" the next statement prints true but the intermediate debug statement does not print.

I have done this before in a Web app and it works just fine. I do not understand what is going on. Can someone please help me understand this.

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, OCA (DBA)<br /> <br />Winners dont do different things. They do things differently...
Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
Hi Saagar, would you know if there are any other loggers (other than root) which may be conflicting with your setup?

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441


Where (i.e. in which "appender") does it not print? On the CONSOLE or in some file? Check the value of "Threshold" attribute for the appender in which you expect this message to be logged. Maybe the Threshold value is set to INFO, because of which the debug statements are ignored in that appender.

[My Blog] [JavaRanch Journal]
Saagar Kappa

Joined: Nov 10, 2005
Posts: 23
@Paul: I dont have custom settings for each logger based on package or class structure, so the root logger settings take effect in each, but when I update the root logger that doesn't get updated in the current loggers.

@Jaikiran: The idea is to give the user the ability to debug at command line before launching the program. The threshold is info but I want to be able to selectively change it to debug at runtime..
I agree. Here's the link:
subject: How to change log level in log4j at runtime
It's not a secret anymore!