Reputation: 3650
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.
Upvotes: 69
Views: 88768
Reputation: 51
Try to use getName()
method.
private static final Logger log = LogManager.getLogger(JwtAuthenticationFilter.class.getName());
Upvotes: 0
Reputation: 9685
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())
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 71
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.
Upvotes: 7
Reputation: 3202
You can always use any string as logger name other than class type. It's definitely ok.
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.
Upvotes: 64
Reputation: 136
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.
Upvotes: 1
Reputation: 5962
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.
Upvotes: 9
Reputation: 15758
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.
Upvotes: 0