log4j - Architecture
Components Of log4j
Log4j is comprised of three main components.
- Logger
- Appender
- Layout
1) Logger :
Logger is the most essential component of the logging process. It is responsible for capturing the logging information.The component logger accepts log requests generated by log statements.It then sends their output to appropriate destination these destinations are termed as appenders.
Components Of log4j
Log4j is comprised of three main components.
- Logger
- Appender
- Layout
1) Logger :
Logger is the most essential component of the logging process. It is responsible for capturing the logging information.The component logger accepts log requests generated by log statements.It then sends their output to appropriate destination these destinations are termed as appenders.
The logger component is accessible through the Logger class of the log4j API.This class provides a static method Logger.getLogger(name)
.
getLogger(name) retrieves an existing logger object by the given name or , creates a new logger of given name if none exists.
The logger object is then used to set properties of logger component. It will invoke methods which generate log requests.These methods are debug(), info(), warn(), error(), fatal(), and log()
Each class in the Java application being logged can have an individual logger assigned to it or share a common logger with other classes.
Any number of loggers can be created for the application to suit specific logging needs
It is a common practice to create one logger for each class, with a name same as the fully-qualified class name.
Getting Logger Object
1) Create a new logger:
Logger logger = Logger.getLogger("MyLogger");
2) Instantiate a static logger globally, based on the name of the class :
static Logger log = Logger.getLogger(YourClassName.class.getName())
Note: While creating a Logger object we need to pass either fully qualified class name or class object as a parameter, class means current class for which we are going to use Log4j.
Examplepublic class Test { static Logger l = Logger.getLogger(Test.class.getName()); public static void main(String[] args) { // Our logic will goes here } }
Now ,log4j API is accessible to user's application classes and can be used for logging.
Logger - Priority levels
Loggers can be assigned different levels of priorities.priority levels in ascending order of priority is given below
1. TRACE
This log4j level gives more detailed information than the DEBUG level and sits top of the hierarchy. This level is introduced from version 1.2.12 in log4j.
2. DEBUG
This log4j level helps developer to debug the application.It is used to print the fine-grained informational events that are most useful to debug an application.
3. INFO
The INFO level is associated with significant events in the normal life cycle of the application. It is used to print message related to progress of the application.
4. WARN
The WARN level designates potentially harmful situations of the application.The messages coming out of this level may not halt the progress of the system.
5. ERROR
This level gives information about a serious error which needs to be resolved and may result in unstable state. Error conditions do not necessarily cause the application to crash and the application may continue to service subsequent requests.
6. FATAL
The FATAL level represents very severe error situation that will lead the application to abort.
There are printing methods for each of these priority levels for example logger.info("logstatement1");
generates log request of priority level INFO
for logstatement1.
To summarize priority level is given below.
trace < debug < info < warn < error < fatal
FATAL is largest whereas TRACE has lowest priority
log4j Appenders
Appender job is to write the log statement into the external file or database or repository.
In log4j there are various appenders available such as..
ConsoleAppender
appends log events to System.out or System.err using a layout specified by the user. The default target is System.out
FileAppender
appends log events to a file.
WriterAppender
appends log events to a Writer or an OutputStream depending on the user's choice.
RollingFileAppender
extends FileAppender to backup the log files when they reach a certain size.
DailyRollingFileAppender
extends FileAppender so that the underlying file is rolled over at a user chosen frequency.
SMTPAppender
sends an e-mail when a specific logging event occurs, typically on errors or fatal errors
TelnetAppender
specializes in writing to a read-only socket
SyslogAppender
sends messages to a remote syslog domain.Layout
The Layout component defines the format in which the log statements are written to the log destination by appender
Layout is an abstract class in log4j API; it can be extended to create user-defined layouts
Some ready-made layouts are also available in log4j package; they are PatternLayout, SimpleLayout, DateLayout, HTMLLayout,and XMLLayout.
♦ Type of Layouts
HTMLLayout
formats the output as a HTML tablePatternLayout
formats the output based on a conversion pattern specified, or if none is specified, the default conversion patternSimpleLayout
formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message