SlideShare a Scribd company logo
Log4j
Documentation
This documentation explains how to set up log4j with email, files and stdout. It compares
XML to properties configuration files, shows how to change LogLevels for a running
application. Furthermore, we explain best practices on logging and exception handling.
• Create the file 'src/main/resources/log4j.properties’.
• 'pom.xml' file contains:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
• Now we will discuss about some statements in DwfLogger.java file:
Log4j(DwfLogger.java ) will first check for a file log4j.xml(dwflogger.xml) and then for
a log4j.properties file in the root directory of the classes folder (= src folder before
compilation).
 URL loggerUrl = ClassLoader.getSystemResource("dwflogger.xml");
The above statement given in DwfLogger.java file and it goes to the dwflogger.xml
file.
 PropertyConfigurator.configure("log4j.properties");
The above statement goes to log4j.properties file used to parse the URL to
configure log4j unless the URL ends with the ".xml" extension.
 The PropertyConfigurator will be used to parse the URL to configure
log 4j unless the URL ends with the ".xml" extension.
 Logger logger = DwfLogger.getLogger(DwfLogger.class);
 getLogger(java.lang.String name)
Return a new logger instance named as the first parameter using
the default factory.
 getLogger
public Logger getLogger(java.lang.String name)
Return a new logger instance named as the first parameter using
the default factory.
If a logger of that name already exists, then it will be returned.
Otherwise, a new logger will be instantiated and then linked with its existing ancestors as well as
children.
Specified by:
getLogger in interface LoggerRepository
Parameters:
name - The name of the logger to retrieve.
 GetLogger
public Logger
getLogger(java.lang.String name,LoggerFactory factory)
Return a new logger instance named as the first parameter using
factory.
If a logger of that name already exists, then it will be returned.
Otherwise, a new logger will be instantiated by the factory
parameter and linked with its existing ancestors as well as
children.
Specified by:
getLogger in interface LoggerRepository
Parameters:
name - The name of the logger to retrieve.
factory - The factory that will make the new logger
instance.
 Logger level
logger.trace("This is the Message that is logged as Trace Level");
logger.debug("This is the Message that is logged as Debug Level");
logger.info("This is the Message that is logged as Info Level");
logger.warn("This is the Message that is logged as Warn Level");
logger.error("This is the Message that is logged as Error Level");
logger.fatal ("This is the Message that is logged as Fatal Level");
The following Levels are available. But you can define custom levels as well. Examples
are provided with the log4j download.
Level Description
all All levels including custom levels
trace developing only, can be used to
follow the program execution.
debug developing only, for debugging
purpose
info Production optionally, Course
grained (rarely written informations),
I use it to print that a configuration is
initialized, a long running import job
is starting and ending.
warn Production, simple application error
or unexpected behaviour.
Application can continue. I warn for
example in case of bad login
attemps, unexpected data during
import jobs.
error Production, application
error/exception but application can
continue. Part of the application is
probably not working.
fatal Production, fatal application error,
application cannot continue, for
example database is down.
No Do not log at all.
• dwflogger.xml :
Three main components you need to configure to obtain the same result are the logger, appender
and layout.
 The logger object is the one that is used to log messages.
 The appender is the one that specifies the output destination like console or a file and
 The Layout is the one that specify the format in which the log messages should be logged.
In <root> tag, here first ‘appender-ref’ is “ConsoleAppender”.so in “log4j:configuration” tag
“appender name” is ConsoleAppender and class is "org.apache.log4j.ConsoleAppender".
 ConsoleAppender : Logs to console.
 FileAppender : Logs to a file
The invocation of the BasicConfigurator.configure method creates a rather simple log4j
setup. This method is hardwired to add to the root logger a ConsoleAppender. The output
will be formatted using a PatternLayout set to the pattern "%-4r [%t] %-5p %c %x - %m
%n".
Note that by default, the root logger is assigned to Level.DEBUG.
 Layout of the log file
