Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change global setting for Logger instances

Tags:

java

logging

I'm using java.util.logging.Logger as the logging engine for my application. Each class uses it's own logger, i.e., each class has:

private final Logger logger = Logger.getLogger(this.getClass().getName()); 

I want to set a logging level for all my classes, and be able to change it (i.e., have the setting in one place). Is there a way to do this, other that using a global Level variable and manually set each logger to it?

like image 804
Amir Rachum Avatar asked Jun 10 '11 14:06

Amir Rachum


People also ask

What is global logger in Java?

Normally, when enabling logging in an application, you define more granular loggers, usually per Java packages or classes. If you do not want to do that, you can use this global logger, which will handle all logging statements, no matter the library, package or class they are contained in.

Why logger is printing twice in Java?

In some cases, logging output is executed twice to a log destination. This is a side effect of the Log4j additivity which is active for each logger by default.


1 Answers

One easy way is to use a logging properties file, by including this VM argument:

-Djava.util.logging.config.file="logging.properties"  

where "logging.properties" is the path to a file containing logging configuration. For relative paths, the working directory of the process is significant.

In that file, include a line like this:

.level= INFO 

This sets the global level, which can be overridden for specific handlers and loggers. For example, a specific logger's level can be overridden like this:

 com.xyz.foo.level = SEVERE 

You can get a template for a logging properties file from jre6\lib\logging.properties.

like image 53
Andy Thomas Avatar answered Sep 22 '22 02:09

Andy Thomas