Lombok Logging: Vinothkumar Selvaarasan T
Lombok Logging: Vinothkumar Selvaarasan T
Vinothkumar Selvaarasan t-
[email protected]
What is Logging
Logging is the process of writing log message during the execution of a
program.
This logging allows you to report and persist error and warning messages
as well as info message so that the message can later be reterived and
analysed.
This is very helpful to understand exceptions in the applications which are
deployed in higher environment
Vinothkumar Selvaarasan
[email protected]
What is SLF4J
SLF4J->Simple Logging Facade for Java.
Most popular one.
Provides abstraction layer to an underlying logging component
It is not a logging component and it does not do the actual logging.
Internally SLF4J will use logging framework to generate actual logs in runtime.
We can configure any logging framework like Log4J or Logback or Java
Logging
Log4j is a logging component and it does the actual Logging.
Logically SLF4J and Log4J are two different components
If you want to compare SLF4J then the right comparision would be to compare
with Apache Common Logging.
SLF4J is an API designed to give generic access to many logging
frameworks;log4j is being one ofVinothkumar
them. Selvaarasan
[email protected]
What is Log4j
Log4j framework is open source framework provided by Apache for Java
Projects.
Apache Log4j is a java based logging utility.
Log4j is one of several Java logging frameworks.
Log4j is a reliable , fast and flexible logging framework written in java ,
which is distributed under the Apache Software License.
Advantage of Log4j:
Logging is important component of the software development. A well-
written logging code offers quick debugging, easy maintenance and
structures storage of an application’s run time information
Vinothkumar Selvaarasan
[email protected]
Log4J Components
Loggers: Responsible for capturing logging information.
Layouts: Responsible for formatting logging information in different
styles.
Appenders: Responsible for publishing logging information to various
preferred destinations (File/Console/Email/Database).
Vinothkumar Selvaarasan
[email protected]
Logger
Logger is a class in the org.apache.log4j.* package.
We have to initialize one Logger object for each Java class.
We use Logger’s methods to generate log statements.
Log4j provides the factory method to get Logger objects.
Syntax:
static Logger log=Logger.getLogger(CurrentClass.class);
Vinothkumar Selvaarasan
[email protected]
Logging Levels:
Level Description
ALL All levels including custom levels.
DEBUG Most useful for debug the an application
INFO Messages the highlight the progress of the application
WARN Designates potentially harmful situations.
ERROR Designates error events that might still allow the application to continue
running
FATAL Designates Very severe error events that will presumably lead the
application abort
OFF The highest possible rank and is intended to turn off Loging
TRACE Designates finer-grained informational events than the DEBUG
Levels: ALL->TRACE->DEBUG->INFO->WARN->ERROR->FATAL->OFF
Vinothkumar Selvaarasan [email protected]
Appender:
Appenders are used to write messages into a file or console or DB or
SMTP.
Vinothkumar Selvaarasan
[email protected]
Layout
This is used to define the formatting in which logs will print in a
repository.we have different types of layouts:
PatternLayout
SimpleLayout
XMLLayout
HTMLLayout
Vinothkumar Selvaarasan t-
[email protected]
Add Maven Dependencies
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version>1.4.1</version>
</dependency>
Vinothkumar Selvaarasan t-
[email protected]
Log4j Configuration
Basic Configuration
Using properties files
Using XML file
Programmatic Configuration
Vinothkumar Selvaarasan t-
[email protected]
Using properties files
To configure log4j from an external properties file invoke the static
method configure() of the class
PropertyConfigurator:
PropertyConfigurator.configure(String configFilename)
Vinothkumar Selvaarasan t-
[email protected]
Properties file with ConsoleAppender
log4j.rootLogger=DEBUG,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%t] %-5p %c %M -%m
%n
Vinothkumar Selvaarasan t-
[email protected]
Properties file with FileAppender
log4j.rootLogger=DEBUG,console,file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
Log4j.appender.file.File=AppLogs.log
log4j.appender.file.layout.ConversionPattern=%d(dd-MMM-yyyy
hh:mm:ss )[%t] %-5p %c %M -%m%n
Vinothkumar Selvaarasan t-
[email protected]
Conversion pattern in log4j
Statement Conversion
Category Name (for Logger name) c
Fully Qualified class name C
Date and Time d,d{format}
File name of Java Class F
Location(class, method and Line No) l
Line Number only L
Log Message m
Method name M
Priority p
New Line Seperator n
Thread Name t
Time elaspsed R
Thread nested diagnostic x
Percentage Sign %%
Vinothkumar Selvaarasan t-
[email protected]