Java / log4j
Apache commons logging is a facade API that abstracts the underlying implementation. Using commons logging, one can decide between different implementations such as log4j, slf4j and java util Logging without touching the code.
log4j is a logging framework that provides the actual implementation.
Yes. The logging can be written to a database, file, console, JMS and Apache flume.
log4j framework is a reliable, fast and light weigtht logging API. It is written using Java and distributed under Apache software license.
It is highly configurable using external configuration files at runtime. It supports log direct to various destinations including databases, file, console, Unix syslog and many more.
There are mainly 3 components.
Logger captures logging information.
Appender publishes logging information to various destinations.
Layout formats the logging data in different configured styles to support readability.
- Thread safe.
- Optimized for performance.
- Supports more than one appender per logger.
- Extensible.
- Logging behavior is specified in a configuration file loaded during runtime.
- Supports different logging levels such as WARN, INFO, and ERROR.
- The logging format can be easily changed my altering the Layout class or by extending it.
- The destination and the log writing strategy can be altered by implementing Appender interface.
There are 8 levels. You can also define your custom levels by sub-classing the Level class.
- ALL,
- DEBUG,
- INFO,
- WARN,
- ERROR,
- FATAL,
- TRACE,
- and OFF.
Appender is used to deliver LogEvents to its destination. For example, it is used to write the logs in the file.
The DEBUG level designates fine-grained informational events useful to debug an application.
The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
The WARN level designates potentially harmful situations.
The ERROR level designates error events that might still allow the application to continue running.
The TRACE Level designates finer-grained informational events than the DEBUG.
FATAL level designates very severe error events that will presumably lead the application to abort.
The ALL Level has the lowest possible rank and is intended to turn on all logging.
The OFF Level has the highest possible rank and is intended to turn off logging.
The format characters used in log4j are,
L- it is used to output the line number from where the logging request was processed or issued.
m- It is used to output the application supplied message related to the logging event.
p- It is used to output the priority of the logging event.
C- It is used to output the class name of the caller issuing the logging request.
Yes, Log4j is a thread-safe; its components are built to be used in multithreaded systems.
The log4j API provides the object org.apache.log4j.jdbc. JDBCAppender object that can put logging information in a database.
There are many types of appenders that include,
- ConsoleAppender logs to standard output,
- FileAppender writes logs to some file,
- Rolling file appender appends to a file with a maximum size.
An appender uses Layout to format the log before writing to the destination. The layout allows formatting logging information in various patterns and styles.
A log request of level n in a logger with level m is enabled if m >= n.
For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.
ImmediateFlush property enables the output stream to the file being flushed with each append operation.
By default the property is set to true.
To switch off immediateFlush use the below configuration. When set to false, it may improve performance.
log4j.appender.FILE.ImmediateFlush=false
Log4j has following advantages over standard logging API:
- robust logging.
- log4j has more features available than standard logging API.
- configuring and using log4j is easier.
- log4j also has a much more robust log formatting.
- more handlers are available for log4j.
The Simple Logging Facade for Java (SLF4J) is a simple facade or abstraction for various logging frameworks such as java.util.logging, LogBack, log4j allowing the end user to plug in the desired logging framework at deployment time. It is not itself a logging library, but a generic interface to one of many logging libraries.
Diagnostic logging are used to understand the state of the system at a particular point of interest.
Security logging establishes accountability and useful for forensics.
Audit logging is the digital evidence and used for auditing purposes.