Learning log4j for Java beginners with a sample set of projects using log4j 1.2.
This was done for Level 2 computer engineering students at University of Moratuwa 2015.
I have hosted the samples in github (https://github.com/lkamal/log4j-workshop
), so that you will be able to download and try yourself.
1. Logging with Log4j
KAMAL METTANANDA
SOFTWARE ARCHITECT
ACCELERITE – PERSISTENT SYSTEMS
@KAMALMETTANANDA
2. Who am I?
B.Sc. in 2004 University of Moratuwa
Masters in computer sciences 2012
10+ years of industry experience
Working as an Architect in Accelerite
Author of www.digizol.com
2KAMAL METTANANDA
3. Agenda
What is logging
• Why logging
• How to log
Log4j 1.2
• Format (Layouts)
• Handler (Appenders)
• Logger (Categories)
Log4j 2
Java logging API
Learn further
3KAMAL METTANANDA
5. What is logging
Pile of logs in Indonesia. Photos by Rhett Butler
“Logging, or
commercial
logging,
involves
cutting trees
for sale as
timber or
pulp”
• mongabay.com
5KAMAL METTANANDA
6. What is logging
“Logging refers to the recording of
activity. Logging is a common issue for
development teams. Several frameworks
ease and standardize the process of
logging for the Java platform.”
• wikipedia
6KAMAL METTANANDA
8. What to log
• Meaningful message
• State of objects
• Exceptions
• Class name
• Time of logging
• Thread/invoker
Useful
information
to
troubleshoot
8KAMAL METTANANDA
9. How to log
•Most simplest
•Not enough information
•Unable to switch on/off
System.out.print()
9KAMAL METTANANDA
10. How to log
• A lot of required information
• Simple enable/disable mechanism
• Configurable without code changes
• Multiple destinations
Use a dedicated logging library
10KAMAL METTANANDA
27. Layout - Format
• org.apache.log4j.SimpleLayout
• INFO - message
• org.apache.log4j.HTMLLayout
• HTML table row per log line
• org.apache.log4j.xml.XMLLayout
• XML tag per log line
• org.apache.log4j.PatternLayout
• Mostly used
log4j.appender.<appender>.layout
property value
27KAMAL METTANANDA
28. Calculator with log formatting
• 06_calculator_log4j_format
Calculator
with
xml/html logs
• 07_calculator_log4j_patterlayout
Calculator
with Pattern
Layout logs
28KAMAL METTANANDA
30. Appenders – Output Handlers
• A name must be given for <appender>
• org.apache.log4j.ConsoleAppender
• System out / console
• org.apache.log4j.FileAppender
• To a given file
• org.apache.log4j.RollingFileAppender
• When one file is full – move to next
• org.apache.log4j.DailyRollingFileAppender
• Daily write to a new file
log4j.appender.<appender> property
30KAMAL METTANANDA
31. Appenders – Log Level
• Required log level can be configured by
overriding root level.
• e.g.:
• log4.appender.console.Threshold=INFO
log4j.appender.<appender>.Threshold
31KAMAL METTANANDA
32. Calculator with appenders
• 08_calculator_log4j_appe
nders
Calculator
with
multiple log
destinations
32KAMAL METTANANDA
38. Log4j – Performance hit
Minimize with methods like
isDebugEnabled() isInfoEnabled()
Smaller performance hit
38KAMAL METTANANDA
39. Log4j – Performance hit
if (logger.isDebugEnabled()) {
logger.debug(“method invoked ” + result);
}
if (logger.isInfoEnabled()) {
logger.info(“method invoked ” + result);
}
39KAMAL METTANANDA
40. Log4j 1.2 Summary
Supports logging by
simple on/off
mechanism
required information
multiple destinations
Needs only 3 items
Library
Configuration file
Logger instances
40KAMAL METTANANDA
41. Log4j 2
Performance improved against log4j 1.2
Configuration via xml, not properties file
Migration tool for 1.2 to 2 available
Not by original author
• Disclaimer: I am the founder of log4j, slf4j and logback projects but
unaffiliated with log4j 2.0. As I understand it, notwithstanding its
name, log4j 2.0 is very different than log4j 1.x. As far as the user API is
concerned, log4j 2.0 is largely incompatible with log4j 1.x.
• stackoverflow.com/a/12095467/2581128
41KAMAL METTANANDA
42. JUL - Java logging API
Java itself has
a logging API
java.util.logging.Logger
But was too
late to be born
42KAMAL METTANANDA