The layout specifies how a log message looks like.
First you define the layout in log4j.properties file
log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout
The pattern layout requires another parameter, i.e. the pattern.
log4j.appender.rootAppender.layout.ConversionPattern=[%d{dd MMM
yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n
There you can see that we have DateLayout, HTMLLayout, PatternLayout,
SimpleLayout, XMLLayout as options.
SimpleLayout has no properties to be set. It is simple.
We used PatternLayout in our example and we set a property named ConversionPattern.
This property allows us to define the log output as folows.
[%d{dd MMM yyyy
HH:mm:ss}]
Date in Format
[%-6p] %6 defines a right justified print with 6
characters, p prints the priority of the log
message
[%C{2}] -> [%M
(%L)] - %m%n
conversion patterns
A flexible layout configurable with pattern string.
The goal of this class is to format a LoggingEvent and return the results as a String. The
results depend on the conversion pattern.
The conversion pattern is closely related to the conversion pattern of the printf function in
C. A conversion pattern is composed of literal text and format control expressions called
conversion specifiers.
You are free to insert any literal text within the conversion pattern.
Each conversion specifier starts with a percent sign (%) and is followed by optional
format modifiers and a conversion character. The conversion character specifies the type
of data, e.g. category, priority, date, thread name. The format modifiers control such
things as field width, padding, left and right justification. The following is a simple
example.
Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the log4j
environment was set to use a PatternLayout. Then the statements
Category root = Category.getRoot();
root.debug("Message 1");
root.warn("Message 2");
would yield the output
DEBUG [main]: Message 1
WARN [main]: Message 2
Note that there is no explicit separator between text and conversion specifiers. The
pattern parser knows when it has reached the end of a conversion specifier when it reads
a conversion character. In the example above the conversion specifier %-5p means the
priority of the logging event should be left justified to a width of five characters. The
recognized conversion characters are
Conversion
Character
Effect
c
Used to output the category of the logging event. The
category conversion specifier can be optionally
followed by precision specifier, that is a decimal
constant in brackets.
If a precision specifier is given, then only the
corresponding number of right most components of the
category name will be printed. By default the category
name is printed in full.
For example, for the category name "a.b.c" the pattern
%c{2} will output "b.c".
C
Used to output the fully qualified class name of the
caller issuing the logging request. This conversion
specifier can be optionally followed by precision
specifier, that is a decimal constant in brackets.
If a precision specifier is given, then only the
corresponding number of right most components of the
class name will be printed. By default the class name is
output in fully qualified form.
For example, for the class name
"org.apache.xyz.SomeClass", the pattern %C{1} will
output "SomeClass".
WARNING Generating the caller class information is
slow. Thus, it's use should be avoided unless execution
speed is not an issue.
d
Used to output the date of the logging event. The date
conversion specifier may be followed by a date format
specifier enclosed between braces. For example,
%d{HH:mm:ss,SSS} or
%d{dd MMM yyyy HH:mm:ss,SSS}. If no date
format specifier is given then ISO8601 format is
assumed.
The date format specifier admits the same syntax as the
time pattern string of the SimpleDateFormat.
Although part of the standard JDK, the performance of
SimpleDateFormat is quite poor.
For better results it is recommended to use the log4j
date formatters. These can be specified using one of the
strings "ABSOLUTE", "DATE" and "ISO8601" for
specifying AbsoluteTimeDateFormat,
DateTimeDateFormat and respectively
ISO8601DateFormat. For example, %d{ISO8601}
or %d{ABSOLUTE}.
These dedicated date formatters perform significantly
better than SimpleDateFormat.
F
Used to output the file name where the logging request
was issued.
WARNING Generating caller location information is
extremely slow. It's use should be avoided unless
execution speed is not an issue.
l
Used to output location information of the caller which
generated the logging event.
The location information depends on the JVM
implementation but usually consists of the fully
qualified name of the calling method followed by the
callers source the file name and line number between
parentheses.
The location information can be very useful. However,
it's generation is extremely slow. It's use should be
avoided unless execution speed is not an issue.
L
Used to output the line number from where the logging
request was issued.
WARNING Generating caller location information is
extremely slow. It's use should be avoided unless
execution speed is not an issue.
m
Used to output the application supplied message
associated with the logging event.
M
Used to output the method name where the logging
request was issued.
WARNING Generating caller location information is
extremely slow. It's use should be avoided unless
execution speed is not an issue.
n
Outputs the platform dependent line separator character
or characters.
This conversion character offers practically the same
performance as using non-portable line separator strings
such as "n", or "rn". Thus, it is the preferred way of
specifying a line separator.
p Used to output the priority of the logging event.
r
Used to output the number of milliseconds elapsed from
the construction of the layout until the creation of the
logging event.
t
Used to output the name of the thread that generated the
logging event.
x Used to output the NDC (nested diagnostic context)
associated with the thread that generated the logging
event.
X
Used to output the MDC (mapped diagnostic context)
associated with the thread that generated the logging
event. The X conversion character must be followed by
the key for the map placed between braces, as in
%X{clientNumber} where clientNumber is the
key. The value in the MDC corresponding to the key
will be output.
% The sequence %% outputs a single percent sign.
By default the relevant information is output as is. However, with the aid of format modifiers it is
possible to change the minimum field width, the maximum field width and justification.
The optional format modifier is placed between the percent sign and the conversion
character.
The first optional format modifier is the left justification flag which is just the minus (-)
character. Then comes the optional minimum field width modifier. This is a decimal
constant that represents the minimum number of characters to output. If the data item
requires fewer characters, it is padded on either the left or the right until the minimum
width is reached. The default is to pad on the left (right justify) but you can specify right
padding with the left justification flag. The padding character is space. If the data item is
larger than the minimum field width, the field is expanded to accommodate the data. The
value is never truncated.
This behavior can be changed using the maximum field width modifier which is
designated by a period followed by a decimal constant. If the data item is longer than the
maximum field, then the extra characters are removed from the beginning of the data
item and not from the end. For example, it the maximum field width is eight and the data
item is ten characters long, then the first two characters of the data item are dropped. This
behavior deviates from the printf function in C where truncation is done from the end.
Below are various format modifier examples for the category conversion specifier.
Format
modifier
left
justify
minimum
width
maximum
width
comment
%20c false 20 none
Left pad with spaces if the category
name is less than 20 characters long.
%-20c true 20 none
Right pad with spaces if the category
name is less than 20 characters long.
%.30c NA none 30
Truncate from the beginning if the
category name is longer than 30
characters.
%20.30c false 20 30
Left pad with spaces if the category
name is shorter than 20 characters.
However, if category name is longer
than 30 characters, then truncate from
the beginning.
%-20.30c true 20 30
Right pad with spaces if the category
name is shorter than 20 characters.
However, if category name is longer
than 30 characters, then truncate from
the beginning.
Below are some examples of conversion patterns.
%r [%t] %-5p %c %x - %m%n
This is essentially the TTCC layout.
%-6r [%15.15t] %-5p %30.30c %x - %m%n
Similar to the TTCC layout except that the relative time is right padded if less than 6
digits, thread name is right padded if less than 15 characters and truncated if longer and
the category name is left padded if shorter than 30 characters and truncated if longer.
Now in both “dwflogger.xml” and “log4j.properties” files, give the same ConversionPattern
value in log4j.properties as given below
 dwflogger.xml : <appender
name="ConsoleAppender"
class="org.apache.log4j.ConsoleAppender">
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)]
[%t] - %m%n"/>
</layout>
</appender>
<appender name="FileAppender"
class="org.apache.log4j.FileAppender">
<param name="file"
value="/logs/dwfLogs.log" />
<param name="append"
value="true" />
<param name="encoding"
value="UTF-8" />
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)]
[%t] - %m%n"/>
</layout>
</appender>
<root>
<priority value ="fatal" />
<appender-ref
ref="ConsoleAppender"/>
<appender-ref
ref="FileAppender"/>
</root>
 log4j.properties :
log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.rootAppender.layou
t.ConversionPattern=[%d{dd MMM
yyyy HH:mm:ss}] [%-6p] [%C{2}] ->
[%M (%L)] [%t] - %m%n
log4j.appender.dwfsAppender.layo
ut=org.apache.log4j.PatternLayout
log4j.appender.dwfsAppender.layo
ut.ConversionPattern=[%d{dd MMM
yyyy HH:mm:ss}] [%-6p] [%C{2}] ->
[%M (%L)] [%t] - %m%n
 In dwflogger.xml file <root> tag ,
<priority value ="fatal" /> ----------------------> Here the Priority
value is taken as ‘fatal’.
 Already we discuss about <appender-ref ref="ConsoleAppender"/>
 And <appender-ref ref="FileAppender"/>
FileAppender : FileAppender has the File and Append options both of which
are ambigous until the other is also set.
File : <param name="file"
value="/logs/dwfLogs.log" /> (In logs folder log file will be create)
Append : <param name="append" value="true" />
(Here true means log file information is appending)
Encoding : <param name="encoding" value="UTF-8" />
(Here encoding is UTF-8 )
• log4j.properties file:
 log4j.rootLogger=DEBUG,rootAppender
RootLogger : The rootLogger is the one that resides
on the top of the logger hierarchy. Here we set its level to DEBUG and added
the root appender (RA) to it. The root
appender can have arbitrary name, here its name is RA. Once the appender is
created and its layout is set you need to
specify which loggers can use this appender. If you
set this appender to the rootLogger
then all the loggers will log message to
this appender. Since the rootLogger is on top of the
hierarchy all the loggers will inherit
its logger level and its appenders.
Here we mention rootAppender so we are taking
“log4j.appender.rootAppender”
log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.rootAppender.File=/logs/application.log
log4j.appender.rootAppender.MaxFileSize=10MB
Same as the above is dwfsAppender so we are taking
“log4j.appender.dwfsAppender” as shown below
log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender
log4j.appender.dwfsAppender.File=/logs/dwfs.log
log4j.appender.dwfsAppender.MaxFileSize=10MB
log4j.appender.dwfsAppender.MaxBackupIndex=1
In the above two appenders (rootAppender and dwfsAppender) ,
 RollingFileAppender : Logs to a file, starts a new file once the max size
is reached. (An alternative is the DailyRollingFileAppender which creates on
file per day).
log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender
So by using RollingFileAppender we created two
log files namely,” application.log and dwfs.log”.If u want to rename the file name.then rename these two
files.
log4j.appender.rootAppender.File=/logs/application.log
log4j.appender.dwfsAppender.File=/logs/dwfs.log
 MaxFileSize : Maximum size of the file.
log4j.appender.rootAppender.MaxFileSize=10MB (Maximum size of file is 10MB)
 MaxBackupIndex : Keeping number of backup files
log4j.appender.dwfsAppender.MaxBackupIndex=1 (keep one Backup file)

More Related Content

What's hot

SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
venkatam
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprograms
Simon Huang
 

What's hot (19)

intro unix/linux 08
intro unix/linux 08intro unix/linux 08
intro unix/linux 08
 
Introduction to SAS Data Set Options
Introduction to SAS Data Set OptionsIntroduction to SAS Data Set Options
Introduction to SAS Data Set Options
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Sas cheat
Sas cheatSas cheat
Sas cheat
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprograms
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Programming style guideline very good
Programming style guideline very goodProgramming style guideline very good
Programming style guideline very good
 
Compare And Merge Scripts
Compare And Merge ScriptsCompare And Merge Scripts
Compare And Merge Scripts
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Java SE 8
Java SE 8Java SE 8
Java SE 8
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Rspamd symbols
Rspamd symbolsRspamd symbols
Rspamd symbols
 

Viewers also liked (6)

Log4j 2 source code reading
Log4j 2 source code readingLog4j 2 source code reading
Log4j 2 source code reading
 
Log4j 2 writing
Log4j 2 writingLog4j 2 writing
Log4j 2 writing
 
Log4j Logging Mechanism
Log4j Logging MechanismLog4j Logging Mechanism
Log4j Logging Mechanism
 
