









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of log4j, a popular java logging library. It covers the concepts of categories, priorities, and appenders, and explains how they work together to enable logging. The document also includes examples and links to further resources.
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Fall 2010
cs
46
Standard usage.
class Foo {class Foo {
category log = null;public Foo(){
log = Category getInstance(getClass());log = Category.getInstance(getClass());log.info(“Constructing foo”);
Public String doStuff(Long x){
log.debug(“doing stuff”);
Fall 2010
cs
47
categoriescategories
lies at the heart of log4j.
Categories define a hierarchy and give the
p
rogrammer
programmer
g
y
g
p
g
p
g
runrun-
-time
time
control on which statements are printed or not.
Categories are assigned priorities
priorities. A log statement is
printed depending on its priority
and
its category
printed depending on its priority
and
its category.
Used to support output to multiple logs (Appenders) atthe same time.
L
4j
fi
INFO
Log4j.category.com.mycompany.finance=INFO,
FIN_Appender
This will direct all log messages in package
g
g
p
g
com.mycompany.finance with priority > INFO.
Fall 2010
cs
49
`
For example, the "database" category, "RMI"
t
"
it "
t
th
"XML"
t
category, "security" category, or the "XML" category.
Fall 2010
cs
50
If no category is defined via a configuration file orprogrammatically, then all messages will be sent toprogrammatically, then all messages will be sent tothe root category.
`
All Categories define a priority level and anAppender.
Ex of definition in (log4j.properties):Log4j rootCategory=WARN ROOT AppenderLog4j.rootCategory=WARN, ROOT_Appender
Fall 2010
cs
52
``
``
``
``
Fall 2010
cs
53
Used to customize the layout of a log entry. The format is closelyrelated to conversion pattern of the printf function in ‘c’ The followingoptions are available:
p
`
c - Used to output the category of the logging event.
`
C - Used to output the fully qualified class name of the caller issuingthe logging request.
`
d - Used to output the date of the logging event. The date conversionspecifier may be followed by a date format specifier enclosedbetween braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMMyyyy HH:mm:ss,SSS}. If no date format specifier is given thenyyyy
}
p
g
ISO8601 format is assumed
`
F - Used to output the file name where the logging request wasissued.
`
l
Used to output location information of the caller which generated
`
l - Used to output location information of the caller which generatedthe logging event. (C+M+L)
`
L - Used to output the line number from where the logging requestwas issued.
Fall 2010
cs
55
Fall 2010
cs
56
Logger, Appender and Layouts work together to enablelogging
Logger ~ logging space `
A hierarchical tree with associated Appender, Level,and Layout settings at every leveland Layout settings at every level
`
Level
`
Five built in levels (plus “all” and “off”)
`
Settings can also inherit to lower tree levels
Appenders
A
d
h
dl
l
t
t
A
ppenders handle log output
Many appenders available
Layouts
Fall 2010
cs
58
Layouts
Download Log4J API `
http://logging.apache.org/log4j/1.2/download.html
p
gg
g
p
g
g j
log4j.properties: eclipse log4j HOWTO in 10 minutes `
http://snippets.dzone.com/posts/show/
Short introduction to log4j `
http://logging.apache.org/log4j/1.2/manual.htm
Examples
Examples `
http://www.devdaily.com/blog/post/java/simple-log4j-example/
Fall 2010
cs
59