What is logging ?
Logging is an API. by using logs we can store the error/application execution details into various preferred destinations such as a file, database, console, etc..!
Why we need logging?
with the help of System.out.println() we can see the error messages in console.
but in a real time if the project developed and deploy into live environment we can't check the console messages, so logs came into picture with the help of logs we can store the error details into destinations like text file or database etc..! .
Two types of objects available with log4j framework.
1)Core Objects
2)Support Objects
Core objects are mandatory objects of the framework.
core objects are
1)Logger
2)Appender
3)Layout
Logger
Logger is the top-level layer which provides the Logger object.
The Logger object is responsible for taking logging information, and they are stored in a namespace hierarchy.
Logger is a class, which is available in org.apache.log4j.*.
Logger class provided methods to write log message.
1.trace(String msg)
2.debug(String msg)
3.info(String msg)
4.warn(String msg)
5.error(String msg)
6.fatal(String msg)
Logger methods are used to generate log statements in a java class instead of print(sop) method.
Note: we are not using all this methods at a time based on requirement error/inform level we can use any one of method.
Appender
The appender is the lower layer component, which provides Appender objects.
The Appender object is responsible for publishing logging information to various preferred destinations such as file, database, console, Unix Syslog, etc.
/** Logger classes are used to generate statements in different levels, and Appender takes these logs and stores in destination files/database etc..! **/
Appender is not a class, it is an interface.
File Appender
used to append log events to a file.
Rolling File Appender
used to back up(storing) the log files when they reach a certain size.
Daily Rolling File Appender
underlying log file is rolled over at an user-chosen frequency.
Console Appender
appends log events to System.Err or System.Out using a layout specified by the user. The default console is System.Out.
JDBC Appender
used to store log messages in database.
SMTP Appender
used to send an email when a specific logging event occurs, typically on errors or fatal errors.
Socket Appender
used for remote storage.
Syslog Appender
sends messages to a remote Syslog domain.
Telnet Appender
specializes in writing to a read-only socket.
Writer Appender
used to append log events to a Writer or an Output Stream depending on the user's choice.
Layout
The layout layer provides Layout objects which are used to format logging information in different styles.
It is used to provide support to Appender objects before publishing logging information.
there are different types of layout classes in log4j:
Simple Layout: It is used to format the output in a very simple manner; it prints the Level, then a dash "-" and then the log message.
Pattern Layout: used to format the output based on conversion pattern specified or if none is specified, the default conversion pattern is considered.
HTML Layout: It formats the output as an HTML table.
SLF4J
SLF4J is Simple Logging Façade for Java.
SLF4J is an API designed to give generic access to many other frameworks like Logback, log4j etc.!.
SLF4J is not a logging component and it does not do the actual logging.
It provides an abstraction layer on logging component.
ex: Log4j is a logging component and it does the logging instructed to do.
Why we use SLF4J
log components are used to perform log operation, but the main problem is if we are using any one of log framework suppose log4J after some days we like other log framework, in that case switching from one technology to another is very difficult.
to overcome this problem SLF4J came into picture. with the help of SLF4j we can switch from one frame to another easily, because SLF4J provides abstraction on log frameworks.
No comments:
Post a Comment