|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jgroups.log.Trace
Provides timestamped logging to files, streams, writers, and sockets at differing levels of verbosity. Log outputs are associated with a module name (see this package's description) and a trace level.
Since tracing statements create a lot of strings, even when tracing is disabled, a trace statement should
always be conditional, e.g. if(Trace.trace) Trace.info("UDP.run()", "bla")
. Thus, if tracing
is disabled, the strings won't be created. For trace statements that should always be printed,
we don't need to make the trace statement conditional. Note that, if trace statements should be removed in
the production code, we can use the Trace.debug flag, e.g. if(Trace.debug) Trace(...)
. If
Trace.debug is set to false when the code is compiled, the entire statement will not be added to the
generated classfile.
A simple example of using Trace
:
// Send all WARN and higher messages to /tmp/logfile for MyClass Trace.setOutput("MyClass", Trace.WARN, "/tmp/logfile"); // Send all messages to /tmp/AnotherClass_DEBUG for AnotherClass Trace.setOutput("AnotherClass", Trace.DEBUG, "/tmp"); // Printed to /tmp/logfile because FATAL > WARN Trace.println("MyClass", Trace.FATAL, "help!"); // Not printed because DEBUG < WARN (the current level for MyClass) Trace.println("MyClass", Trace.DEBUG, "blah, blah"); // Printed to /tmp/AnotherClass_DEBUG because of the // "module inheritance" rules (see package overview). Trace.println("AnotherClass.myMethod()", Trace.DEBUG, "bletch"); // Close the outputs separately. Could also call Trace.closeAllOutputs() Trace.closeOutput("MyClass"); Trace.closeOutput("AnotherClass");
For a more complete discussion of module names and the rules used to find the output associated with a module, see the Description section of the overview for this package.
Field Summary | |
static boolean |
copy
Used to determine whether to copy messages (copy=true) in retransmission tables, or whether to use references (copy=false). |
static boolean |
debug
Allows for conditional compilation, e.g. |
static int |
DEBUG
The trace level for all messages. |
protected static java.lang.String |
DEFAULT_OUTPUT
|
protected static java.lang.String |
DEFAULT_OUTPUT_FILE_NAME
The file name used when setting a new default output and when the file name specified is a directory. |
protected static java.lang.String |
DEFAULT_OUTPUT_STMT
|
protected static Tracer |
defaultTracer
The default tracer to use when a message is not destined for any particular output. |
static int |
ERROR
The trace level for all ERROR and FATAL messages. |
static int |
FATAL
The trace level for only FATAL messages. |
protected static java.lang.String |
FILE_SEPARATOR
The character used to denote directories (e.g. |
protected static java.lang.String |
identifier
Used to e.g. |
static int |
INFO
The trace level for all messages except DEBUG and TEST. |
protected static java.lang.String |
NEWLINE
The newline string used in println . |
protected static java.lang.String |
STDERR_STMT
|
protected static Tracer[] |
STDERRS
We initially create Tracers for each STDERR level |
protected static java.lang.String |
STDOUT_STMT
|
protected static Tracer[] |
STDOUTS
We initially create Tracers for each STDOUT level |
static int |
TEST
The trace level for all messages except DEBUG. |
protected static java.lang.String |
TIMESTAMP_FORMAT_STMT
|
static boolean |
trace
Users of Trace can check this flag to determine whether to trace. |
protected static java.lang.String |
TRACE_STMT
|
protected static java.util.HashMap |
TRACERS
Maps module names to Tracer s. |
static int |
WARN
The trace level for all WARN, ERROR, and FATAL messages. |
Method Summary | |
static void |
addTracer(java.lang.String module,
Tracer tracer)
Associates the specified module name with a Tracer . |
protected static boolean |
bogusModuleName(java.lang.String module)
Returns true if the specified module name is illegal. |
static void |
closeAllOutputs()
Closes all outputs for all modules. |
static void |
closeDefaultOutput()
Closes the current default output. |
static void |
closeOutput(java.lang.String module)
Closes the output for the specified module. |
static void |
debug(java.lang.String module,
java.lang.String message)
Helper method. |
static void |
error(java.lang.String module,
java.lang.String message)
Helper method. |
static void |
fatal(java.lang.String module,
java.lang.String message)
Helper method. |
protected static Tracer |
findTracerFor(java.lang.String module)
Returns the Tracer to be used for output for the specified
module name. |
static void |
flushAllOutputs()
Flushes all output for all modules. |
static void |
flushOutput(java.lang.String module)
Flushes all output for the specified module. |
static void |
flushStderrs()
|
static void |
flushStdouts()
|
protected static Tracer |
getDefaultTracer()
Returns the default Tracer to be used when no other
appropriate one is found. |
static java.lang.String |
getStackTrace(java.lang.Throwable x)
Converts an exception stack trace into a java.lang.String |
static Tracer |
getStderrTracer(int level)
|
static Tracer |
getStdoutTracer(int level)
|
static void |
info(java.lang.String module,
java.lang.String message)
Helper method. |
static void |
init()
Same as init(String fname), but looks in user's home directory then in CLASSPATH for jgroups.properties. |
static void |
init(java.lang.String fname)
|
protected static Tracer |
lookup(java.lang.String module)
Returns the Tracer that matches the specified module
name. |
protected static java.lang.String |
makeOutputFileName(java.lang.String module,
int level)
Returns a file name created from a module name and trace level. |
protected static java.lang.String |
moduleLookupName(java.lang.String module)
Returns the string to be used to store and look up the specified module. |
protected static void |
parse(java.lang.String key,
java.lang.String val)
|
protected static void |
parseDefaultOutput(java.lang.String val)
|
protected static void |
parseTimestampFormat(java.lang.String val)
|
protected static void |
parseTrace(java.lang.String val)
Format is |
static void |
print(java.lang.String module,
int level,
java.lang.String message)
Finds the appropriate output for the specified module and sends message to it. |
static void |
print(java.lang.String module,
int level,
java.lang.String identifier,
java.lang.String message)
Prints the identifier (e.g. |
static void |
println(java.lang.String module,
int level,
java.lang.String message)
Appends a line separator to message and calls
print . |
protected static void |
removeTracer(java.lang.String module)
Removes the Tracer for the specified module. |
static void |
restoreDefaultOutput()
Sets the default output back to its original value, STDOUT . |
static void |
setAutoFlush(java.lang.String module,
boolean auto)
Sets the auto-flush flag for the specified module's output. |
static void |
setAutoFlushAll(boolean auto)
Sets the auto-flush flag for all outputs. |
static void |
setAutoFlushDefault(boolean auto)
Sets the auto-flush flag for the current default output. |
static void |
setAutoFlushForAllStderrs(boolean auto)
|
static void |
setAutoFlushForAllStdouts(boolean auto)
|
static void |
setDefaultOutput(java.net.InetAddress addr,
int port)
Sets the default output to the socket at addr on
port . |
static void |
setDefaultOutput(int level,
java.net.InetAddress addr,
int port)
Sets the default output to the socket at addr on
port at the specified trace level. |
static void |
setDefaultOutput(int level,
java.io.PrintStream outputStream)
Sets the default output to the specified PrintStream and
trace level. |
static void |
setDefaultOutput(int level,
java.io.PrintWriter writer)
Sets the default output to the specified PrintWriter and
trace level. |
static void |
setDefaultOutput(int level,
java.lang.String fileName)
Sets the default output to the specified file and trace level. |
static void |
setDefaultOutput(int level,
java.lang.String host,
int port)
Sets the default output to the socket at host on
port with the given trace level. |
static void |
setDefaultOutput(java.io.PrintStream outputStream)
Sets the default output to the specified PrintStream . |
static void |
setDefaultOutput(java.io.PrintWriter writer)
Sets the default output to the specified PrintWriter . |
static void |
setDefaultOutput(java.lang.String fileName)
Sets the default output to the specified file. |
static void |
setDefaultOutput(java.lang.String host,
int port)
Sets the default output to the socket at host on
port . |
static void |
setIdentifier(java.lang.String id)
|
static void |
setOutput(java.lang.String module,
int level)
Changes the trace level for module . |
static void |
setOutput(java.lang.String module,
int level,
java.net.InetAddress addr,
int port)
Sends a module's output to the socket at addr on
port . |
static void |
setOutput(java.lang.String module,
int level,
java.io.PrintStream outputStream)
Sends a module's output to the specified PrintStream . |
static void |
setOutput(java.lang.String module,
int level,
java.io.PrintWriter writer)
Sends a module's output to the specified PrintWriter . |
static void |
setOutput(java.lang.String module,
int level,
java.lang.String fileName)
Sends a module's output to the specified file. |
static void |
setOutput(java.lang.String module,
int level,
java.lang.String host,
int port)
Sends a module's output to the socket at host on
port . |
static void |
setTimestampFormat(java.lang.String format)
Set the timestamp format. |
static void |
setTrace(boolean t)
|
protected static int |
string2Level(java.lang.String l)
|
static void |
test(java.lang.String module,
java.lang.String message)
Helper method. |
static void |
warn(java.lang.String module,
java.lang.String message)
Helper method. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static boolean trace
if(Trace.trace) Trace.info("UDP.run()", "sending message");
public static final boolean debug
public static final boolean copy
public static final int DEBUG
public static final int TEST
public static final int INFO
public static final int WARN
public static final int ERROR
public static final int FATAL
protected static final java.lang.String DEFAULT_OUTPUT_FILE_NAME
protected static final java.lang.String TRACE_STMT
protected static final java.lang.String DEFAULT_OUTPUT_STMT
protected static final java.lang.String TIMESTAMP_FORMAT_STMT
protected static final java.lang.String STDOUT_STMT
protected static final java.lang.String STDERR_STMT
protected static final java.lang.String DEFAULT_OUTPUT
protected static final java.util.HashMap TRACERS
Tracer
s.
protected static final Tracer[] STDOUTS
protected static final Tracer[] STDERRS
protected static Tracer defaultTracer
null
; defaults to
null
.
protected static final java.lang.String NEWLINE
println
. This is the system
property line.separator
, and is not necessarily the
newline character ('\n').
println(java.lang.String, int, java.lang.String)
protected static final java.lang.String FILE_SEPARATOR
protected static java.lang.String identifier
Method Detail |
public static void init(java.lang.String fname) throws java.io.IOException, java.lang.SecurityException
fname
- Fully qualified name of property file. If it is a directory, "/jgroups.properties"
will be appended to the directory name.
java.io.IOException
java.lang.SecurityException
public static void init()
public static Tracer getStdoutTracer(int level)
public static void flushStdouts()
public static void setAutoFlushForAllStdouts(boolean auto)
public static Tracer getStderrTracer(int level)
public static void flushStderrs()
public static void setAutoFlushForAllStderrs(boolean auto)
public static void setIdentifier(java.lang.String id)
public static void setTrace(boolean t)
public static void setOutput(java.lang.String module, int level)
module
. The output destination
is not changed. No other module (either higher or lower in the module
"inheritance chain") is affected. If this module has no output
associated with it (and thus any output would go to the current
default output), nothing happens. If the module name is not well-formed,
nothing happens.
module
- a module namelevel
- a trace levelpublic static void setOutput(java.lang.String module, int level, java.lang.String fileName) throws java.io.IOException
print
or println
specifies
a trace level >= level
.
If fileName
is the name of a directory, a file name will
be generated using module
and level
(see
module
- a module namelevel
- a trace levelfileName
- a file or directory name
java.io.IOException
- when a FileWriter
can not be createdpublic static void setOutput(java.lang.String module, int level, java.io.PrintStream outputStream)
PrintStream
.
Output is only sent when a call to print
or
println
specifies a trace level >= level
.
The most common values of outputStream
will be
System.out
or System.err
.
In fact, that is so likely that we keep pre-generated outputs around for
those two and return one when outputStream
is one of those
two values.
module
- a module namelevel
- a trace leveloutputStream
- a PrintStream
public static void setOutput(java.lang.String module, int level, java.io.PrintWriter writer)
PrintWriter
.
Output is only sent when a call to print
or
println
specifies a trace level >= level
.
module
- a module namelevel
- a trace levelwriter
- a PrintWriter
public static void setOutput(java.lang.String module, int level, java.lang.String host, int port) throws java.net.UnknownHostException, java.io.IOException
host
on
port
. As with the Socket
class, if
port
is zero then the first available port will be used.
Output is only sent when a call to print
or
println
specifies a trace level >= level
.
module
- a module namelevel
- a trace levelhost
- the name of a host machineport
- a port number
java.net.UnknownHostException
- if host
can not be resolved
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void setOutput(java.lang.String module, int level, java.net.InetAddress addr, int port) throws java.io.IOException
addr
on
port
. As with the Socket
class, if
port
is zero then the first available port will be used.
Output is only sent when a call to print
or
println
specifies a trace level >= level
.
module
- a module namelevel
- a trace leveladdr
- an InetAddress
port
- a port number
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void closeOutput(java.lang.String module)
STDOUT
, STDERR
, or the default output.
module
- a module namepublic static void closeAllOutputs()
public static void flushOutput(java.lang.String module)
module
- a module namepublic static void flushAllOutputs()
public static void setAutoFlush(java.lang.String module, boolean auto)
module
- a module nameauto
- if true
auto-flushing is turned onpublic static void setAutoFlushAll(boolean auto)
The auto-flush value for STDOUT
and STDERR
are also set.
auto
- if true
auto-flushing is turned onpublic static void setAutoFlushDefault(boolean auto)
auto
- if true
auto-flushing is turned onpublic static void print(java.lang.String module, int level, java.lang.String message)
message
to it. When looking for which output to use,
the following set of rules are used. If no output is found (the
returned output is null
), then nothing is printed.
null
).null
).
module
- a module namelevel
- a trace levelmessage
- the string to be outputpublic static void print(java.lang.String module, int level, java.lang.String identifier, java.lang.String message)
public static void println(java.lang.String module, int level, java.lang.String message)
message
and calls
print
. The system property "line.separator" is used;
this is not necessarily a single newline character ('\n').
module
- a module namelevel
- a trace levelmessage
- the string to be outputprint(java.lang.String, int, java.lang.String)
public static void debug(java.lang.String module, java.lang.String message)
public static void test(java.lang.String module, java.lang.String message)
public static void info(java.lang.String module, java.lang.String message)
public static void warn(java.lang.String module, java.lang.String message)
public static void error(java.lang.String module, java.lang.String message)
public static void fatal(java.lang.String module, java.lang.String message)
public static void setTimestampFormat(java.lang.String format)
format
string is that used by SimpleDateFormat
. If
format
is null
, then the ISO 8601 date
format ("CCYY-MM-DDThh:mm:ss,s") is used.
protected static Tracer getDefaultTracer()
Tracer
to be used when no other
appropriate one is found. May be null
Tracer
(defaultTracer
);
may be null
Tracer
public static void restoreDefaultOutput()
STDOUT
.
public static void closeDefaultOutput()
public static void setDefaultOutput(int level, java.lang.String fileName) throws java.io.IOException
fileName
is the name of a directory, the file name
DEFAULT_OUTPUT_FILE_NAME
("default.out") is used.
If the file does not exist, it is created. If the file does exist, the file is opened for appending.
fileName
- a file or directory name
java.io.IOException
- when a FileWriter
can not be createdpublic static void setDefaultOutput(java.lang.String fileName) throws java.io.IOException
fileName
is the name of a directory, the file name
DEFAULT_OUTPUT_FILE_NAME
("default.out") is used.
If the file does not exist, it is created. If the file does exist, the file is opened for appending.
fileName
- a file or directory name
java.io.IOException
- when a FileWriter
can not be createdpublic static void setDefaultOutput(int level, java.io.PrintStream outputStream)
PrintStream
and
trace level.
The most common values of outputStream
will be
System.out
or System.err
.
In fact, that is so likely that we keep pre-generated outputs around for
those two and return one when outputStream
is one of those
two values.
outputStream
- a PrintStream
public static void setDefaultOutput(java.io.PrintStream outputStream)
PrintStream
.
The most common values of outputStream
will be
System.out
or System.err
.
In fact, that is so likely that we keep pre-generated outputs around for
those two and return one when outputStream
is one of those
two values.
outputStream
- a PrintStream
public static void setDefaultOutput(int level, java.io.PrintWriter writer)
PrintWriter
and
trace level.
writer
- a PrintWriter
public static void setDefaultOutput(java.io.PrintWriter writer)
PrintWriter
.
writer
- a PrintWriter
public static void setDefaultOutput(int level, java.lang.String host, int port) throws java.net.UnknownHostException, java.io.IOException
host
on
port
with the given trace level. As with the
Socket
class, if port
is zero then the first
available port will be used.
host
- the name of a host machineport
- a port number
java.net.UnknownHostException
- if host
can not be resolved
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void setDefaultOutput(java.lang.String host, int port) throws java.net.UnknownHostException, java.io.IOException
host
on
port
. As with the Socket
class, if
port
is zero then the first available port will be used.
host
- the name of a host machineport
- a port number
java.net.UnknownHostException
- if host
can not be resolved
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void setDefaultOutput(int level, java.net.InetAddress addr, int port) throws java.io.IOException
addr
on
port
at the specified trace level. As with the
Socket
class, if port
is zero then the first
available port will be used.
addr
- an InetAddress
port
- a port number
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void setDefaultOutput(java.net.InetAddress addr, int port) throws java.io.IOException
addr
on
port
. As with the Socket
class, if
port
is zero then the first available port will be used.
addr
- an InetAddress
port
- a port number
java.io.IOException
- if an I/O error occurs when creating a socketSocket
public static void addTracer(java.lang.String module, Tracer tracer)
Tracer
.
module
- a module nametracer
- a Tracer
Tracer
protected static void removeTracer(java.lang.String module)
Tracer
for the specified module.
Does not close or otherwise modify the Tracer
.
module
- a module nameTracer
protected static Tracer lookup(java.lang.String module)
Tracer
that matches the specified module
name. No rule matching is done; if the module name as returned
by moduleLookupName
is not found, null
is returned. This method is called from setOutput
but
not from print
.
module
- a module name
Tracer
associated with this module, or
null
if not foundTracer
,
findTracerFor(java.lang.String)
protected static java.lang.String moduleLookupName(java.lang.String module)
module
with everything
after first parenthesis chopped off. The first parenthesis is kept.
This method is called from setOutput
but not from
print
.
Keeping the parenthesis helps us distinguish between modules that have method names in them and fully qualified class names. As an admittedly artificial example, the module named "Foo.Method()" would be distinguishable from a class whose name is Method and full path is "Foo.Method".
module
- a module name
Tracer
lookup
and storageTracer
protected static Tracer findTracerFor(java.lang.String module)
Tracer
to be used for output for the specified
module name. Performs a series of searches described in the comment for
print
. May return null
, if no match is found
and getDefaultTracer
returns null
. This method
is called from print
, and is not called from
setOutput
.
module
- a module name
Tracer
or null
if there are no
matches and the default tracer is null
Tracer
,
lookup(java.lang.String)
,
getDefaultTracer()
protected static java.lang.String makeOutputFileName(java.lang.String module, int level)
module
- a module namelevel
- a trace level (DEBUG, FATAL, etc.)
protected static boolean bogusModuleName(java.lang.String module)
true
if the specified module name is illegal.
Currently only checks to see that if the name contains '(' that there
is a '.' before it.
module
- a module name
true
if the specified module name is illegal,
else returns false
protected static void parse(java.lang.String key, java.lang.String val)
protected static void parseTrace(java.lang.String val)
protected static void parseDefaultOutput(java.lang.String val)
protected static void parseTimestampFormat(java.lang.String val)
protected static int string2Level(java.lang.String l)
public static java.lang.String getStackTrace(java.lang.Throwable x)
x
- an exception
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |