bobanahalf
bobanahalf

Reputation: 879

log4j - Configuring logger name

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:

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.

Upvotes: 2

Views: 4525

Answers (4)

rgoers
rgoers

Reputation: 9141

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.

Upvotes: 3

Paul Vargas
Paul Vargas

Reputation: 42020

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.

Upvotes: 3

Matteo Steccolini
Matteo Steccolini

Reputation: 1845

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.

Upvotes: 1

snerd
snerd

Reputation: 1297

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.

Upvotes: 0

Related Questions