Skip to main content

Posts

Showing posts with the label logging

Deciding what to log

In this section we will discuss what to log and the various levels at which we can generate log statements. Even though I have mentioned that we should generate log statements when control enters and exits a method, this is a rather controversial point. Especially generating log statements on exiting; because a method could have multiple exit routes. What do you think? I would say, if in doubt - do NOT generate logs when exiting a method. Logs that show control has entered a method do have value in my personal opinion, however you can leave those out as well if you do not see much value in them. Let me first draw your attention to the output from last page. 0 [main] DEBUG LoggerTest - Entering main 453 [main] DEBUG LoggerTest - Exiting main What is DEBUG? As I have explained, we should log several events from our program as it executes. But all events are not equally important. Hence, Log4J supports six levels for logging, where each level is appropriate for a...

Log4j's configuration file

Log4J configuration file # Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c - %m%n Program 01   import  org.apache.log4j.*; 02   public class  LoggerTest  { 03   04      static  String category = LoggerTest. class .getName () ; 05      static  Logger logger = Logger.getLogger ( category ) ; 06      static  { 07        PropertyConfigurator.configure ( "test.properties" ) ; 08      } 09   10      public static  void  main ( String args []) { 11      ...

Initializing Log4j

  In the next post I will describe a log4j configuration file.  Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net     Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco .  

Log4j an introduction

Log4J is a logging library that helps us create log statements that are way more powerful than System.out.println("..."). Log4J messages can be formatted with a timestamp, source, and many other things. The Log4J system is initialzed using a property file, which determines the destination, and formatting of the log messages. Log messages can be selectively turned on/off by modifying the property file.   Here is Log4J's documentation page. It has a very good introduction and a wiki .  Discuss this post in the learning forum . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net     Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco .  

Generating log statements from your application

In most software applications, control passes through complex code while users use the system. For large applications it is very important to keep track of where control is passing through in the application, if any exceptions are being thrown and what actions are being invoked by the user. Keepingtrack of these things help in maintaining an audit log and also in debugging the system if it fails at any point. In this section we will understand why we should not use System.out.println() and we will also understand how to use a very good open source alternative called Log4J . When you write small programs and run them, how do you keep track of where your program is passing control? How do you debug your program if it does'nt work properly? Most likely the answer is: System.out.println("some message"); This is the most common way to debug and track a small program, but it is also a flawed approach. It is cumbersome to print the source of the message. It is cumbersome to prin...