Logging in Java: Commonly Used Appenders and Configurations in log4j, Slides of Java Programming

An overview of commonly used log appenders in log4j, including consoleappender, fileappender, rollingfileappender, dailyrollingfileappender, socketappender, jmsappender, jdbcappender, smtpappender, and xmllayout, htmllayout, and patternlayout. It also covers the basics of configuring log4j using a properties file and defines the root logger, appender definitions, and at least one appender with a specified layout.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Commonly used Appenders
Commonly used Appenders
`` ConsoleAppenderConsoleAppender sends messages to the
console (
stdout
/
stderr
)
console
(
stdout
/
stderr
)
`` FileAppenderFileAppender sends messages to a file (which
you name)
you
name)
`` RollingFileAppenderRollingFileAppender sends messages to a file,
limited b
y
len
g
th
yg
`Each time length threshold is exceeded, a new file is
started, with a new suffix
`You specify the main part of the file name and
threshold size
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Logging in Java: Commonly Used Appenders and Configurations in log4j and more Slides Java Programming in PDF only on Docsity!

Commonly used AppendersCommonly used Appenders ^ ConsoleAppenderConsoleAppender – sends messages to theconsole (stdout/stderr)console (stdout/stderr)^ FileAppenderFileAppender – sends messages to a file (whichyou name)you name) ``^ RollingFileAppenderRollingFileAppender – sends messages to a file,limited by length

y^

g

^ Each time length threshold is exceeded, a new file isstarted, with a new suffix^ You specify the main part of the file name andthreshold size

Commonly used AppendersCommonly used Appenders ``^ DailyRollingFileAppenderDailyRollingFileAppender

  • sends messages to a

file limited by timefile, limited by time ^ Rolls over a new log file based on an interval (not limitedto days) and suffixes the file with the date/time^ You specify the main part of the file name and the interval

LayoutsLayouts ^ Layouts are how appenders format the logmessages sent to them by loggersmessages sent to them by loggers^ There are three layouts provided by log4j:^ `^

XMLLayout – format the logging event as an xml line

y^

gg^

g

^ HTMLLayout – format the logging event as an HTML table^ PatternLayout – format the logging event as a printf-likestringstring

PatternLayoutPatternLayout ^ PatternLayout is the most commonly used option^^

Used most frequently with File and Console appenders

q^

y^

pp

(i.e., clear text appenders), which are themselves themost common Very flexible Very flexible If you’ve ever done a SimpleDateFormat, you’ll knowwhat this is like:^^ “%-5p [%t]: %m%n” shows the severity the thread name the`^ %-5p [%t]: %m%n

shows the severity, the thread name, the

message, and a new line

Configuring log4jConfiguring log4j ^ log4j is typically configured using a simple propertiesfilefile^^

You can use an xml file but most people use a propertiesfile ` You can also do it programmatically, but to change thingsrequires a recompile

Configuring log4jConfiguring log4j ^ At minimum, your config file:^^

Defines the root loggerDefines the root logger^^ Defines what severity level of messages should be allowedthrough^ ^ Defines what appender(s) the messages should go to^ Defines what appender(s) the messages should go to Defines at least one appender^^ This in turn usually requires the specification of one layout

ChainsawChainsaw ^ Chainsaw is a GUI log4j log viewer written andmaintained by the log4j teammaintained by the log4j team^ Can receive logging events sent directly via TCP/IP `^ Can also load an XML-layout log file

y^

g

Some Frequently Asked QuestionsSome Frequently Asked Questions `^ What’s the difference between log4j and JakartaCommons Logging?Commons Logging?Jakarta Commons Logging (JCL) is an ultra-thinlayer on top of log4j, the Avalon logkit (which nobody

)^

d h

JDK 1 4 l

i^

API

Th

id

i^ h

uses), and the JDK 1.4 logging API. The idea is thatyou can use JCL and not worry about which loggingimplementation is underneathimplementation is underneath.

Some Frequently Asked QuestionsSome Frequently Asked Questions `^ How much does it slow your app down?Amazingly, not much. Depends on how much youAmazingly, not much. Depends on how much youlog. You can actually do something (at the expenseof lines of code) to make it even faster:^ if

(l

i^

b^

bl^

d())

if^

(log.isDebugEnabled())

log.debug(“x=“+longRunning());

Any other questions?