Logging with log4j v1.2
-
Upload
kamal-mettananda -
Category
Software
-
view
308 -
download
4
Transcript of Logging with log4j v1.2
![Page 1: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/1.jpg)
Logging with Log4jKAMAL METTANANDASOFTWARE ARCHITECTACCELERITE – PERSISTENT SYSTEMS@KAMALMETTANANDA
![Page 2: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/2.jpg)
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
![Page 3: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/3.jpg)
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
![Page 4: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/4.jpg)
KAMAL METTANANDA 4
Objective
Make sure ALL brothers & sisters know how to use log4j in a Java project
![Page 5: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/5.jpg)
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
![Page 6: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/6.jpg)
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
![Page 7: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/7.jpg)
KAMAL METTANANDA 7
Why logging
Troubleshoot by finding
What has happened
What is happening
![Page 8: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/8.jpg)
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
![Page 9: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/9.jpg)
KAMAL METTANANDA 9
How to log
System.out.print()
• Most simplest• Not enough information• Unable to switch on/off
![Page 10: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/10.jpg)
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
![Page 11: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/11.jpg)
KAMAL METTANANDA 11
Logging librariesLog4j v1.2
Log4j v2
JUL - Java Logging API
Logback
Commons logging
SLF4J
![Page 12: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/12.jpg)
KAMAL METTANANDA 12
Log4j 1.2APACHE SOFTWARE
![Page 13: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/13.jpg)
KAMAL METTANANDA 13
Environment SetupInstall Java
• From https://github.com/lkamal/log4j-workshop
Install Eclipse
Import Projects
![Page 14: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/14.jpg)
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
![Page 15: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/15.jpg)
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
![Page 16: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/16.jpg)
KAMAL METTANANDA 16
Hello World sampleHello world with Sysout
•01_helloworld_sysout
Hello world with log4j
•02_helloworld_log4j
![Page 17: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/17.jpg)
KAMAL METTANANDA 17
Log4j Components
Logger
![Page 18: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/18.jpg)
KAMAL METTANANDA 18
Logger instance by name
org.apache.log4j.Logger =
org.apache.log4j.Logger.getLogger(Class)
org.apache.log4j.Logger.getLogger(String)
![Page 19: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/19.jpg)
KAMAL METTANANDA 19
Logger Instance public class MainLog4j { private static Logger logger = Logger.getLogger(MainLog4j.class); …
}
![Page 20: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/20.jpg)
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
![Page 21: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/21.jpg)
KAMAL METTANANDA 21
Calculator sampleCalculator
with Sysout•03_calculator_sysout
Calculator with log4j
•04_calculator_log4j
![Page 22: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/22.jpg)
KAMAL METTANANDA 22
Log level - PriorityTRACE
ERROR
FATALLog Filter Size
Message Priority
![Page 23: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/23.jpg)
KAMAL METTANANDA 23
Log Level - UsageLeast priority messageslogger.trace()
logger.debug()
logger.info()
logger.warn()
logger.error()
Highest priority messageslogger.fatal()
![Page 24: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/24.jpg)
KAMAL METTANANDA 24
Log Level - log4j.properties
log4j.rootLogger=<LEVEL>, <Appenders>
• e.g:• DEBUG, console• INFO , console• ERROR , console
![Page 25: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/25.jpg)
KAMAL METTANANDA 25
Calculator with log levelsCalculator with
log levels
•05_calculator_log4j_levels
![Page 26: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/26.jpg)
KAMAL METTANANDA 26
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
![Page 27: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/27.jpg)
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
![Page 28: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/28.jpg)
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
![Page 29: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/29.jpg)
KAMAL METTANANDA 29
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Package
Log Level
Appenders
![Page 30: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/30.jpg)
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
![Page 31: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/31.jpg)
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
![Page 32: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/32.jpg)
KAMAL METTANANDA 32
Calculator with appenders
Calculator with
multiple log destination
s
• 08_calculator_log4j_appenders
![Page 33: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/33.jpg)
KAMAL METTANANDA 33
log4j.properties - file content
log4j.rootLogger
Log level
Appenders
log4j.appender
Destination
Layout
log4j.logger
Name
Log Level
Appenders
![Page 34: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/34.jpg)
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
![Page 35: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/35.jpg)
KAMAL METTANANDA 35
Calculator with CategoriesCalculator with
different loggers
•09_calculator_log4j_categories
![Page 36: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/36.jpg)
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
![Page 37: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/37.jpg)
KAMAL METTANANDA 37
Calculator with exceptionCalculator with
exception
•10_calculator_log4j_exception
![Page 38: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/38.jpg)
KAMAL METTANANDA 38
Log4j – Performance hit
![Page 39: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/39.jpg)
KAMAL METTANANDA 39
Log4j – Performance hitif (logger.isDebugEnabled()) {
logger.debug(“method invoked ” + result);
}
if (logger.isInfoEnabled()) {
logger.info(“method invoked ” + result);
}
![Page 40: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/40.jpg)
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
![Page 41: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/41.jpg)
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
![Page 42: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/42.jpg)
KAMAL METTANANDA 42
JUL - Java logging APIJava itself has a
logging API
java.util.logging.Logger
![Page 43: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/43.jpg)
KAMAL METTANANDA 43
Logging libraries
© zeroturnaround.com
![Page 44: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/44.jpg)
KAMAL METTANANDA 44
Learn furtherLogback
Log4j 2
JUL - Java logging API
Commons-logging
SLF4j
![Page 45: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/45.jpg)
KAMAL METTANANDA 45
![Page 46: Logging with log4j v1.2](https://reader035.fdocuments.net/reader035/viewer/2022081505/58887b301a28ab34788b63fb/html5/thumbnails/46.jpg)
Thank you