Exception handling and logging best practices
Exception handling and logging best practicesException handling and logging best practices
Exception handling and logging best practices
 
Exception handling & logging in Java - Best Practices (Updated)
Exception handling & logging in Java - Best Practices (Updated)Exception handling & logging in Java - Best Practices (Updated)
Exception handling & logging in Java - Best Practices (Updated)
 
Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management System
 

Similar to Log4j

LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 

Similar to Log4j (20)

11i Logs
11i Logs11i Logs
11i Logs
 
Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
 
Logging
LoggingLogging
Logging
 
Log4j in 8 slides
Log4j in 8 slidesLog4j in 8 slides
Log4j in 8 slides
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
 
Presentation log4 j
Presentation log4 jPresentation log4 j
Presentation log4 j
 
Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
 
JSON Logger Baltimore Meetup
JSON Logger Baltimore MeetupJSON Logger Baltimore Meetup
JSON Logger Baltimore Meetup
 
Rein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4jRein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4j
 
Log4 J
Log4 JLog4 J
Log4 J
 
Logback
LogbackLogback
Logback
 
Annotation Processing in Android
Annotation Processing in AndroidAnnotation Processing in Android
Annotation Processing in Android
 
Dost.jar and fo.jar
Dost.jar and fo.jarDost.jar and fo.jar
Dost.jar and fo.jar
 
Fluentd unified logging layer
Fluentd   unified logging layerFluentd   unified logging layer
Fluentd unified logging layer
 
Log4jxml ex
Log4jxml exLog4jxml ex
Log4jxml ex
 
Bi
BiBi
Bi
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 

