Class Logger
- java.lang.Object
-
- org.jcsp.net.Logger
-
public class Logger extends 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
andNode.err
which are by default connected toSystem.out
andSystem.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.
- Author:
- Quickstone Technologies Limited
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_CLASS_NAME
The default string if the class name is omitted.static int
DEFAULT_LOGGING_LEVEL
The default logging level (currently 5)static int
MAX_LOGGING
The logging level for really important messages.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Logger
getLogger(String name)
Returns a named logger within the system.void
log(int level, Object message)
Log a message at a specific level with the default class name.void
log(Class clazz, int level, Object message)
Log a message at the specified level with the specific class.void
log(Class clazz, Object message)
Log a message with the specific class.void
log(Object message)
Log a message at the default level with the default class name.void
log(Object object, int level, Object message)
Log a message at the specified level with the class of the given object.void
log(Object object, Object message)
Log a message with the specific class.void
log(String className, int level, Object message)
Log a message at the specified level with the specific class name.void
log(String className, Object message)
Log a message with the specific class.void
setDevice(String device)
Sets the current output device for this logger.void
setLevel(String clazz, int level)
Sets the current logging level for a given class (and its subclasses).
-
-
-
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 String DEFAULT_CLASS_NAME
The default string if the class name is omitted.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Logger
public Logger(String name, 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 toSystem.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(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:
org.jcsp.net.Logger.InvalidLoggerException
- if the logger specified doesn't exist.
-
setDevice
public void setDevice(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(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(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, Object message)
Log a message at a specific level with the default class name.- Parameters:
level
- the logging level.message
- the message.
-
log
public void log(String className, int level, 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.
-
log
public void log(Class clazz, int level, 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(Object object, int level, 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(String className, Object message)
Log a message with the specific class.- Parameters:
class
- the class generating the message.message
- the message.
-
log
public void log(Class clazz, Object message)
Log a message with the specific class.- Parameters:
class
- the class generating the message.message
- the message.
-
-