Limitations of System.out.println for Logging and Introduction to Log4j, Slides of Java Programming

The limitations of using system.out.println for application logging and introduces log4j as an alternative. Log4j is an open-source logging library that provides more advanced features such as multiple loggers, appenders, and layouts. It also allows for separating messages by domain, severity, and making one log in different formats. Instructions on how to install and use log4j.

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
Limitations of logging to
System.out.println
`Application log messages get intermingled with
server lo
g
messa
g
es
Limitations of logging to
System.out.println
gg
`System.out and System.errare not persistent
`There’s only one System.out and only one System.err
`Can’t log the same message to multiple destinations
using System.out.println()and System.err.println()
`
Can
t make one log in text format and another in XML
`
Can t
make
one
log
in
text
format
and
another
in
XML
format
`Can’t separate messages into separate logs by domain,
subject, severity, who can fix, etc.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Limitations of System.out.println for Logging and Introduction to Log4j and more Slides Java Programming in PDF only on Docsity!

Limitations of logging to

System.out.println

Limitations of logging to `^ Application log messages get intermingled withserver log messages

System.out.println

g^

g

`^ System.out

and

System.err

are not persistent

`^ There’s only one

System.out

and only one

System.err

`^ Can’t log the same message to multiple destinationsusing

System.out.println()

and^

System.err.println()

`^ Can

’t make one log in text format and another in XML

^ Can t make one log in text format and another in XMLformat^ Can’t separate messages into separate logs by domain,subject, severity, who can fix, etc.

Guess what?Guess what?

Log4j architectureLog4j architecture `^ Log4j has three main things in its architecture:loggersloggers, appenders

appenders, and layouts

layouts.

loggersloggers, appenders

appenders, and layouts

layouts.

^ LoggersLoggers are the things you work with in code to sendmessages to.^ AppendersAppenders are output destinations ``^ LayoutsLayouts are the formats that appenders use to writetheir output

LoggersLoggers ^ A logger is what your code interacts with to requestthat a message be logged.that a message be logged.^ It takes all message requests, regardless of level,severity, eventual destination, etc. ^ Internally every logger has a java-class-like name^^ Dots between words work like package and sub-package

t

separators

LoggersLoggers ^ You declare loggers in your code then start loggingto them^ You do this using the

Logger.getLogger(Class

c)^ or

Logger.getLogger(String s)

methods

^ If the logger specified does not exist it is automatically^ If the logger specified does not exist it is automaticallycreated `^ You generally pass the class (or name of the class) that isd^

l^ i^

th^ l

declaring the logger

LoggersLoggers ^ Before using your loggers you need to make surethe log4j engine is configuredthe log4j engine is configured^ Typically this is done by reading a properties file, butcan be done programmatically `^ Will discuss configuration in more depth in a minute

class

Logger

-^ basic methods

class

Logger

basic methods

package org.apache.log4j; public class

Logger

{ // C

ti^

&^ t i

l^

th d

// Creation & retrieval methods:public static Logger getRootLogger();public static Logger getLogger(String name);// printing methods:// printing methods:public void trace(Object message);public void debug(Object message);public void info(Object message);public void info(Object message);public void warn(Object message);public void error(Object message);public void fatal(Object message);p^

(O j^

g );

// generic printing method:public void log(Level l, Object message);}

Fall 2010 cs

}^25

Using a LoggerUsing a Logger ^ The logger will always accept your message^ However the logger might not always route it^ However, the logger might not always route itanywhere^ Where it goes is a function of

g

^ the message severity^ the logger’s name `^ what appenders are attached to it

AppendersAppenders `^ Appenders are actual output destinations for themessages.

g

`^ There are lots of different kinds of appenders, ofwhich

System.out (stdout)

and

System.err (stderr)

are

just twojust two ` There are also appenders for files, Swingcomponents, remote socket servers, JMS, NT Eventloggers, and remote Unix Syslog daemons