A simple example of log4j
This Example shows you how to create a log in a program.Description of the code:
-
Logger.getLogger(): Logger class is used for handling the majority of log operations and getLogger method is used for return a logger according to the value of the parameter. If the logger already exists, then the existing instance will be returned. If the logger
is not already exist, then create a new instance.
-
Logger.debug(): This method is used to check that the specified category
is DEBUG enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class
ObjectRenderer.
-
Logger.info(): This method is used to check that the specified category
is INFO enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class
ObjectRenderer.
-
Logger.warn(): This method is used to check that the specified category
is WARN enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class
ObjectRenderer.
-
Logger.error(): This method is used to check that the specified category
is ERROR enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class
ObjectRenderer.
- Logger.fatal(): This method is used to check that the specified category is FATAL enabled or not, if yes then it converts the massage passed as a string argument to a string by using appropriate object renderer of class ObjectRenderer.
LogExample.java
|
log4j.properties
|
Output:
[DEBUG] 55:17 (LogExample.java:main:11)
|
Download code