Planning and Managing Software Projects 2011-12Logging frameworksEmanuele Della Valle, Lecturer: Daniele Dell’Agliohttp://...
Outline    Logging framework    Key concepts          Declaration and Naming          Levels          Appenders      ...
Why a logging framework?   System.out/err are not enough!   Logging frameworks overcome the System.out/err    limits    ...
Example (example1 package)...public int sum(int a, int b){               System.out.println(                              ...
Example...Logger logger = Logger.getLogger(Example.class);public int sum(int a, int b){               logger.info(        ...
Declaration and Naming   Loggers are initializated through a factory method                      Logger x = Logger.getLog...
Example (example2 package)                                                                             rootLogger         ...
Levels   While developing a system is useful to print out    messages to check the correct behaviour of the    applicatio...
Level hierarchy in Log4J                                                                  FATAL                           ...
Meaning of the levels There are not strictly rules to verify if levels are used  correctly Anyway the Log4j documentatio...
Configuring the levels    Each logger has an assigned level. The level is     assigned in to possible ways    Explicit d...
Example In the properties file:log4j.rootlogger=INFO                                                    INFO    rootLogge...
Example In the properties file:log4j.rootlogger=INFO                                                    INFO    rootLogge...
Example In the properties file:log4j.rootlogger=INFO                                                     INFO   rootLogge...
Appenders   Loggers can write on several output systems   Each output system is defined by an appender   There is a hig...
Example#Rolling file appender: fileaplog4j.appender.fileap=org.apache.log4j.RollingFileAppenderlog4j.appender.fileap.File=...
Layouts   Layouts defines the format of the log lines   Each appender is related to one layout              log4j.append...
Example#Rolling file appenderlog4j.appender.fileap=org.apache.log4j.RollingFileAppenderlog4j.appender.fileap.File=log.xmll...
Logging frameworks in Java   Log4J is not the only available logging framework for    Java   Other notable logging syste...
Logging façades   It is considered a bad practice to create a strong    dependance between the code and a specific loggin...
SLF4J   SLF4J abstracts the logging concepts allowing users to    decide what logging system use in their application   ...
SLF4JFrom http://www.slf4j.org/manual.html Planning and Managing Software Projects – Emanuele Della Valle
SLF4J   In huge projects several logger framework could be used   In this case SLF4J allows to use migration libraries  ...
SLF4J   In huge projects several logger framework could be used   In this case SLF4J allows to use migration libraries  ...
SLF4J   In huge projects several logger framework could be used   In this case SLF4J allows to use migration libraries  ...
References   Dont Use System.out.println! Use Log4j -    http://www.vipan.com/htdocs/log4jhelp.html   Apache Log4J manua...
Upcoming SlideShare
Loading in...5
×

P&MSP2012 - Logging Frameworks

382

Published on

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

No Downloads
Views
Total Views
382
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

P&MSP2012 - Logging Frameworks

  1. 1. Planning and Managing Software Projects 2011-12Logging frameworksEmanuele Della Valle, Lecturer: Daniele Dell’Agliohttp://emanueledellavalle.org
  2. 2. Outline Logging framework Key concepts  Declaration and Naming  Levels  Appenders  Layouts Java logging frameworks  Logging systems: Log4J, JUL, LogBack  Logging systems façades: ACL, SLF4J Planning and Managing Software Projects – Emanuele Della Valle
  3. 3. Why a logging framework? System.out/err are not enough! Logging frameworks overcome the System.out/err limits  Several importance levels for messages  Use of different output systems (console, file, mail...)  ... We will consider Log4J as logging framework (but the concepts are similar in other systems)Planning and Managing Software Projects – Emanuele Della Valle
  4. 4. Example (example1 package)...public int sum(int a, int b){ System.out.println( "The input values are "+a+" and "+b); int ret = a+b; System.out.println("The sum is "+ret); return ret;}... Planning and Managing Software Projects – Emanuele Della Valle
  5. 5. Example...Logger logger = Logger.getLogger(Example.class);public int sum(int a, int b){ logger.info( "The input values are "+a+" and "+b); //System.out.println( "The input values are "+a+" and "+b); int ret = a+b; logger.info("The sum is "+ret); //System.out.println("The sum is "+ret); return ret;}... Planning and Managing Software Projects – Emanuele Della Valle
  6. 6. Declaration and Naming Loggers are initializated through a factory method Logger x = Logger.getLogger("Logger1"); The logger name is unique  Two invocations of “Logger1” return the same logger object Usually logger are named with the name of the class Logger x = Logger.getLogger(ClassX.class); Log4J builds and manages a tree of loggers  Nodes and leaves are determined through logger names  The root of the tree is called rootLogger  The tree is a key concept for the management of the logger system (as we will see in the next slides)Planning and Managing Software Projects – Emanuele Della Valle
  7. 7. Example (example2 package) rootLogger pmsp logging example2 p1 p2Where each logger is declared as: Class1 s1 Class3Logger classXLogger= Logger.getLogger(ClassX.class) Class2 Planning and Managing Software Projects – Emanuele Della Valle
  8. 8. Levels While developing a system is useful to print out messages to check the correct behaviour of the application When the application is deployed most of those messages are not relevant Logging frameworks allow to define several levels of importance for the messagesPlanning and Managing Software Projects – Emanuele Della Valle
  9. 9. Level hierarchy in Log4J FATAL ERROR WARN INFO DEBUG TRACE Planning and Managing Software Projects – Emanuele Della Valle
  10. 10. Meaning of the levels There are not strictly rules to verify if levels are used correctly Anyway the Log4j documentation indicates a general guide line: • FATAL: designates very severe error events that will presumably lead the application to abort. • ERROR: designates error events that might still allow the application to continue running. • WARN: The WARN level designates potentially harmful situations. • INFO: designates informational messages that highlight the progress of the application at coarse-grained level. • DEBUG: designates fine-grained informational events that are most useful to debug an application. • TRACE: designates finer-grained informational events than the DEBUG Planning and Managing Software Projects – Emanuele Della Valle
  11. 11. Configuring the levels Each logger has an assigned level. The level is assigned in to possible ways Explicit declaration: the level associated to a logger is declared:  in the configuration file: log4j.logger.<logger-name>=<level>  or in the code: logger.setLevel(Level.<level>); Implicit declaration: the level is inferred through the logger tree  Loggers inherit the level of their parents Planning and Managing Software Projects – Emanuele Della Valle
  12. 12. Example In the properties file:log4j.rootlogger=INFO INFO rootLogger INFO pmsp INFO logging INFO example2 INFO p1 p2 INFO INFO Class1 s1 INFO Class3 INFO INFO Class2 Declared Inferred Planning and Managing Software Projects – Emanuele Della Valle
  13. 13. Example In the properties file:log4j.rootlogger=INFO INFO rootLoggerlog4j.logger.pmsp.logging. INFO pmsp example2.p2.Class3=ERROR INFO logging INFO example2 INFO p1 p2 INFO INFO Class1 s1 INFO Class3 ERROR INFO Class2 Declared Inferred Planning and Managing Software Projects – Emanuele Della Valle
  14. 14. Example In the properties file:log4j.rootlogger=INFO INFO rootLoggerlog4j.logger.pmsp.logging. INFO pmsp example2.p2.Class3=ERRORlog4j.logger.pmsp.logging. INFO logging example2=DEBUG DEBUG example2 DEBUG p1 p2 DEBUG DEBUG Class1 s1 DEBUGClass3 ERROR DEBUG Class2 Declared Inferred Planning and Managing Software Projects – Emanuele Della Valle
  15. 15. Appenders Loggers can write on several output systems Each output system is defined by an appender There is a high number of available appenders in Log4J  Console, File, DB, network, etc.  Appenders are declared and configured in the properties file log4j.appenders.<name>=<appender class> log4j.appenders.<name>.<param1>=... log4j.appenders.<name>.<param2>=... Each logger is associated to one or more appenders  The assignment is managed through the logger tree (like the levels)Planning and Managing Software Projects – Emanuele Della Valle
  16. 16. Example#Rolling file appender: fileaplog4j.appender.fileap=org.apache.log4j.RollingFileAppenderlog4j.appender.fileap.File=log.xmllog4j.appender.fileap.MaxFileSize=100KB#Stdout appender: stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.rootLogger=INFO,stdoutlog4j.logger.psmp.logging.example2.p2.Class3=FATAL,fileap(Class3 is associated to both stdout and fileap appenders)More info is availabe in the Log4j documentation:http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AppenderSkeleton.html Planning and Managing Software Projects – Emanuele Della Valle
  17. 17. Layouts Layouts defines the format of the log lines Each appender is related to one layout log4j.appender.<name>.layout=<layout class> log4j.appender.<name>.layout.<param1>=... log4j.appender.<name>.layout.<param2>=... Some layouts built-in in Log4J are:  DateLayout  HTMLLayout  XMLLayout  PatternLayoutPlanning and Managing Software Projects – Emanuele Della Valle
  18. 18. Example#Rolling file appenderlog4j.appender.fileap=org.apache.log4j.RollingFileAppenderlog4j.appender.fileap.File=log.xmllog4j.appender.fileap.MaxFileSize=100KBlog4j.appender.fileap.layout= org.apache.log4j.xml.XMLLayout#Stdout appenderlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout= org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern= %d [%t] %-5p %c - %m%n Planning and Managing Software Projects – Emanuele Della Valle
  19. 19. Logging frameworks in Java Log4J is not the only available logging framework for Java Other notable logging systems are:  Java Util Logging: built-in in the Java framework, it can be used without including external libraries in the project. On the other hand, it has a complex level hierarchy and less features than Log4J  LogBack: aims to be the succesor of Log4j. This framework is faster than Log4J and overcomes some of its limitsPlanning and Managing Software Projects – Emanuele Della Valle
  20. 20. Logging façades It is considered a bad practice to create a strong dependance between the code and a specific logging framework The best practice is to use a logging façade that “wraps” the logging framework system The two most used systems in Java are  Apache Commons Logging (formerly Jakarta Commons Logging)  SLF4JPlanning and Managing Software Projects – Emanuele Della Valle
  21. 21. SLF4J SLF4J abstracts the logging concepts allowing users to decide what logging system use in their application SLF4J is “configured” at deploy time  Developer should import the right libraries into the project  Classpath must have at most one bridge libraryPlanning and Managing Software Projects – Emanuele Della Valle
  22. 22. SLF4JFrom http://www.slf4j.org/manual.html Planning and Managing Software Projects – Emanuele Della Valle
  23. 23. SLF4J In huge projects several logger framework could be used In this case SLF4J allows to use migration libraries  They bounds an existing logging framework into SLF4j commons-logging.jar From: http://www.slf4j.org/legacy.htmlPlanning and Managing Software Projects – Emanuele Della Valle
  24. 24. SLF4J In huge projects several logger framework could be used In this case SLF4J allows to use migration libraries  They bounds an existing logging framework into SLF4j From: http://www.slf4j.org/legacy.htmlPlanning and Managing Software Projects – Emanuele Della Valle
  25. 25. SLF4J In huge projects several logger framework could be used In this case SLF4J allows to use migration libraries  They bounds an existing logging framework into SLF4j From: http://www.slf4j.org/legacy.htmlPlanning and Managing Software Projects – Emanuele Della Valle
  26. 26. References Dont Use System.out.println! Use Log4j - http://www.vipan.com/htdocs/log4jhelp.html Apache Log4J manual http://logging.apache.org/log4j/1.2/manual.html SLF4J manual http://www.slf4j.org/manual.html Think again before adopting the commons-logging API http://articles.qos.ch/thinkAgain.html Logging, which framework to choose? Log4j, Commons Logging, LogBack, SLF4J http://mike.vanvendeloo.net/2011/05/06/logging- which-framework-to-choose-log4j-commons-logging- logback-slf4jPlanning and Managing Software Projects – Emanuele Della Valle

×