0

I was looking into log strategies and I found this article, talking about the dangers of static logger references.

The official log4j2 documentation shows the static logger pattern on it's examples, so this got me really intrigued about it:

// Define a static logger variable so that it references the
// Logger instance named "MyApp".
private static final Logger logger = LogManager.getLogger(MyApp.class);

I am using log4j2and I wanted to know if this is still a problem, since the article was written back in 2009.

user207421
  • 305,947
  • 44
  • 307
  • 483
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • It's not a problem if the class containing this statement is deployed along with your webapp. It's only a problem if you share it by deploying it e.g. in a library in Tomcat's `lib` directory. – user207421 Aug 18 '17 at 01:22
  • 1
    The article doesn’t seem to be about leaks at all. In fact, the word “leak” doesn’t occur on that page. It’s addressing the ability of different applications all deployed to the same server to have their loggers independently configured. – VGR Aug 18 '17 at 01:23
  • @VGR yeah, sorry, I got it from here: https://stackoverflow.com/a/5173816/1227166 – Michel Feinstein Aug 18 '17 at 03:55
  • piepera states that a class with a static field containing a Logger instance cannot be garbage collected. I doubt this is true; if having a reference to something which other classes have a reference to, prevented an object from being garbage collected, there’d be a pretty serious problem with garbage collection overall. Also, I’m nearly certain that java.util.logging does not retain references to Loggers; I don’t know about Log4J or SLF4J. – VGR Aug 18 '17 at 15:50

0 Answers0