Log4j in 8 slides


Published on

LOG4j allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files.

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Log4j in 8 slides

  1. 1. log4j in 8 slides Tarin Gamberini www.taringamberini.comThanks to Ceki Gülcü for the “Short introduction to log4j” http://logging.apache.org/log4j/1.2/manual.html
  2. 2. Logger Named HierarchyA logger is said to be an ancestor of another logger ifits name followed by a dot is the prefix part in thedescendant logger name. root com.site.software com.site.software.model com.site.software.model.dao com.site.software.model.dao.PersonDAOImpl com.site.software.view● com.site.software is an ancestor logger of the descendant com.site.software.model.dao● com.site.software is the parent logger of the child com.site.software.model
  3. 3. LevelsA logger may be assigned to a level.● Properties configuration file log4j.rootLogger=ERROR log4j.logger.com.site.software=INFO● XML configuration file● Java configuration file Logger.getRootLogger().setLevel(Level.ERROR); Logger.getLogger(“com.site.software”).setLevel(Level.INFO);● levels are ordered TRACE < DEBUG < INFO < WARN < ERROR < FATAL
  4. 4. Level InheritanceThe inherited level for a given logger L, is equal tothe first non-null level in the logger named hierarchy,starting at L and proceeding upwards in thehierarchy towards the root logger. Assigned InheritedLogger Name Level levelroot ERROR ERRORcom.site.software WARN WARNcom.site.software.model INFO INFOcom.site.software.model.dao null INFOcom.site.software.model.dao.PersonDAOImpl null INFOcom.site.software.view null WARN
  5. 5. Logging RequestA log request of level p in a logger configured (eitherassigned or inherited, whichever is appropriate) withlevel q, is enabled if p >= q.package com.site.software.model.dao;import org.apache.log4j.Logger;public class PersonDAOImpl { private static final Logger LOG = Logger.getLogger(PersonDAOImpl.class); public PersonDAOImpl() { LOG.debug("You cant see me in the log because debug < INFO"); LOG.info("You will see me in the log because info = INFO"); LOG.warn("You will see me in the log because warn > INFO");
  6. 6. AppendersA logger may be assigned to an appender: a namedoutput destination your log messages are forwardedto.# The root logger logs to the consolelog4j.rootLogger=ERROR, con# The com.site.software logger logs to a filelog4j.logger.com.site.software=INFO, FileApp# The con appender will log in the consolelog4j.appender.con=org.apache.log4j.ConsoleAppender#The FileApp appender will log in a filelog4j.appender.FileApp=org.apache.log4j.FileAppender
  7. 7. Appender AdditivityEach enabled logging request for a given logger Lwill be forwarded to all the appenders in that loggerLA as well as all the appenders higher HA in thelogger named hierarchy. Logger Name LA HA root con com.site.software null con com.site.software.model FileApp, c con com.site.software.model.dao d FileApp, c, con com.site.software.view e con
  8. 8. Layout Conversion PatternEach appender has a layout component responsiblefor formatting log messages accordingly toconversion patterns.log4j.appender.con=org.apache.log4j.ConsoleAppenderlog4j.appender.con.layout=org.apache.log4j.PatternLayoutlog4j.appender.con.layout.ConversionPattern=%d [%t] %-5p %m (%c:%L)%nProduced logs:2010-05-14 19:29:11,996 [main] INFO You will see me in the log becauseinfo = INFO (com.site.software.model.dao.PersonDAOImpl:10)2010-05-14 19:29:11,997 [main] WARN You will see me in the log becausewarn > INFO (com.site.software.model.dao.PersonDAOImpl:11)
  9. 9. A lot of Appenders and LayoutsAppenders● ConsoleAppender appends log events to System.out or System.err● FileAppender appends log events to a file● RollingFileAppender extends FileAppender to backup the log files when they reach a certain size● DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency● SMTPAppender sends an e-mail when a specific logging event occurs● JMSAppender publishes log events to a JMS Topic● JDBCAppender provides for sending log events to a databaseLayouts● PatternLayout configurable string pattern in a printf C function style● XMLLayout appends log events as a series of log4j:event (log4j.dtd)● HTMLLayout outputs events in a HTML table