Logging with Log4j
KAMAL METTANANDA
SOFTWARE ARCHITECT
ACCELERITE – PERSISTENT SYSTEMS
@KAMALMETTANANDA
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
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
Objective
Make sure ALL
brothers & sisters
know how to use log4j
in a Java project
4KAMAL METTANANDA
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
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
Why logging
Troubleshoot by finding
What has
happened
What is
happening
7KAMAL METTANANDA
What to log
• Meaningful message
• State of objects
• Exceptions
• Class name
• Time of logging
• Thread/invoker
Useful
information
to
troubleshoot
8KAMAL METTANANDA
How to log
•Most simplest
•Not enough information
•Unable to switch on/off
System.out.print()
9KAMAL METTANANDA
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
Logging libraries
Log4j v1.2
Log4j v2
JUL - Java Logging API
Logback
Commons logging
SLF4J
11KAMAL METTANANDA
Log4j 1.2
APACHE SOFTWARE
12KAMAL METTANANDA
Environment Setup
Install Java
Install Eclipse
Import Projects
• From https://github.com/lkamal/log4j-
workshop
13KAMAL METTANANDA
Log4j 1.2
Started in 1996
• Released in around 2000
Free and Open source
• Apache Licensed – 2.0
Latest version 1.2.17
14KAMAL METTANANDA
Enabling Log4j in project
• log4j-1.2.x.jarJar file
• log4j.properties OR
• log4j.xml
Configuration
file
• org.apache.log4j.Logger
Logger
instance
15KAMAL METTANANDA
Hello World sample
• 01_helloworld_sysout
Hello world
with Sysout
• 02_helloworld_log4j
Hello world
with log4j
16KAMAL METTANANDA
Log4j Components
Logger
Appender
Layout
17KAMAL METTANANDA
Logger instance by name
org.apache.log4j.Logger =
org.apache.log4j.Logger.getLogger(Class)
org.apache.log4j.Logger.getLogger(String)
18KAMAL METTANANDA
Logger Instance
public class MainLog4j {
private static Logger logger =
Logger.getLogger(MainLog4j.class);
…
}
19KAMAL METTANANDA
Simple log4j.properties file
# top level config=logging level, appenders
log4j.rootLogger=INFO, console
# console appender configurations
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
20KAMAL METTANANDA
Calculator sample
• 03_calculator_sysout
Calculator
with Sysout
• 04_calculator_log4j
Calculator
with log4j
21KAMAL METTANANDA
Log level - Priority
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
LogFilterSize
MessagePriority
22KAMAL METTANANDA
Log Level - Usage
Least priority messageslogger.trace()
logger.debug()
logger.info()
logger.warn()
logger.error()
Highest priority messageslogger.fatal()
23KAMAL METTANANDA
Log Level - log4j.properties
log4j.rootLogger=<LEVEL>, <Appenders>
•e.g:
•DEBUG, console
•INFO , console
•ERROR , console
24KAMAL METTANANDA
Calculator with log levels
• 05_calculator_log4j_levels
Calculator
with log
levels
25KAMAL METTANANDA
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
26KAMAL METTANANDA
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
Calculator with log formatting
• 06_calculator_log4j_format
Calculator
with
xml/html logs
• 07_calculator_log4j_patterlayout
Calculator
with Pattern
Layout logs
28KAMAL METTANANDA
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
29KAMAL METTANANDA
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
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
Calculator with appenders
• 08_calculator_log4j_appe
nders
Calculator
with
multiple log
destinations
32KAMAL METTANANDA
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Name
Log Level
Appenders
33KAMAL METTANANDA
Logger - Categories
log4j.logger.<logger-name>
property
• Log level
• Restrict the log level for the package
• Appender
• Restrict the appender for the logs
34KAMAL METTANANDA
Calculator with Categories
• 09_calculator_log4j_categories
Calculator
with
different
loggers
35KAMAL METTANANDA
Logging Exceptions
• logger.trace(msg, exception)
• logger.debug(msg, exception)
• logger.info(msg, exception)
• logger.warn(msg, exception)
• logger.error(msg, exception)
• logger.fatal(msg, exception)
Exceptions
can be
logged
with stack
trace
36KAMAL METTANANDA
Calculator with exception
• 10_calculator_log4j_exception
Calculator
with
exception
37KAMAL METTANANDA
Log4j – Performance hit
Minimize with methods like
isDebugEnabled() isInfoEnabled()
Smaller performance hit
38KAMAL METTANANDA
Log4j – Performance hit
if (logger.isDebugEnabled()) {
logger.debug(“method invoked ” + result);
}
if (logger.isInfoEnabled()) {
logger.info(“method invoked ” + result);
}
39KAMAL METTANANDA
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
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
JUL - Java logging API
Java itself has
a logging API
java.util.logging.Logger
But was too
late to be born
42KAMAL METTANANDA
Logging libraries
© zeroturnaround.com
43KAMAL METTANANDA
Learn further
Logback
Log4j 2
JUL - Java logging API
Commons-logging
SLF4j
44KAMAL METTANANDA
45KAMAL METTANANDA
Thank you

