Changes between Initial Version and Version 1 of LOGGING


Ignore:
Timestamp:
09/16/08 17:22:05 (17 years ago)
Author:
Tanya
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LOGGING

    v1 v1  
     1= Logging = 
     2For logging we use the log4j library.  [HTTP://logging.apache.org/log4j/1.2/index.html log4j] 
     3 
     4== Logging levels == 
     5log4j has several levels for logging. They allow users to sort information according to its significance. Levels can be used for easy management of the amount and verbosity of the output information. The library can be easily configured to show only messages not less than a given level. That's why it is important to identify well the level of the information you are about to log. 
     6 
     7Currently there are six logging levels: 
     8 
     9 * FATAL - It is used to notify of an error that surely leads to application aborting. 
     10{{{ 
     11public static void checkAssertions() { 
     12        Logger.getLogger(Main.class).fatal( 
     13                        "Assertions are disabled." 
     14                        + "Please enable them with the -ea option."); 
     15        throw new Error("Assertions are disabled." 
     16                        + "Please enable them with the -ea option."); 
     17} 
     18}}} 
     19 
     20 * ERROR - It is used when an operation fails to complete and is likely to cause the application to abort, but it may still survive. 
     21{{{ 
     22try { 
     23        bi = ImageIO.read(in); 
     24} catch (IOException e) { 
     25        logger.error("Cannot load icon",e); 
     26} 
     27}}} 
     28 
     29 * WARN - Used when a potentially harmful situation has occurred. Like in:  
     30{{{ 
     31 public final void removeListener(PropertyListener<T> listener) { 
     32       if(!getListereners().contains(listener)) { 
     33           logger().warn("Property.removeListener(): removing not registered!"); 
     34       } 
     35       assert getListereners().contains(listener); 
     36       getListereners().remove(listener); 
     37 } 
     38}}} 
     39 
     40 * INFO - Used to give information of major steps of the application life cycle. Like in: 
     41{{{ 
     42public PageView(final Page page) { 
     43                getBean().init(model(), page); 
     44                 
     45                logger().info("PageView created"); 
     46        } 
     47}}} 
     48 
     49 * DEBUG - Gives information about the step by step progress of the application. For example entering/leaving a method: 
     50{{{ 
     51private void badLocationSafeRun() { 
     52        logger().debug("Chain " + this + " starts flushing"); 
     53        try { 
     54                underFlow(); 
     55                overFlow(); 
     56        } catch (final BadLocationException e) { 
     57                throw new RuntimeException(e); 
     58        } 
     59        logger().debug("Chain " + this + " has been flushed"); 
     60} 
     61}}} 
     62 
     63 * TRACE - The most verbose of levels. Used to watch the progress of a method. Also for variable output during debugs 
     64{{{ 
     65private void overFlow() throws BadLocationException { 
     66        logger().debug("Overflowing starts"); 
     67        while (baseOverflows()) { 
     68                Element cur = this.base 
     69                                .getCharacterElement(getBaseLength() - 1); 
     70                int startOffset = cur.getStartOffset(); 
     71                int endOffset = cur.getEndOffset(); 
     72 
     73                logger().trace("Overflowing from " + startOffset + " to " + endOffset); 
     74 
     75                String text = getText(this.base, startOffset, endOffset) 
     76                                .substring(getBaseLength() - startOffset - 1, 
     77                                                getBaseLength() - startOffset); 
     78                ... 
     79                logger().trace("Following text was overflowed - " + text); 
     80        } 
     81} 
     82}}} 
     83 
     84== Logging in instance methods == 
     85It should be performed using a method that looks like: 
     86 
     87{{{ 
     88modifier Logger logger(){ 
     89                return Logger.getLogger(this.getClass()); 
     90        } 
     91}}} 
     92 
     93It is advisable that all mutable base classes implement such a method (with "protected" modifier) so that their successors can easily access the proper Logger for their class. 
     94 
     95Classes that have a logger method: 
     96 * !BaseProObject 
     97 * Property 
     98 * Change 
     99 
     100== Logging in class methods == 
     101The correct Logger is obtained like this 
     102{{{ 
     103public class Logic { 
     104... 
     105       Logger logger = Logger.getLogger(Logic.class); 
     106... 
     107} 
     108}}} 
     109 
     110Depending on the intensity of logging, the logger may be stored in a local variable( low intensity) or a class variable( used in several static methods). 
     111 
     112== Configuring log4j in Veda == 
     113 
     114Log4j configuration is stored in "log4j.properties" file. It is read at application startup. 
     115 
     116In order to change the lowest logged level edit the following line: 
     117{{{ 
     118log4j.rootLogger=all, ConsoleLogging 
     119}}} 
     120and replace "all" with the desired level (choose on from - off, fatal, error, warn, info, debug, trace). 
     121 
     122Note that the Properties library is configured separately here: 
     123{{{ 
     124log4j.logger.net.asteasolutions.veda.research.properties=all, ConsoleLogging 
     125}}} 
     126This reflects on all classes extending the Property class. 
     127 
     128Destination and format of logged messages can be changed by configuring the appropriate Appenders in "log4j.properties". 
     129 
     130== Logging overhead == 
     131 
     132 
     133In general log4j has been optimized in speed, but do take note that logging adds some performance overhead. 
     134 
     135 * Computing of the arguments of logger methods is done before checking whether this message will be logged at all.  
     136 * Also choosing whether to log or not takes some time.  
     137 * And do not forget that the actual formating and output of the messages is also time-consuming.