1 / 23

Logging in Java applications

Logging in Java applications. Sean C. Sullivan July 23, 2002 Portland Java Users Group. In the beginning…. public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello world!”); } }. Real-world applications are complex.

brac
Download Presentation

Logging in Java applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logging in Java applications Sean C. Sullivan July 23, 2002 Portland Java Users Group

  2. In the beginning… public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello world!”); } }

  3. Real-world applications are complex • applications are multi-threaded and multi-user • multiple web applications per application server • each web application may communicate with one-or-more backend systems

  4. Why logging? • Logs provide precise context about a run of the application • Logs can be saved to a persistent medium to be studied at a later time

  5. When to use logging • In your development phase: • logging can help you debug the code • In your production environment: • helps you troubleshoot problems

  6. Hello Log4j import org.apache.log4j.*; public class HelloLog4j { private static Logger logger = Logger.getLogger(HelloLog4j.class); public static void main(String[] args) { BasicConfigurator.configure(); logger.debug(“In the main method"); logger.info("What a beautiful day."); logger.error(“This is an error message.”); } }

  7. Output from HelloLog4j 0 [main] DEBUG HelloLog4j - In the main method 0 [main] INFO HelloLog4j - What a beautiful day. 10 [main] ERROR HelloLog4j - This is an error message.

  8. Hello java.util.logging import java.util.logging.*; public class HelloJDKLogging { private static Logger logger = Logger.getLogger("com. foo.HelloJDKLogging"); public static void main(String argv[]) { logger.setLevel(Level.ALL); logger.fine(“Program started"); logger.info(“This app uses java.util.logging”); logger.warning(“Oops, I did it again"); } }

  9. Output from HelloJDKLogging Jul 23, 2002 12:12:43 PM HelloJDKLogging main INFO: This app uses java.util.logging Jul 23, 2002 12:12:43 PM HelloJDKLogging main WARNING: Oops, I did it again

  10. Logging concepts • named loggers • levels • destination for log messages • message log format

  11. Log4j features • logging via JMS • logging to a database via JDBC • logging to Windows NT event log • logging to Unix syslog • GUI log viewer (“chainsaw”)

  12. Comparison: log4j & java.util.logging

  13. org.apache.log4j.Level Order: DEBUG < INFO < WARN < ERROR < FATAL Other levels: • Level.ALL • Level.OFF

  14. java.util.Logging.Level Other levels: • Level.ALL • Level.OFF Order: FINEST < FINER < FINE < CONFIG < INFO < WARNING < SEVERE

  15. Named loggers in Log4j • Logger “com.foo” is a parent of “com.foo.Bar” • children inherit a Level value from their parent

  16. Log4j Appender’s • AsyncAppender • ConsoleAppender • DailyRollingFileAppender • JMSAppender • NTEventLogAppender • NullAppender • RollingFileAppender • SMTPAppender • SocketAppender • SyslogAppender

  17. java.util.logging Handlers • StreamHandler • ConsoleHandler • FileHandler • SocketHandler • MemoryHandler

  18. java.util.logging Formatters • SimpleFormatter • XMLFormatter

  19. Log4j log viewer: Chainsaw

  20. Logging: Best practices • use the appropriate message level • roll your log files daily / weekly • review your error log on a daily basis

  21. Logging: Worst practices • System.out.println / System.err.println • logging passwords to a log file • logging informational messages to STDERR • logging a message for every single HTTP request • multiple applications sending log messages to a single log file • ignoring error messages that appear in your application error log • misleading log messages

  22. Related projects… • Jakarta Commons Logging • http://jakarta.apache.org/commons/ • Protomatter Syslog library • http://protomatter.sourceforge.net/

  23. Summary • Stop using System.out.println • Start using Log4j or java.util.logging

More Related