Log4j is a Reliable, Fast and Flexible Logging Framework (APIs) written in Java which is distributed under the Apache Software License.
Log4j has three main components:
1. loggers: Responsible for capturing logging information.
2. appenders: Responsible for publishing logging information to various preferred destinations.
3. layouts: Responsible to format logging information in different styles.
Log4j Logging Levels:
1. ALL: All levels including custom levels.
2. DEBUG:
3. ERROR: Designates error events that might still allow the application to continue running.
4. FATAL: Designates very severe error events that will presumably lead the application to abort.
5. INFO:
6. OFF: The highest possible rank and is intended to turn off logging.
7. TRACE:
8. WARN: Designates potentially harmful situations.
ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
Log4j Configuration:
The log4j.properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j.properties in the CLASSPATH.
- The level of the root logger is defined as WARN and attaches appender named X to it.
- Set the appender named X to be a valid appender.
- Set the layout for the appender X
log4j.properties Syntax:
Following is the syntax of log4j.properties file for an appender X:
# Define the root logger with appender X
log4j.rootLogger = WARN, X
# Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender
# Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n
NOTE:
- %m: Used to output the application supplied message associated with the logging event.
- %n: Outputs the platform dependent line separator character or characters.
Simple Program:
public class LogClass {
private static org.apache.log4j.Logger log = Logger
.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
Output:(Note that Log Level is set to WARN)
Warn Message!
Error Message!
Fatal Message!
Log4j has three main components:
1. loggers: Responsible for capturing logging information.
2. appenders: Responsible for publishing logging information to various preferred destinations.
3. layouts: Responsible to format logging information in different styles.
Log4j Logging Levels:
1. ALL: All levels including custom levels.
2. DEBUG:
3. ERROR: Designates error events that might still allow the application to continue running.
4. FATAL: Designates very severe error events that will presumably lead the application to abort.
5. INFO:
6. OFF: The highest possible rank and is intended to turn off logging.
7. TRACE:
8. WARN: Designates potentially harmful situations.
ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
Log4j Configuration:
The log4j.properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j.properties in the CLASSPATH.
- The level of the root logger is defined as WARN and attaches appender named X to it.
- Set the appender named X to be a valid appender.
- Set the layout for the appender X
log4j.properties Syntax:
Following is the syntax of log4j.properties file for an appender X:
# Define the root logger with appender X
log4j.rootLogger = WARN, X
# Set the appender named X to be a File appender
log4j.appender.X=org.apache.log4j.FileAppender
# Define the layout for X appender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.conversionPattern=%m%n
NOTE:
- %m: Used to output the application supplied message associated with the logging event.
- %n: Outputs the platform dependent line separator character or characters.
Simple Program:
public class LogClass {
private static org.apache.log4j.Logger log = Logger
.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}
Output:(Note that Log Level is set to WARN)
Warn Message!
Error Message!
Fatal Message!