Why does log4j Logger.getLogger () need to pass a class type?

I read several articles on how to use log4j. Most of them give the code below:

Logger logger = Logger.getLogger("com.foo.Bar"); 

or

  Logger logger = Logger.getLogger(XXX.class); 

This will initialize the logger object. But my question is: why send the class type as a parameter? It seems when I use the registrar, I don't care what class I use it in. Thus, the type of the class does not affect the registrar. If I declare the registrar static and public, I can call this registrar in another class. So what is the author’s intention to create it like that? Will the class type bind something when I use the registrar? Or I can send any type of class to the getLogger function.

+53
java log4j
Jan 30 '13 at 4:27
source share
7 answers
  • You can always use any string as the log name, except for the class type. This is definitely normal.

  • The reason many people use class type, I think:

    • Easy to use. You don’t have to worry about duplicating log names in a complex Java EE application. If other users also use your registrar name, you may have a log file that includes not only the output of your class, but

    • It is easy to check the logging class, as the log name will be displayed in the log file. You can quickly jump to a specific class;

    • When you distribute your class, people may want to redirect logging from your class to a specific file or to another location. In this case, if you use a special registrar name, we may need to check the source code or make it impossible if souce is not available.

+53
Jan 30 '13 at 5:08
source share

From javadoc : Logger.getLogger(Class) is short for getLogger(clazz.getName()) . The convention used with log4j and other logging frameworks is to define a static logger for each class. For example,

 public class SomeClass { private static final Logger LOG = Logger.getLogger(SomeClass.class); ... } 

I found this convention to work well for organizing log output. This, of course, is not required, but is a good practice.

+8
Jan 30 '13 at 4:35
source share

1: you can use "class name" or "string name" when you define in log4j.properties before, such as

 log4j.logger.anything=INFO,anything 

so you can write your journal as

  Logger logger = Logger.getLogger("anything"); 

2: If you define some kind of log name, you can easily check it, cus they are separate.

+6
Oct. 14 '13 at 3:25
source share

XXX.class is the name of your registrar to flag subsequent log statements. To give you an idea of ​​which class certain log entries belong to, belongs to / from.

0
Jan 30 '13 at 4:32
source share

A logger with a class name is optional, you can use your own message. Consent to use:

 Logger logger = Logger.getLogger(XXX.class) 

and useful for debugging. It will record which line of code is executed.

0
Dec 27 '17 at 22:41
source share

This may depend on the particular log library, but usually it is more than a name. This means the hierarchy of the registrar. When you set up your registrar, you usually pass in both the log level and the registrar name, for example:

 <logger name="org.hibernate" level="ALL"/> 

This attribute is not just a name, but an attribute means hierarchy. And if instead you would write something like:

 <logger name="org" level="ALL"/> 

This will affect not only sleep mode, but also everything that is in the org package. On the other hand, if you write something like:

<logger name="org.hibernate.validator" level="ALL"/>

this only applies to the hibernate authentication package and not the rest of hibernate.

By the way, if you do not want to specify a specific class for each factory, you can use something like (Kotlin):

 val logger: Logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()) 
0
Feb 07 '19 at 10:04
source share

You can track your journal by class type.

example1:

 public class Bar { Logger logger = Logger.getLogger("com.foo.Bar"); ... logger.debug("debug message"); } 

Perhaps you can see the log message below.

 DEBUG: **com.foo.Bar** debug message 

example2:

 public class Foo { Logger logger = Logger.getLogger("com.foo.Foo"); ... logger.debug("debug message"); } 

Perhaps you can see the log message below.

 DEBUG: **com.foo.Foo** debug message 

If you have a lot of java and logger class messages, it is too difficult to find where the log messages are.

-one
Jan 30 '13 at 4:46
source share



All Articles