Features
- It is thread-safe.
- It is optimized for speed.
- It is based on a named logger hierarchy.
- It supports multiple output appenders per logger.
- It supports internationalization.
- It is not restricted to a predefined set of facilities.
- Logging behavior can be set at runtime using a configuration file.
- It is designed to handle Java Exceptions from the start.
- It uses multiple levels, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL.
- The format of the log output can be easily changed by extending the Layout class.
- The target of the log output as well as the writing strategy can be altered by implementations of the Appender interface.
- It is fail-stop. However, although it certainly strives to ensure delivery, log4j does not guarantee that each log statement will be delivered to its destination.
Architecture
Applications using the Log4j 2 API will request a Logger with a specific name from the LogManager. The LogManager will locate the appropriate LoggerContext and then obtain the Logger from it. If the Logger must be created it will be associated with the LoggerConfig that contains either a) the same name as the Logger, b) the name of a parent package, or c) the root LoggerConfig. LoggerConfig objects are created from Logger declarations in the configuration. The LoggerConfig is associated with the Appenders that actually deliver the LogEvents.
Configuration
The log4j.properties file is a log4j configuration file that keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j.properties in the CLASSPATH.
Simple Usage
Hello World
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HelloWorld { private static final Logger logger = LogManager.getLogger("HelloWorld"); public static void main(String[] args) { logger.info("Hello, World!"); } }
log4j.properties Syntax:
# Define the root logger with appender X log4j.rootLogger = DEBUG, 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
log4j.properties Example
Using the above syntax, we define the following in log4j.properties file:
-
The level of the root logger is defined as DEBUG, The DEBUG appender named FILE to it.
-
The appender FILE is defined as org.apache.log4j.FileAppender. It writes to a file named log.out located in the log directory.
-
The layout pattern defined is %m%n, which means the printed logging message will be followed by a newline character.
# Define the root logger with appender filelog4j.rootLogger = DEBUG, FILE# Define the file appenderlog4j.appender.FILE=org.apache.log4j.FileAppenderlog4j.appender.FILE.File=
#123;log}/log.out# Define the layout for file appenderlog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n
More info
Log4j can save logfile into a file or DB or just print it out.
For later reference, you can check this Quick Guide
Reference
log4j Tutorial | pretty useful website for self-learner
Apache Log4j 2 Offical Site