Docsity

Log4j: Understanding Categories, Priorities, and Appenders, Slides of Java Programming

Partial preview of the text

Download Log4j: Understanding Categories, Priorities, and Appenders and more Slides Java Programming in PDF only on Docsity!

Basic APIBasic API `

As of 1.0, printing messages are of theform:form:.

debug(Object message Throwable t)debug(Object message, Throwable t)debug(Object message)If the 1

st

argument is a String object, it will be

written in its present form. Other objects

p

j

rendered by a registered Object renderer for itsclass or using the Object.toString method.

Fall 2010

cs

46

Basic Usage ExampleBasic Usage Example `

Standard usage.

class Foo {class Foo {

category log = null;public Foo(){

log = Category getInstance(getClass());log = Category.getInstance(getClass());log.info(“Constructing foo”);

Public String doStuff(Long x){

log.debug(“doing stuff”);

Fall 2010

cs

47

CategoriesCategories The notion of

categoriescategories

lies at the heart of log4j.

`

Categories define a hierarchy and give the

p

rogrammer

programmer

g

y

g

p

g

p

g

runrun-

-time

time

control on which statements are printed or not.

`

Categories are assigned priorities

priorities. A log statement is

printed depending on its priority

and

its category

printed depending on its priority

and

its category.

`

Used to support output to multiple logs (Appenders) atthe same time.

L

4j

fi

INFO

Log4j.category.com.mycompany.finance=INFO,

FIN_Appender

This will direct all log messages in package

g

g

p

g

com.mycompany.finance with priority > INFO.

Fall 2010

cs

49

Category NamesCategory Names `

You can name categories by

locality

. It turns out

that instantiating a category in each class, with

g

g

y

the category name equal to the fully-qualifiedname of the class, is a useful and straightforwardapproach of defining categoriesapproach of defining categories.

`

However, this is not the only way for namingcategories. A common alternative is to namecategories. A common alternative is to namecategories by

functional areas

`

For example, the "database" category, "RMI"

t

"

it "

t

th

"XML"

t

category, "security" category, or the "XML" category.

Fall 2010

cs

50

Root categoryRoot category `

If no category is defined via a configuration file orprogrammatically, then all messages will be sent toprogrammatically, then all messages will be sent tothe root category.

`

All Categories define a priority level and anAppender.

Ex of definition in (log4j.properties):Log4j rootCategory=WARN ROOT AppenderLog4j.rootCategory=WARN, ROOT_Appender

Fall 2010

cs

52

AppendersAppenders `

An Appender is a object that sends logmessages to their final destinationmessages to their final destination.

``

FileAppenderFileAppender – Write to a log file

``

SocketAppenderSocketAppender

Dumps log output to a socket

``

SocketAppenderSocketAppender – Dumps log output to a socket

``

SyslogAppenderSyslogAppender – Write to the syslog.

Fall 2010

cs

53

PatternLayout

Customize your messageCustomize your message `

Used to customize the layout of a log entry. The format is closelyrelated to conversion pattern of the printf function in ‘c’ The followingoptions are available:

p

`

c - Used to output the category of the logging event.

`

C - Used to output the fully qualified class name of the caller issuingthe logging request.

`

d - Used to output the date of the logging event. The date conversionspecifier may be followed by a date format specifier enclosedbetween braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMMyyyy HH:mm:ss,SSS}. If no date format specifier is given thenyyyy

}

p

g

ISO8601 format is assumed

`

F - Used to output the file name where the logging request wasissued.

`

l

Used to output location information of the caller which generated

`

l - Used to output location information of the caller which generatedthe logging event. (C+M+L)

`

L - Used to output the line number from where the logging requestwas issued.

Fall 2010

cs

55

PatternLayout –Customize your messageCustomize your message `

n - Outputs the platform dependent line separator

character or characters.

`

M - Used to output the method name where the loggingrequest was issued.

`

p - Used to output the priority of the logging event.

`

t - Used to output the name of the thread that generatedthe logging eventthe logging event.

`

x - Used to output the NDC (nested diagnostic context)associated with the thread that generated the loggingevent.

Fall 2010

cs

56

Basic components of Log4jBasic components of Log4j `

Logger, Appender and Layouts work together to enablelogging

`

Logger ~ logging space `

A hierarchical tree with associated Appender, Level,and Layout settings at every leveland Layout settings at every level

`

Level

`

Five built in levels (plus “all” and “off”)

`

Settings can also inherit to lower tree levels

`

Appenders

A

d

h

dl

l

t

t

`

A

ppenders handle log output

`

Many appenders available

`

Layouts

Fall 2010

cs

58

`

Layouts

Links &

Refernces

Links &

Refernces

`

Download Log4J API `

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

p

gg

g

p

g

g j

`

log4j.properties: eclipse log4j HOWTO in 10 minutes `

http://snippets.dzone.com/posts/show/

`

Short introduction to log4j `

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

`

Examples

`

Examples `

http://www.devdaily.com/blog/post/java/simple-log4j-example/

Fall 2010

cs

59