Log4j Logging Mechanism

7,632 views

Published on

An introduction to Log4j, installing and using it.

Published in: Technology, Business
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,632
On SlideShare
0
From Embeds
0
Number of Embeds
763
Actions
Shares
0
Downloads
290
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Log4j Logging Mechanism

  1. 1. Log4j Logging Mechanism <ul><li>Java Tech Sessions </li></ul>By Kunal Dabir
  2. 2. Agenda <ul><li>Logging, why ? </li></ul><ul><li>Logging Methods / Alternatives </li></ul><ul><li>Log4J Basics </li></ul><ul><li>Loggers, Appenders & Layouts </li></ul><ul><li>Log4j Configuration & Optimization </li></ul><ul><li>Demonstration & Discussion </li></ul>
  3. 3. Logging, why? <ul><li>Logging is easier than debugging </li></ul><ul><li>Logging is faster than debugging </li></ul><ul><li>Logging can work in environments where debugging is not supported </li></ul><ul><li>Can work in production environments </li></ul><ul><li>Logs can be referenced anytime in future as the data is stored </li></ul>
  4. 4. Logging Methods <ul><li>Console Logging </li></ul><ul><li>File Logging </li></ul><ul><li>Database Logging </li></ul><ul><li>Email/SMS Notifications </li></ul><ul><li>Log to a socket </li></ul><ul><li>Many others… </li></ul>
  5. 5. Logging Methods, How? <ul><li>The evil System.out.println() </li></ul><ul><li>Custom Solution to Log to various datastores, eg text files, db, etc… </li></ul><ul><li>Use Standard APIs, eg. </li></ul><ul><ul><li>Log4j, </li></ul></ul><ul><ul><li>Avlon Logkit, </li></ul></ul><ul><ul><li>Java’s standard logging mechanism. </li></ul></ul>
  6. 6. Log4j Basics <ul><li>Who will log the messages? </li></ul><ul><ul><li>The Loggers </li></ul></ul><ul><li>What decides the priority of a message? </li></ul><ul><ul><li>Level </li></ul></ul><ul><li>Where will it be logged? </li></ul><ul><ul><li>Decided by Appender </li></ul></ul><ul><li>In what format will it be logged? </li></ul><ul><ul><li>Decided by Layout </li></ul></ul>
  7. 7. Logger <ul><li>Responsible for Logging </li></ul><ul><li>Accessed through java code </li></ul><ul><li>Configured Externally </li></ul><ul><li>Every Logger has a name </li></ul><ul><li>Prioritize messages based on level </li></ul><ul><ul><li>TRACE, DEBUG, INFO, WARN, ERROR & FATAL </li></ul></ul><ul><li>Usually named following dot convention like java classes do. </li></ul><ul><ul><li>Eg com.foo.bar.ClassName </li></ul></ul><ul><li>Follows inheritance based on name </li></ul>
  8. 8. Logger Hierarchy <ul><li>Named Hierarchy </li></ul><ul><ul><li>“ A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.” </li></ul></ul><ul><ul><li>http://logging.apache.org/log4j/1.2/manual.html </li></ul></ul>
  9. 9. Logger API <ul><li>Factory methods to get Logger </li></ul><ul><ul><li>Logger.getLogger(Class c) </li></ul></ul><ul><ul><li>Logger.getLogger(String s) </li></ul></ul><ul><li>Method used to log message </li></ul><ul><ul><li>trace(), debug(), info(), warn(), error(), fatal() </li></ul></ul><ul><ul><li>Details </li></ul></ul><ul><ul><ul><li>void debug(java.lang.Object message) </li></ul></ul></ul><ul><ul><ul><li>void debug(java.lang.Object message, java.lang.Throwable t) </li></ul></ul></ul><ul><ul><li>Generic Log method </li></ul></ul><ul><ul><ul><li>void log(Priority priority, java.lang.Object message) </li></ul></ul></ul><ul><ul><ul><li>void log(Priority priority, </li></ul></ul></ul><ul><ul><ul><ul><li>java.lang.Object message, java.lang.Throwable t) </li></ul></ul></ul></ul>
  10. 10. Root Logger <ul><li>The root logger resides at the top of the logger hierarchy. It is exceptional in two ways: </li></ul><ul><ul><li>it always exists, </li></ul></ul><ul><ul><li>it cannot be retrieved by name. </li></ul></ul><ul><li>Logger.getRootLogger() </li></ul>
  11. 11. Appender <ul><li>Appenders put the log messages to their actual destinations. </li></ul><ul><li>No programatic change is require to configure appenders </li></ul><ul><li>Can add multiple appenders to a Logger. </li></ul><ul><li>Each appender has its Layout. </li></ul><ul><li>ConsoleAppender, DailyRollingFileAppender, FileAppender, JDBCAppender, JMSAppender, NTEventLogAppender, RollingFileAppender, SMTPAppender, SocketAppender, SyslogAppender, TelnetAppender </li></ul>
  12. 12. Layout <ul><li>Used to customize the format of log output. </li></ul><ul><li>Eg. HTMLLayout, PatternLayout, SimpleLayout, XMLLayout </li></ul><ul><li>Most commonly used is PatternLayout </li></ul><ul><ul><li>Uses C-like syntax to format. </li></ul></ul><ul><ul><ul><li>Eg. &quot;%-5p [%t]: %m%n </li></ul></ul></ul><ul><ul><ul><li>DEBUG [main]: Message 1 WARN [main]: Message 2 </li></ul></ul></ul>
  13. 13. Log4j Configuration <ul><li>Programatic Configuration </li></ul><ul><ul><li>BasicConfigurator </li></ul></ul><ul><ul><li>PropertyConfigurator </li></ul></ul>
  14. 14. Log4j Optimization & Best Practises <ul><li>User logger as private static variable </li></ul><ul><li>Only one instance per class </li></ul><ul><li>Name logger after class name </li></ul><ul><li>Don’t use too many appenders </li></ul><ul><li>Don’t use time-consuming conversion patterns (see javadoc) </li></ul><ul><li>Use Logger.isDebugEnabled() if need be </li></ul><ul><li>Prioritize messages with proper levels </li></ul>
  15. 15. Resources <ul><li>http://logging.apache.org/log4j/1.2/index.html </li></ul><ul><li>http://www.vipan.com/htdocs/log4jhelp.html </li></ul><ul><li>http://logging.apache.org/log4j/1.2/manual.html </li></ul>
  16. 16. Demonstration & Discussion <ul><li>A simple demo application using eclipse. </li></ul>
  17. 17. Thank You !

×