Why log4j's Logger.getLogger() need pass a Class type?

JavaLog4j

Java Problem Overview


I read some articles about how to use log4j. Most of them give below code as a beginning:

 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 need send the class type as a parameter? It seems when I use the logger, I don't care in which class I use it.So the Class type seems no effect to logger. If I declare a logger as static and public, I can call this logger at another class, So what's the intention of the author to design it like this? Will the Class type bind something when I use the logger? Or I can send any Class types to the getLogger function.

Java Solutions


Solution 1 - Java

  1. You can always use any string as logger name other than class type. It's definitely ok.

  2. The reason why many people use class type, I guess:

  • Easy to use. You don't need to worry about logger name duplication in a complex Java EE application. If other people also use your logger name, you may have a log file including no only the output of your class;

  • Easy to check the logging class, as the logger name will show in the log file. You can quickly navigate to the specific class;

  • When you distribute you class, people may want to redirect the logging from your class to a specific file or somewhere else. In such case, if you use a special logger name, we may need to check the source code or imposssible to do that if souce is unavailable.

Solution 2 - Java

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

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

I have found this convention to work well for organizing logging output. It's certainly not required but is a useful practice.

Solution 3 - Java

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 record your log as

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

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

Solution 4 - Java

You can trace your log by class type.

example1:

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

Maybe you can see below a log message.

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

example2:

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

Maybe you can see below a log message.

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

If you have a lot of java class and logger message, It's too difficult to find where log messages are from.

Solution 5 - Java

It can depend on a specific logging library, but usually it's more than a name. It signifies logger's hierarchy. When you configure your logger, you would usually pass both Log Level and Logger name, like this:

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

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

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

not only hibernate, but everything that is in org package will be impacted. On the other hand, if you write something like:

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

it will only concern hibernate validation package, and not the rest of Hibernate.

By the way, if you don't want to specify a concrete class for every factory, you can use something like(Kotlin):

val logger: Logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass())

Solution 6 - Java

XXX.class is to Name your Logger i.e to flag subsequent log statements . To give you an idea which class certain log statements belongs to / originated from.

Solution 7 - Java

Logger with class name is not mandatory, you can use your own message. It is convention to use:

Logger logger = Logger.getLogger(XXX.class) 

and is useful to debug. It will log which line of code is executed.

Solution 8 - Java

Try to use getName() method.

private static final Logger log = LogManager.getLogger(JwtAuthenticationFilter.class.getName());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionroast_soulView Question on Stackoverflow
Solution 1 - JavaJintian DENGView Answer on Stackoverflow
Solution 2 - JavaJohn McCarthyView Answer on Stackoverflow
Solution 3 - JavaLiubeyView Answer on Stackoverflow
Solution 4 - JavaOh Jong AmView Answer on Stackoverflow
Solution 5 - JavayuranosView Answer on Stackoverflow
Solution 6 - JavaTheWhiteRabbitView Answer on Stackoverflow
Solution 7 - JavaFarhadView Answer on Stackoverflow
Solution 8 - JavaSiddharth SonwaneView Answer on Stackoverflow