logging with log4j v1.2
TRANSCRIPT
Logging with Log4jKAMAL METTANANDASOFTWARE ARCHITECTACCELERITE – PERSISTENT SYSTEMS@KAMALMETTANANDA
KAMAL METTANANDA 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
KAMAL METTANANDA 3
AgendaWhat is logging
• Why logging• How to log
Log4j 1.2• Format (Layouts)• Handler (Appenders) • Logger (Categories)
Log4j 2
Java logging API
Learn further
KAMAL METTANANDA 4
Objective
Make sure ALL brothers & sisters know how to use log4j in a Java project
KAMAL 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
KAMAL 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
KAMAL METTANANDA 7
Why logging
Troubleshoot by finding
What has happened
What is happening
KAMAL METTANANDA 8
What to log•Meaningful message•State of objects•Exceptions•Class name•Time of logging•Thread/invoker
Useful informati
on to troubles
hoot
KAMAL METTANANDA 9
How to log
System.out.print()
• Most simplest• Not enough information• Unable to switch on/off
KAMAL METTANANDA 10
How to log
Use a dedicated logging library
• A lot of required information• Simple enable/disable mechanism• Configurable without code changes• Multiple destinations
KAMAL METTANANDA 11
Logging librariesLog4j v1.2
Log4j v2
JUL - Java Logging API
Logback
Commons logging
SLF4J
KAMAL METTANANDA 12
Log4j 1.2APACHE SOFTWARE
KAMAL METTANANDA 13
Environment SetupInstall Java
• From https://github.com/lkamal/log4j-workshop
Install Eclipse
Import Projects
KAMAL METTANANDA 14
Log4j 1.2Started in 1996
• Released in around 2000
Free and Open source
• Apache Licensed – 2.0
Latest version 1.2.17
KAMAL METTANANDA 15
Enabling Log4j in project
•log4j-1.2.x.jarJar file•log4j.properties OR•log4j.xml
Configuration file
•org.apache.log4j.LoggerLogger instance
KAMAL METTANANDA 16
Hello World sampleHello world with Sysout
•01_helloworld_sysout
Hello world with log4j
•02_helloworld_log4j
KAMAL METTANANDA 17
Log4j Components
Logger
KAMAL METTANANDA 18
Logger instance by name
org.apache.log4j.Logger =
org.apache.log4j.Logger.getLogger(Class)
org.apache.log4j.Logger.getLogger(String)
KAMAL METTANANDA 19
Logger Instance public class MainLog4j { private static Logger logger = Logger.getLogger(MainLog4j.class); …
}
KAMAL 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
KAMAL METTANANDA 21
Calculator sampleCalculator
with Sysout•03_calculator_sysout
Calculator with log4j
•04_calculator_log4j
KAMAL METTANANDA 22
Log level - PriorityTRACE
ERROR
FATALLog Filter Size
Message Priority
KAMAL METTANANDA 23
Log Level - UsageLeast priority messageslogger.trace()
logger.debug()
logger.info()
logger.warn()
logger.error()
Highest priority messageslogger.fatal()
KAMAL METTANANDA 24
Log Level - log4j.properties
log4j.rootLogger=<LEVEL>, <Appenders>
• e.g:• DEBUG, console• INFO , console• ERROR , console
KAMAL METTANANDA 25
Calculator with log levelsCalculator with
log levels
•05_calculator_log4j_levels
KAMAL METTANANDA 26
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
KAMAL METTANANDA 27
Layout - Format
log4j.appender.<appender>.layout property value
• 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
KAMAL METTANANDA 28
Calculator with log formatting
Calculator with
xml/html logs
•06_calculator_log4j_format
Calculator with Pattern Layout logs
•07_calculator_log4j_patterlayout
KAMAL METTANANDA 29
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
KAMAL METTANANDA 30
Appenders – Output Handlers
log4j.appender.<appender> property
• 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
KAMAL METTANANDA 31
Appenders – Log Level
log4j.appender.<appender>.Threshold
• Required log level can be configured by overriding root level.• e.g.:
• log4.appender.console.Threshold=INFO
KAMAL METTANANDA 32
Calculator with appenders
Calculator with
multiple log destination
s
• 08_calculator_log4j_appenders
KAMAL METTANANDA 33
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Name
Log Level
Appenders
KAMAL 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
KAMAL METTANANDA 35
Calculator with CategoriesCalculator with
different loggers
•09_calculator_log4j_categories
KAMAL 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
KAMAL METTANANDA 37
Calculator with exceptionCalculator with
exception
•10_calculator_log4j_exception
KAMAL METTANANDA 38
Log4j – Performance hit
KAMAL METTANANDA 39
Log4j – Performance hitif (logger.isDebugEnabled()) {
logger.debug(“method invoked ” + result);
}
if (logger.isInfoEnabled()) {
logger.info(“method invoked ” + result);
}
KAMAL METTANANDA 40
Log4j 1.2 SummarySupports logging by
simple on/off mechanism
required information
multiple destinations
Needs only 3 items
Library
Configuration file
Logger instances
KAMAL METTANANDA 41
Log4j 2Performance improved against log4j 1.2
• 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
Configuration via xml, not properties file
Migration tool for 1.2 to 2 available
Not by original author
KAMAL METTANANDA 42
JUL - Java logging APIJava itself has a
logging API
java.util.logging.Logger
KAMAL METTANANDA 43
Logging libraries
© zeroturnaround.com
KAMAL METTANANDA 44
Learn furtherLogback
Log4j 2
JUL - Java logging API
Commons-logging
SLF4j
KAMAL METTANANDA 45
Thank you