Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging static methods with log4J

Tags:

java

log4j

In each class I have logger that is used for logging:

public class Myclass
{
    public final Logger log = Logger.getLogger(getClass());
}

But what to do when I need to log static methods? log is not static variable?

like image 614
vico Avatar asked Sep 11 '15 10:09

vico


People also ask

Should loggers be static?

Loggers should be declared to be static and final. It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program.

Does logging use log4j?

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


1 Answers

Make your logger static:

public class Myclass
{
    private static final Logger log = Logger.getLogger(Myclass.class);
}
like image 152
Eugene Beschastnov Avatar answered Sep 26 '22 11:09

Eugene Beschastnov