logging with log4j v1.2

46
Logging with Log4j KAMAL METTANANDA SOFTWARE ARCHITECT ACCELERITE – PERSISTENT SYSTEMS @KAMALMETTANANDA

Upload: kamal-mettananda

Post on 14-Apr-2017

307 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Logging with log4j v1.2

Logging with Log4jKAMAL METTANANDASOFTWARE ARCHITECTACCELERITE – PERSISTENT SYSTEMS@KAMALMETTANANDA

Page 2: Logging with log4j v1.2

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

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

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

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

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

KAMAL METTANANDA 7

Why logging

Troubleshoot by finding

What has happened

What is happening

Page 8: Logging with log4j v1.2

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

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

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

KAMAL METTANANDA 11

Logging librariesLog4j v1.2

Log4j v2

JUL - Java Logging API

Logback

Commons logging

SLF4J

Page 12: Logging with log4j v1.2

KAMAL METTANANDA 12

Log4j 1.2APACHE SOFTWARE

Page 13: Logging with log4j v1.2

KAMAL METTANANDA 13

Environment SetupInstall Java

• From https://github.com/lkamal/log4j-workshop

Install Eclipse

Import Projects

Page 14: Logging with log4j v1.2

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

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

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

KAMAL METTANANDA 17

Log4j Components

Logger

Page 18: Logging with log4j v1.2

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

KAMAL METTANANDA 19

Logger Instance public class MainLog4j { private static Logger logger = Logger.getLogger(MainLog4j.class); …

}

Page 20: Logging with log4j v1.2

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

KAMAL METTANANDA 21

Calculator sampleCalculator

with Sysout•03_calculator_sysout

Calculator with log4j

•04_calculator_log4j

Page 22: Logging with log4j v1.2

KAMAL METTANANDA 22

Log level - PriorityTRACE

ERROR

FATALLog Filter Size

Message Priority

Page 23: Logging with log4j v1.2

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

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

KAMAL METTANANDA 25

Calculator with log levelsCalculator with

log levels

•05_calculator_log4j_levels

Page 26: Logging with log4j v1.2

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

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

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

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

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

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

KAMAL METTANANDA 32

Calculator with appenders

Calculator with

multiple log destination

s

• 08_calculator_log4j_appenders

Page 33: Logging with log4j v1.2

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

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

KAMAL METTANANDA 35

Calculator with CategoriesCalculator with

different loggers

•09_calculator_log4j_categories

Page 36: Logging with log4j v1.2

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

KAMAL METTANANDA 37

Calculator with exceptionCalculator with

exception

•10_calculator_log4j_exception

Page 38: Logging with log4j v1.2

KAMAL METTANANDA 38

Log4j – Performance hit

Page 39: Logging with log4j v1.2

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

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

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

KAMAL METTANANDA 42

JUL - Java logging APIJava itself has a

logging API

java.util.logging.Logger

Page 43: Logging with log4j v1.2

KAMAL METTANANDA 43

Logging libraries

© zeroturnaround.com

Page 44: Logging with log4j v1.2

KAMAL METTANANDA 44

Learn furtherLogback

Log4j 2

JUL - Java logging API

Commons-logging

SLF4j

Page 45: Logging with log4j v1.2

KAMAL METTANANDA 45

Page 46: Logging with log4j v1.2

Thank you