I have the following code:
private static final Log LOGGER = LogFactory.getLog(MyClass.class);
if (LOGGER.isErrorEnabled()) {
LOGGER.error("ID_1: Log Message: " + parameter));
}
And I want the severity to be set to WARN or FATAL.
If I am able to update the log4j.properties during run-time, then how would I change the severity of this message? Using log4j.logger.com.foo=WARN will only change the severity for an entire package.
Is there a way to be more specific?
Can we change the logging level of a single logging statement during run-time?
The short answer is No you can’t.
To do what you want to do, you would need to change the code to call the Logger method that takes a Level as an argument, and replace the guard; e.g.
And I don’t think this is going to help because you then have the problem of getting the right value into that
levelvariable.A better alternative would be to create a logger with a unique name, and only use in that particular statement. Then you can control logging of that statement (via the logging configs) independently of any other statements.
Or, more realistically, use class names or package names to name your loggers. That gives you more granularity than a single global logger … without creating an insane number of Logger instances.