Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: konspire/common/log/MultiThreadLog.java


1   package konspire.common.log;
2   
3   import java.util.*;
4   import java.text.*;
5   
6   
7   /**
8    * <code>MultiThreadLog</code> is an extension of the 
9    * <code>Log</code> class to add information to each log
10   * entry specifying the thread that generated the entry.  In a
11   * multi-threaded application, a particular log file may be
12   * intermixed with a number of entries from a number of
13   * threads.  To make sense of what is going on in a particular
14   * thread, this type of log will prepend all entries with the name
15   * of the thread, so you can "grep" out a particular thread of
16   * interest.<p>
17   *
18   * Like the <code>Log</code> class, this class could be extended to do 
19   * application-specific behavior,
20   * or better yet you could create a class "wrapper" like <code>AppLog</code>
21   * to make instances well-known Singletons with handy static/class methods.
22   *
23   * @author Todd Lauinger
24   * @version $Revision: 1.3 $
25   *
26   * @see Log
27   * @see AppLog
28   */
29  public class MultiThreadLog extends Log {
30  
31      // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32      // Constructor section
33  
34    /**
35     * Public constructor to register a new <code>MultiThreadLog</code>
36     * with a particular name.
37     * The name of the log is very important.  It is used as a key into
38     * the log properties file to find settings for how the log is to
39     * be opened.
40     */
41    public MultiThreadLog(String name) {
42        super(name);
43    }
44  
45    /**
46     * Adds a message string to the log if the log's level is
47     * currently set at or above the loggingLevel passed in.
48     * Prepends the current thread name before the log entry.
49     *
50     * @param logString The string to write to all logging mechanisms
51     *
52     * @param loggingLevel The level at which to log the string.
53     */
54    synchronized public void logString(String logString, int loggingLevel) {
55  
56      // Log the message if higher or equal to current logging level.
57      // Note, the subclass does this check as well, but for performance
58      // we will catch it here so we don't have to look up the
59      // current thread information if we don't need to.
60      if (loggingLevel <= currentLoggingLevel) {
61          super.logString(Thread.currentThread().getName() + 
62                  logFieldSeparator + logString, loggingLevel) ;
63      }
64    }
65  }