Logging with log4j v1.2

  • 1.
    Logging with Log4j KAMALMETTANANDA 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
  • 4.
    Objective Make sure ALL brothers& sisters know how to use log4j in a Java project 4KAMAL METTANANDA
  • 5.
    What is logging Pileof 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 “Loggingrefers 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
  • 7.
    Why logging Troubleshoot byfinding What has happened What is happening 7KAMAL 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 •Mostsimplest •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
  • 11.
    Logging libraries Log4j v1.2 Log4jv2 JUL - Java Logging API Logback Commons logging SLF4J 11KAMAL METTANANDA
  • 12.
  • 13.
    Environment Setup Install Java InstallEclipse Import Projects • From https://github.com/lkamal/log4j- workshop 13KAMAL METTANANDA
  • 14.
    Log4j 1.2 Started in1996 • Released in around 2000 Free and Open source • Apache Licensed – 2.0 Latest version 1.2.17 14KAMAL METTANANDA
  • 15.
    Enabling Log4j inproject • log4j-1.2.x.jarJar file • log4j.properties OR • log4j.xml Configuration file • org.apache.log4j.Logger Logger instance 15KAMAL METTANANDA
  • 16.
    Hello World sample •01_helloworld_sysout Hello world with Sysout • 02_helloworld_log4j Hello world with log4j 16KAMAL METTANANDA
  • 17.
  • 18.
    Logger instance byname org.apache.log4j.Logger = org.apache.log4j.Logger.getLogger(Class) org.apache.log4j.Logger.getLogger(String) 18KAMAL METTANANDA
  • 19.
    Logger Instance public classMainLog4j { private static Logger logger = Logger.getLogger(MainLog4j.class); … } 19KAMAL METTANANDA
  • 20.
    Simple log4j.properties file #top level config=logging level, appenders log4j.rootLogger=INFO, console # console appender configurations log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.SimpleLayout 20KAMAL METTANANDA
  • 21.
    Calculator sample • 03_calculator_sysout Calculator withSysout • 04_calculator_log4j Calculator with log4j 21KAMAL METTANANDA
  • 22.
    Log level -Priority TRACE DEBUG INFO WARN ERROR FATAL LogFilterSize MessagePriority 22KAMAL METTANANDA
  • 23.
    Log Level -Usage Least priority messageslogger.trace() logger.debug() logger.info() logger.warn() logger.error() Highest priority messageslogger.fatal() 23KAMAL METTANANDA
  • 24.
    Log Level -log4j.properties log4j.rootLogger=<LEVEL>, <Appenders> •e.g: •DEBUG, console •INFO , console •ERROR , console 24KAMAL METTANANDA
  • 25.
    Calculator with loglevels • 05_calculator_log4j_levels Calculator with log levels 25KAMAL METTANANDA
  • 26.
    log4j.properties - filecontent log4j.rootLogger Log level Appenders log4j.appender Destination Layout log4j.logger Package Log Level Appenders 26KAMAL 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 logformatting • 06_calculator_log4j_format Calculator with xml/html logs • 07_calculator_log4j_patterlayout Calculator with Pattern Layout logs 28KAMAL METTANANDA
  • 29.
    log4j.properties - filecontent log4j.rootLogger Log level Appenders log4j.appender Destination Layout log4j.logger Package Log Level Appenders 29KAMAL METTANANDA
  • 30.
    Appenders – OutputHandlers • 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 – LogLevel • 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
  • 33.
    log4j.properties - filecontent log4j.rootLogger Log level Appenders log4j.appender Destination Layout log4j.logger Name Log Level Appenders 33KAMAL METTANANDA
  • 34.
    Logger - Categories log4j.logger.<logger-name> property •Log level • Restrict the log level for the package • Appender • Restrict the appender for the logs 34KAMAL METTANANDA
  • 35.
    Calculator with Categories •09_calculator_log4j_categories Calculator with different loggers 35KAMAL METTANANDA
  • 36.
    Logging Exceptions • logger.trace(msg,exception) • logger.debug(msg, exception) • logger.info(msg, exception) • logger.warn(msg, exception) • logger.error(msg, exception) • logger.fatal(msg, exception) Exceptions can be logged with stack trace 36KAMAL METTANANDA
  • 37.
    Calculator with exception •10_calculator_log4j_exception Calculator with exception 37KAMAL METTANANDA
  • 38.
    Log4j – Performancehit Minimize with methods like isDebugEnabled() isInfoEnabled() Smaller performance hit 38KAMAL METTANANDA
  • 39.
    Log4j – Performancehit if (logger.isDebugEnabled()) { logger.debug(“method invoked ” + result); } if (logger.isInfoEnabled()) { logger.info(“method invoked ” + result); } 39KAMAL METTANANDA
  • 40.
    Log4j 1.2 Summary Supportslogging by simple on/off mechanism required information multiple destinations Needs only 3 items Library Configuration file Logger instances 40KAMAL METTANANDA
  • 41.
    Log4j 2 Performance improvedagainst 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 - Javalogging API Java itself has a logging API java.util.logging.Logger But was too late to be born 42KAMAL METTANANDA
  • 43.
  • 44.
    Learn further Logback Log4j 2 JUL- Java logging API Commons-logging SLF4j 44KAMAL METTANANDA
  • 45.
  • 46.