public final class Tracer extends Object
Tracers provide facilities to log trace messages. A tracer is bound to one or more loggers and is associated with a level - SEVERE, WARNING, INFO, CONFIG, FINE, FINER and FINEST. The level serves as a level based filter for trace messages being logged. Each trace message is associated with a level and the message is suppressed by the tracer in case the message level is less than the tracer level at the time the message is logged.
By default, a tracer is not bound to any logger. A tracer can be bound to
either loggers maintained by TraceLoggerFactory
or SLF4J loggers.
A user binds and unbinds loggers to and from a tracer via the bind(java.lang.Object)
and unbind(java.lang.String)
methods. The nv.trace.useSlf4j system property
determines whether the tracer gets loggers from SLF4j or TraceLoggerFactory.
A tracer is created with the Tracer.Level.INFO
level. A user can
change the tracer level either by directly specifying the new level or by
specifying a system property from which the new level is obtained. A user
requests to log a trace message via the log(java.lang.String, com.neeve.trace.Tracer.Level)
method. The method
compares the message level with the tracer's level and if greater or equal
dispatches the message to all the bound loggers. The messages will be
suppressed by the loggers in case the logger level is greater than the tracer
level.
The act of binding a tracer to an SLF4J logger will cause the tracer's level to be set using the level of the logger. Programmatic setting of the level will cause the level to be overriden. This allows for the trace level to be set using centralized SLF4J mechanisms and then optionally overriden using environment variables/system properties.
Modifier and Type | Class and Description |
---|---|
static class |
Tracer.Level
Defines the various trace levels.
|
Modifier and Type | Field and Description |
---|---|
boolean |
debug |
static Tracer.Level |
defaultLevel |
boolean |
fine |
boolean |
verbose |
Modifier and Type | Method and Description |
---|---|
void |
bind(String name)
Bind to a logger by name.
|
static Tracer |
create(String name,
Tracer.Level defaultLevel)
Create a tracer bound to the specified logger using a specified level
|
static Tracer |
create(Tracer.Level level)
Create a tracer using a specified level.
|
Tracer.Level |
getLevel()
Get the tracer level.
|
static Tracer.Level |
getLevel(org.slf4j.Logger logger)
Get a trace level from an SLF4J logger.
|
static Tracer.Level |
getLevel(Properties props,
String name)
Get a trace level from a property.
|
static Tracer.Level |
getLevel(Properties props,
String name,
Tracer.Level defLevel)
Get a trace level from a property.
|
static Tracer.Level |
getLevel(String str)
Get a trace level from a string.
|
boolean |
isEnabled(Tracer.Level level)
Tests whether the given trace level is enabled.
|
void |
log(String msg,
Tracer.Level level)
Log a trace message.
|
void |
setLevel(Properties props,
String name)
Set the tracer level from a property.
|
void |
setLevel(String level)
Set the tracer level.
|
void |
setLevel(Tracer.Level level)
Set the tracer level.
|
void |
setPrefix(String prefix)
Deprecated.
Trace prefix functionality is no longer supported for performance reasons.
|
void |
unbind(String name)
Unbind from a logger.
|
public static final Tracer.Level defaultLevel
public volatile boolean debug
public volatile boolean fine
public volatile boolean verbose
public static Tracer create(String name, Tracer.Level defaultLevel)
logger
- The name of the tracerdefaultLevel
- The default trace level. All trace messages greater than this
trace level will be suppressed. If the logger does not have a configured trace level
this default level will be used.public static Tracer create(Tracer.Level level)
level
- The tracer level. All trace messages greater than this
trace level will be suppressed.public static Tracer.Level getLevel(String str)
str
- The level string.
This method returns a trace level with name equal to the supplied string. The string to level mapping is done case insensitively.
IllegalArgumentException
- Thrown in case the supplied string
does not correspond to one of the defined levels.public static Tracer.Level getLevel(Properties props, String name, Tracer.Level defLevel)
props
- The property table to use.name
- The property name.public static Tracer.Level getLevel(Properties props, String name)
props
- The property table to use.name
- The property name.defaultLevel
is returned in case the property is not found
in the table or the property value does not correspond to a valid level.public static Tracer.Level getLevel(org.slf4j.Logger logger)
logger
- The logger to map the trace level from.public final void setLevel(Tracer.Level level)
level
- The level to setpublic final void setLevel(String level)
level
- The level to setpublic final void setLevel(Properties props, String name)
props
- The property table to use.name
- The property name.
This method sets the level of a trace object from a property value. The absence of the property in the provided table or an invalid level name in the property value leaves the trace level unchanged.
public final Tracer.Level getLevel()
public final boolean isEnabled(Tracer.Level level)
level
- The level to check.@Deprecated public final void setPrefix(String prefix)
prefix
- The prefix to set.public final void bind(String name)
name
- The name of the logger to bind to.public final void unbind(String name)
name
- The name of the logger to unbind from.public final void log(String msg, Tracer.Level level)
msg
- The message to loglevel
- The message level.
This method outputs a line of trace of a trace level if the level is greater than or equal to the trace level of this object.
Copyright © 2019 Neeve Research, LLC. All Rights Reserved.