Log4j

  • 1. Log4j Documentation This documentation explains how to set up log4j with email, files and stdout. It compares XML to properties configuration files, shows how to change LogLevels for a running application. Furthermore, we explain best practices on logging and exception handling. • Create the file 'src/main/resources/log4j.properties’. • 'pom.xml' file contains: <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> • Now we will discuss about some statements in DwfLogger.java file: Log4j(DwfLogger.java ) will first check for a file log4j.xml(dwflogger.xml) and then for a log4j.properties file in the root directory of the classes folder (= src folder before compilation).  URL loggerUrl = ClassLoader.getSystemResource("dwflogger.xml"); The above statement given in DwfLogger.java file and it goes to the dwflogger.xml file.  PropertyConfigurator.configure("log4j.properties"); The above statement goes to log4j.properties file used to parse the URL to configure log4j unless the URL ends with the ".xml" extension.  The PropertyConfigurator will be used to parse the URL to configure log 4j unless the URL ends with the ".xml" extension.  Logger logger = DwfLogger.getLogger(DwfLogger.class);
  • 2.  getLogger(java.lang.String name) Return a new logger instance named as the first parameter using the default factory.  getLogger public Logger getLogger(java.lang.String name) Return a new logger instance named as the first parameter using the default factory. If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated and then linked with its existing ancestors as well as children. Specified by: getLogger in interface LoggerRepository Parameters: name - The name of the logger to retrieve.  GetLogger public Logger getLogger(java.lang.String name,LoggerFactory factory) Return a new logger instance named as the first parameter using factory. If a logger of that name already exists, then it will be returned. Otherwise, a new logger will be instantiated by the factory parameter and linked with its existing ancestors as well as children. Specified by: getLogger in interface LoggerRepository Parameters: name - The name of the logger to retrieve. factory - The factory that will make the new logger instance.  Logger level
  • 3. logger.trace("This is the Message that is logged as Trace Level"); logger.debug("This is the Message that is logged as Debug Level"); logger.info("This is the Message that is logged as Info Level"); logger.warn("This is the Message that is logged as Warn Level"); logger.error("This is the Message that is logged as Error Level"); logger.fatal ("This is the Message that is logged as Fatal Level"); The following Levels are available. But you can define custom levels as well. Examples are provided with the log4j download. Level Description all All levels including custom levels trace developing only, can be used to follow the program execution. debug developing only, for debugging purpose info Production optionally, Course grained (rarely written informations), I use it to print that a configuration is initialized, a long running import job is starting and ending. warn Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad login attemps, unexpected data during import jobs. error Production, application error/exception but application can continue. Part of the application is probably not working. fatal Production, fatal application error, application cannot continue, for example database is down. No Do not log at all. • dwflogger.xml : Three main components you need to configure to obtain the same result are the logger, appender and layout.
  • 4.  The logger object is the one that is used to log messages.  The appender is the one that specifies the output destination like console or a file and  The Layout is the one that specify the format in which the log messages should be logged. In <root> tag, here first ‘appender-ref’ is “ConsoleAppender”.so in “log4j:configuration” tag “appender name” is ConsoleAppender and class is "org.apache.log4j.ConsoleAppender".  ConsoleAppender : Logs to console.  FileAppender : Logs to a file The invocation of the BasicConfigurator.configure method creates a rather simple log4j setup. This method is hardwired to add to the root logger a ConsoleAppender. The output will be formatted using a PatternLayout set to the pattern "%-4r [%t] %-5p %c %x - %m %n". Note that by default, the root logger is assigned to Level.DEBUG.  Layout of the log file The layout specifies how a log message looks like. First you define the layout in log4j.properties file log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout The pattern layout requires another parameter, i.e. the pattern. log4j.appender.rootAppender.layout.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n There you can see that we have DateLayout, HTMLLayout, PatternLayout, SimpleLayout, XMLLayout as options. SimpleLayout has no properties to be set. It is simple. We used PatternLayout in our example and we set a property named ConversionPattern. This property allows us to define the log output as folows. [%d{dd MMM yyyy HH:mm:ss}] Date in Format [%-6p] %6 defines a right justified print with 6 characters, p prints the priority of the log message [%C{2}] -> [%M (%L)] - %m%n conversion patterns A flexible layout configurable with pattern string.
  • 5. The goal of this class is to format a LoggingEvent and return the results as a String. The results depend on the conversion pattern. The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers. You are free to insert any literal text within the conversion pattern. Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example. Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the log4j environment was set to use a PatternLayout. Then the statements Category root = Category.getRoot(); root.debug("Message 1"); root.warn("Message 2"); would yield the output DEBUG [main]: Message 1 WARN [main]: Message 2 Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier %-5p means the priority of the logging event should be left justified to a width of five characters. The recognized conversion characters are Conversion Character Effect c Used to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed. By default the category name is printed in full. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
  • 6. C Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets. If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". WARNING Generating the caller class information is slow. Thus, it's use should be avoided unless execution speed is not an issue. d Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed. The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. Although part of the standard JDK, the performance of SimpleDateFormat is quite poor. For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE}. These dedicated date formatters perform significantly better than SimpleDateFormat. F Used to output the file name where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.
  • 7. l Used to output location information of the caller which generated the logging event. The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses. The location information can be very useful. However, it's generation is extremely slow. It's use should be avoided unless execution speed is not an issue. L Used to output the line number from where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue. m Used to output the application supplied message associated with the logging event. M Used to output the method name where the logging request was issued. WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue. n Outputs the platform dependent line separator character or characters. This conversion character offers practically the same performance as using non-portable line separator strings such as "n", or "rn". Thus, it is the preferred way of specifying a line separator. p Used to output the priority of the logging event. r Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. t Used to output the name of the thread that generated the logging event. x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging
  • 8. event. X Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output. % The sequence %% outputs a single percent sign. By default the relevant information is output as is. However, with the aid of format modifiers it is possible to change the minimum field width, the maximum field width and justification. The optional format modifier is placed between the percent sign and the conversion character. The first optional format modifier is the left justification flag which is just the minus (-) character. Then comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated. This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, it the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the printf function in C where truncation is done from the end. Below are various format modifier examples for the category conversion specifier. Format modifier left justify minimum width maximum width comment %20c false 20 none Left pad with spaces if the category name is less than 20 characters long. %-20c true 20 none Right pad with spaces if the category name is less than 20 characters long. %.30c NA none 30 Truncate from the beginning if the category name is longer than 30 characters.
  • 9. %20.30c false 20 30 Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. %-20.30c true 20 30 Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. Below are some examples of conversion patterns. %r [%t] %-5p %c %x - %m%n This is essentially the TTCC layout. %-6r [%15.15t] %-5p %30.30c %x - %m%n Similar to the TTCC layout except that the relative time is right padded if less than 6 digits, thread name is right padded if less than 15 characters and truncated if longer and the category name is left padded if shorter than 30 characters and truncated if longer. Now in both “dwflogger.xml” and “log4j.properties” files, give the same ConversionPattern value in log4j.properties as given below  dwflogger.xml : <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n"/> </layout> </appender> <appender name="FileAppender" class="org.apache.log4j.FileAppender"> <param name="file" value="/logs/dwfLogs.log" /> <param name="append" value="true" /> <param name="encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n"/> </layout>
  • 10. </appender> <root> <priority value ="fatal" /> <appender-ref ref="ConsoleAppender"/> <appender-ref ref="FileAppender"/> </root>  log4j.properties : log4j.appender.rootAppender.layout=org.apache.log4j.PatternLayout log4j.appender.rootAppender.layou t.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n log4j.appender.dwfsAppender.layo ut=org.apache.log4j.PatternLayout log4j.appender.dwfsAppender.layo ut.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}] [%-6p] [%C{2}] -> [%M (%L)] [%t] - %m%n  In dwflogger.xml file <root> tag , <priority value ="fatal" /> ----------------------> Here the Priority value is taken as ‘fatal’.  Already we discuss about <appender-ref ref="ConsoleAppender"/>  And <appender-ref ref="FileAppender"/> FileAppender : FileAppender has the File and Append options both of which are ambigous until the other is also set. File : <param name="file" value="/logs/dwfLogs.log" /> (In logs folder log file will be create) Append : <param name="append" value="true" /> (Here true means log file information is appending) Encoding : <param name="encoding" value="UTF-8" /> (Here encoding is UTF-8 ) • log4j.properties file:
  • 11.  log4j.rootLogger=DEBUG,rootAppender RootLogger : The rootLogger is the one that resides on the top of the logger hierarchy. Here we set its level to DEBUG and added the root appender (RA) to it. The root appender can have arbitrary name, here its name is RA. Once the appender is created and its layout is set you need to specify which loggers can use this appender. If you set this appender to the rootLogger then all the loggers will log message to this appender. Since the rootLogger is on top of the hierarchy all the loggers will inherit its logger level and its appenders. Here we mention rootAppender so we are taking “log4j.appender.rootAppender” log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender log4j.appender.rootAppender.File=/logs/application.log log4j.appender.rootAppender.MaxFileSize=10MB Same as the above is dwfsAppender so we are taking “log4j.appender.dwfsAppender” as shown below log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender log4j.appender.dwfsAppender.File=/logs/dwfs.log log4j.appender.dwfsAppender.MaxFileSize=10MB log4j.appender.dwfsAppender.MaxBackupIndex=1 In the above two appenders (rootAppender and dwfsAppender) ,  RollingFileAppender : Logs to a file, starts a new file once the max size is reached. (An alternative is the DailyRollingFileAppender which creates on file per day). log4j.appender.rootAppender=org.apache.log4j.RollingFileAppender log4j.appender.dwfsAppender=org.apache.log4j.RollingFileAppender
  • 12. So by using RollingFileAppender we created two log files namely,” application.log and dwfs.log”.If u want to rename the file name.then rename these two files. log4j.appender.rootAppender.File=/logs/application.log log4j.appender.dwfsAppender.File=/logs/dwfs.log  MaxFileSize : Maximum size of the file. log4j.appender.rootAppender.MaxFileSize=10MB (Maximum size of file is 10MB)  MaxBackupIndex : Keeping number of backup files log4j.appender.dwfsAppender.MaxBackupIndex=1 (keep one Backup file)