1

I've been using log4j for some time. One thing I've never really understood is why they recommend configuring the logger name from the class. Specifically, I wonder:

  1. What is the difference the three logger declarations?
  2. Which is better and why?
  3. Is there a method that allows copy and paste log declaration without having to type the name of the class somewhere? I'd prefer something that didn't use reflection for performance sake.

For example, consider class MyClass. Three declarations I've run across are:

  • final Logger myLogger = LogManager.getLogger(MyClass.class.getName());

  • final Logger myLogger = LogManager.getLogger(MyClass.class);

  • final Logger myLogger = LogManager.getLogger("MyClass");

I have read the doc, and I see references to the first and second forms on on the log4j site, but I don't understand what the advantage is.

bobanahalf
  • 829
  • 1
  • 11
  • 23

4 Answers4

3

In answer to each of your questions:

  • What is the difference the three logger declarations?

    Basically, in two of the three ways you use the class type, directly or getting the name. The third way is an arbitrary name (error-prone) that can match the with the second way.

  • Which is better and why?

    The second way. It is the shortest and least error-prone.

  • Is there a method that allows copy and paste log declaration without having to type the name of the class somewhere? I'd prefer something that didn't use reflection for performance sake.

    If you use Eclipse, you can use a template. See Create Log4J logger or Simple Log4J eclipse template. Similar facilities exist in other IDE's.

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • So you're saying that specifying the class name is better because the IDE will error check that, where a String is just a String. – bobanahalf Feb 26 '15 at 12:36
3

Using the class name as the logger name is a convention and by now is probably considered to be a "best practice". The reason is very simple - filtering.

In Log4j, the Loggers you configure are matched against the loggers your application obtains. So if you have classes that get loggers named

com.mycorp.package1.Class1
com.mycorp.package1.Class2
com.mycorp.package2.Class1
com.mycorp.package2.Class2

you can configure a Logger for "com.mycorp.package1" at some level and have all those classes routed to one set of appenders while "com.mycorp.package2" can be set to a different logging level and routed somewhere else.

Log4j2 doesn't really care that these are fully qualified class names - anything separated by '.' characters will work.

FYI - LogManager.getLogger() will return you a Logger that uses the name of the calling class. Yes, it uses reflection but if you declare the Logger to be static it will only need to be initialized once.

rgoers
  • 8,696
  • 1
  • 22
  • 24
1

If I have understood point 3: I usually declare a logger in parent classes like this:

final Logger myLogger = LogManager.getLogger(getClass());

so derived classes get their own name in the log, you declare the logger only once, and you are not forced to choose a name in every class.

Matteo Steccolini
  • 1,773
  • 1
  • 15
  • 21
0

it's my understanding that you can call your logger whatever you want. assigning it the class name is a way to ensure the name is unique (if you use the fully qualified name) as well as something meaningful (so you know what you're looking at when parsing out your log). to that end, I'd refrain from using static string names just so you have one less detail to worry about.

snerd
  • 1,238
  • 1
  • 14
  • 28
  • IMHO logging classname is a hangover from before IDEs allowed visually debugging. Logs were used for debugging as code what written, and debug logs left in to facilitate chaging code. IMHO well tested Java code should never have debug logs and should log interesting events warns and errors with filterable categories that don't change when code is refactored. Using shortest uncommon package name for uniqueness still makes sense but FQCN might not be beneficial nowadays, it can be verbose, and tends to get longer with time. – teknopaul Aug 24 '22 at 11:35