Package org.jcsp.net

Class Logger


  • public class Logger
    extends java.lang.Object

    Manages the output of diagnostic messages to one or more devices. Instances of this class represent a log output device to which diagnostic or error messages can be written. The instance can be tied to an actual device such as a file or the standard I/O streams provided by the System class.

    All messages must be signed by an application class and may also contain a severity level. The higher the number the less severe the problem. The output generated can be controlled by setting the maximum level that should be reported. Thus, essential output should be written at level 1. Less severe and verbose output should be written at higher levels depending on the granularity of information required when testing a program.

    The default logging level is 5. That is, for all classes only messages up to level 5 by default will be displayed.

    Messages from the infrastructure classes are written to the loggers Node.info and Node.err which are by default connected to System.out and System.in respectively.

    Alternative implementations of this class may instead write to a system event log or format the information in manners tailored to particular debugging environments.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Hashtable all  
      static java.lang.String DEFAULT_CLASS_NAME
      The default string if the class name is omitted.
      static int DEFAULT_LOGGING_LEVEL
      The default logging level (currently 5)
      private java.lang.String lastClass  
      private java.util.Hashtable levels  
      private boolean levelsCached  
      static int MAX_LOGGING
      The logging level for really important messages.
      private java.lang.String name  
      private java.io.PrintStream output  
    • Constructor Summary

      Constructors 
      Constructor Description
      Logger​(java.lang.String name, java.lang.String defaultDevice)
      Creates a new Logger with a given name.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int findMaxLevel​(java.lang.Class clazz)  
      static Logger getLogger​(java.lang.String name)
      Returns a named logger within the system.
      void log​(int level, java.lang.Object message)
      Log a message at a specific level with the default class name.
      void log​(java.lang.Class clazz, int level, java.lang.Object message)
      Log a message at the specified level with the specific class.
      void log​(java.lang.Class clazz, java.lang.Object message)
      Log a message with the specific class.
      void log​(java.lang.Object message)
      Log a message at the default level with the default class name.
      void log​(java.lang.Object object, int level, java.lang.Object message)
      Log a message at the specified level with the class of the given object.
      void log​(java.lang.Object object, java.lang.Object message)
      Log a message with the specific class.
      void log​(java.lang.String className, int level, java.lang.Object message)
      Log a message at the specified level with the specific class name.
      void log​(java.lang.String className, java.lang.Object message)
      Log a message with the specific class.
      private void logImpl​(java.lang.String className, int level, java.lang.Object message)  
      void setDevice​(java.lang.String device)
      Sets the current output device for this logger.
      void setLevel​(java.lang.String clazz, int level)
      Sets the current logging level for a given class (and its subclasses).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAX_LOGGING

        public static final int MAX_LOGGING
        The logging level for really important messages.
        See Also:
        Constant Field Values
      • DEFAULT_LOGGING_LEVEL

        public static final int DEFAULT_LOGGING_LEVEL
        The default logging level (currently 5)
        See Also:
        Constant Field Values
      • DEFAULT_CLASS_NAME

        public static final java.lang.String DEFAULT_CLASS_NAME
        The default string if the class name is omitted.
        See Also:
        Constant Field Values
      • name

        private java.lang.String name
      • output

        private java.io.PrintStream output
      • lastClass

        private java.lang.String lastClass
      • levels

        private java.util.Hashtable levels
      • levelsCached

        private boolean levelsCached
      • all

        private static final java.util.Hashtable all
    • Constructor Detail

      • Logger

        public Logger​(java.lang.String name,
                      java.lang.String defaultDevice)

        Creates a new Logger with a given name. The name assigned is arbitrary but allows the logger to be uniquely identified by name. For example instead of assigning a static reference somewhere to always refer to a logger, one can be named and resolved dynamically:

         public static void main (String[] args) {
           new Logger ("errors", "stderr");
         }
         

        This will allocate a logger with the name errors connected to System.err. Elsewhere in the program one can write:

         Logger.getLogger ("errors")
         

        To obtain a reference to this logger.

        The default device can take one of three reserved values:

        • null - no output device; all logged events are discarded.
        • stdout - write to System.out.
        • stderr - write to System.err.

        If none of these values match it is assumed to be a filename.

        Parameters:
        name - the system unique name of the logger.
        defaultDevice - the output device to use.
    • Method Detail

      • getLogger

        public static Logger getLogger​(java.lang.String name)

        Returns a named logger within the system. Refer to the constructor for an example of its use.

        Parameters:
        name - the name of the logger
        Returns:
        the logger
        Throws:
        Logger.InvalidLoggerException - if the logger specified doesn't exist.
      • setDevice

        public void setDevice​(java.lang.String device)

        Sets the current output device for this logger. For example, to suppress infrastructure messages:

           Node.info.setDevice (null);
         
        Parameters:
        device - the new device to use.
      • setLevel

        public void setLevel​(java.lang.String clazz,
                             int level)

        Sets the current logging level for a given class (and its subclasses). Only messages generated by that class (or its subclasses) with a lesser level will be output.

        Parameters:
        clazz - the name of the class.
        level - the maximum level to display.
      • log

        public void log​(java.lang.Object message)
        Log a message at the default level with the default class name.
        Parameters:
        message - the message to output.
      • log

        public void log​(int level,
                        java.lang.Object message)
        Log a message at a specific level with the default class name.
        Parameters:
        level - the logging level.
        message - the message.
      • logImpl

        private void logImpl​(java.lang.String className,
                             int level,
                             java.lang.Object message)
      • log

        public void log​(java.lang.String className,
                        int level,
                        java.lang.Object message)
        Log a message at the specified level with the specific class name. Note that this does not have to match a real class name so could be used to describe the subsystem generating the message more meaningfully.
        Parameters:
        className - the class or component name.
        level - the logging level.
        message - the message.
      • findMaxLevel

        private int findMaxLevel​(java.lang.Class clazz)
      • log

        public void log​(java.lang.Class clazz,
                        int level,
                        java.lang.Object message)
        Log a message at the specified level with the specific class.
        Parameters:
        class - the class generating the message.
        level - the logging level.
        message - the message.
      • log

        public void log​(java.lang.Object object,
                        int level,
                        java.lang.Object message)
        Log a message at the specified level with the class of the given object.
        Parameters:
        class - the object whose class has generated the message.
        level - the logging level.
        message - the message.
      • log

        public void log​(java.lang.String className,
                        java.lang.Object message)
        Log a message with the specific class.
        Parameters:
        class - the class generating the message.
        message - the message.
      • log

        public void log​(java.lang.Class clazz,
                        java.lang.Object message)
        Log a message with the specific class.
        Parameters:
        class - the class generating the message.
        message - the message.
      • log

        public void log​(java.lang.Object object,
                        java.lang.Object message)
        Log a message with the specific class.
        Parameters:
        class - the class generating the message.
        message - the message.