Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can we change the logging level of log4j at runtime

i have an issue, i want to change the logging level of log4j at runtime, i have tried many things with log4j.properties file, i have also tried to written a code which after particular time again reads the properties file and again configure the logger.

but the problem is, i want to change the logging level to DEBUG for one API call, and then when that call is completed, the logger should again change to the previous value..

please help..

like image 575
M.J. Avatar asked Dec 11 '10 06:12

M.J.


People also ask

Can we change log level at runtime?

For each service that you have running in BMC Atrium Discovery, you can dynamically change the log level without having to restart the appliance.

How do you change the logger level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)


2 Answers

Calling the Logger.setLevel method with the desired Level can alter a Logger's output level at runtime.

The following is an example which demonstrates its usage:

Logger logger = Logger.getLogger("myLogger");
logger.addAppender(new ConsoleAppender(new SimpleLayout()));

System.out.println("*** The current level will be INFO");

logger.setLevel(Level.INFO);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

System.out.println("*** Changing level to DEBUG");

// remember the previous level
Level previousLevel = logger.getLevel();

logger.setLevel(Level.DEBUG);
logger.warn("DEBUG and higher will appear");
logger.info("DEBUG and higher will appear");
logger.debug("DEBUG and higher will appear");

System.out.println("*** Changing level back to previous level");

// revert to previous level
logger.setLevel(previousLevel);
logger.warn("Only INFO and higher will appear");
logger.info("Only INFO and higher will appear");
logger.debug("Only INFO and higher will appear");

The above outputs:

*** The current level will be INFO
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear
*** Changing level to DEBUG
WARN - DEBUG and higher will appear
INFO - DEBUG and higher will appear
DEBUG - DEBUG and higher will appear
*** Changing level back to previous level
WARN - Only INFO and higher will appear
INFO - Only INFO and higher will appear

The above demonstrates how to change the level of one Logger named myLogger, but if the levels of all the loggers in the current repository should be changed, then the setLevel method on the root logger obtained by Logger.getRootLogger should be called to change the levels on all the child loggers.

like image 117
coobird Avatar answered Oct 19 '22 12:10

coobird


The log level of a logger can be changed by calling setLevel as described by @coobird. However, there is a catch!

When you call getLogger(name), the logging library will return you an existing Logger object if possible. If two or more threads request a logger with the same name, they will get the same object. If one of the threads calls setLevel, this will change the logger level for all of the others. That can lead to unexpected behavior.

If you really need to do this kind of thing, a better approach would be to create a logger with a different name for the case where you want to logging at a different level.

However, I'm not convinced of the wisdom of the application calling setLevel at all. The setLevel method is about filtering the log messages, and you should not be wresting control of logging filtering away from the user / deployer.

like image 25
Stephen C Avatar answered Oct 19 '22 12:10

Stephen C