Docsity

Java Logging-Java Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Java Logging-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Advanced Java ProgrammingAdvanced Java Programming

LectureLecture-

-04: Java Logging

04: Java Logging -

- Log4J

Log4J

Fall 2010

cs

1

IntroductionIntroduction `

Using the Log4J as a standard approach to logging.

`

Logging defined as

“A record, as of the

A record, as of the

performance of a machine or the progress of anperformance of a machine or the progress of anperformance of a machine or the progress of anperformance of a machine or the progress of anundertakingundertaking.”

(Dictionary.com)

Fall 2010

cs

2

"The Practice of Programming" by Brian W. Kernighanby Brian W. Kernighan

“As personal choice,

we tend

not to use

debuggers

beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost indetails of complicated data structures and control flow;we find stepping

through

a program less productive

we find stepping

through

a program less productive

than thinking harder and adding output statements andself-checking

code

at

critical

places.

Clicking

over

statements takes longer than scanning the output ofstatements takes longer than scanning the output ofjudiciously-placed

displays.

It

takes

less

time

to

decide where to put print statements than to single-step to the critical section of code, even assuming weknow

where

that

is.

More

important,

debugging

statements stay with the program; debugging sessions

Fall 2010

cs

4

statements

stay

with

the

program

;

debugging

sessions

are transient.”

Reference:Reference:

http://logging.apache.org/log4j/1.2/manual.html

Value of LoggingValue of Logging

`

Do programmers use debuggers or Loggers? `

Logging is often faster then using a debugger

`

Logging is often faster then using a debugger.

`

Logging can be used to diagnose problems in the field.

`

Debugging is difficult in a distributed computing environment.

gg

g

p

g

Note:Note:

The key to using a logger for debugging is to

have lots of events recorded.

Fall 2010

cs

5

What is logged?What is logged? `

Types of information logged `

Program flow

`

Program flow

`

Detailed information about what occurs in a method ata granular level.

`

Information about a specific error that has occurred inthe system.

`

Document historical business events that have

`

Document historical business events that haveoccurred.

Fall 2010

cs

7

Log the logLog the log

`

log statements pollute source code and decreaselegibility

g

y

`

Java language where a preprocessor is not available, logstatements increase the size of the code and reduce itsspeed even when logging is turned offspeed, even when logging is turned off

`

reasonably sized application may contain thousands of logstatements, speed is of particular importance

Abilit

t

bl

l

i

t

ti

ith

t

dif i

`

Abilit

y to enable logging at runtime without modifying

the application binary.

`

Need a way to package log statements that can remain in

y

p

g

g

shipped code without incurring a heavy performance cost.

`

Logging behavior can be controlled by editing a configurationfile without touching the application binary

Fall 2010

cs

8

file, without touching the application binary.

Java Specification Request 47Java Specification Request 47 `

Enable/disable logging at runtime

`

Control logging at a fairly fine granularity

`

Control logging at a fairly fine granularity `

Disable logging for specific functionality

`

Bridge services that connect the logging APIs to

`

Bridge services that connect the logging APIs toexisting logging services (Operating System Logs,Third party logs)

`

A

vailable for public review at

http://java.sun.com/aboutJava/communityprocess/review/jsr047/index.html

Fall 2010

cs

10

What is log4j?What is log4j? `

Log4j is the most popular logging package for Java

`

Sub-project of the logging services project at Apache

`

Sub-project of the logging services project at Apache(http://logging.apache.org) `

Formerly part of the Jakarta project

y

j

`

Used primarily as a debugging and auditing tool

Logging performanceLogging performance `

Log4j claims to be fast and flexible: speed first,

speed first,

flexibility second.flexibility second.flexibility second.flexibility second. `

Although log4j has a many features, its first design goalwas speed.

`

Some log4j components have been rewritten many timesto improve performance.

Fall 2010

cs

13

Cost of loggingCost of logging `

When logging is turned off entirely or just for a set of priorities,

the cost of a log request consists of athe cost of a log request consists of a

method invocation plus an integer comparisonmethod invocation plus an integer comparisonmethod invocation plus an integer comparison.method invocation plus an integer comparison. `

On an AMD Duron clocked at 800Mhz running JDK1.3.1, it costs about 5 nanoseconds to determine if a

logging statement should be logged or not.

`

Actual logging is also quite fast, ranging from

i

d

i

th

Si

l L

t

Si

l L

t

microseconds using the

Si

mpleLayout

SimpleLayout

microseconds using the

TTCCLayoutTTCCLayout

. The

performance of the

PatternLayoutPatternLayout

is almost as

good as the dedicated layouts, except that it is muchgood as the dedicated layouts, except that it is muchmore flexible.

`

This is the cost of formatting the log output and sending it toit

t

t d

ti

ti

A t

l fi

i

th

k

j

d

Fall 2010

cs

14

its target destination. Actual figures in the package javadoc.