public abstract class LogContext extends AbstractContext
Asynchronous logging context integrated with the OSGi logging framework.
The logging back-end, or how the log entries are displayed, stored, or
processed is unspecified but always performed asynchronously.
When running outside OSGi, log messages are sent to System.out
.
Message formatting itself is always performed synchronously using the
current TextContext
.
Logging contexts support automatic prefixing/suffixing of any information relevant to the user/developer (thread info, user id, and so on).
void run() { LogContext ctx = LogContext.enter(); try { // Prefix the executing thread to any message being logged. ctx.prefix("[Thread: ", Thead.currentThread(), "] "); ... } finally { ctx.exit(); } }
Applications should separate messages elements by commas and not
use String
concatenations when calling log methods otherwise
the concatenation is performed even when log events are filtered out.
LogContext ctx = LogContext.enter(); try { ctx.setLevel(Level.INFO); // Does not log debug messages. ... LogContext.debug("Index: ", index, " at maximum value"); // GOOD, no formatting performed ! LogContext.debug("Index: " + index + " at maximum value"); // BAD, formatting performed even though nothing is logged ! ... } finally { ctx.exit(); }
Modifier and Type | Class and Description |
---|---|
static class |
LogContext.Level
Defines the logging levels.
|
Modifier and Type | Field and Description |
---|---|
static Configurable<LogContext.Level> |
LEVEL
Holds the default logging level (
DEBUG ). |
Modifier | Constructor and Description |
---|---|
protected |
LogContext()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
debug(Object... message)
Logs the specified debug message.
|
static LogContext |
enter()
Enters and returns a new log context instance.
|
static void |
error(Object... message)
Logs the specified error message (which may include any
Throwable
instance). |
static void |
info(Object... message)
Logs the specified info message.
|
protected abstract void |
log(LogContext.Level level,
Object... message)
Logs the specified message at the specified level.
|
abstract void |
prefix(Object... prefixes)
Prefixes all messages being logged by the specified prefixes
(prefixing existing prefixes if any).
|
abstract void |
setLevel(LogContext.Level level)
Set the logging level, messages below that level are not logged.
|
abstract void |
suffix(Object... suffixes)
Suffixes all messages being logged by the specified suffixes
(suffixing existing suffixes if any).
|
static void |
warning(Object... message)
Logs the specified warning message.
|
current, current, enter, enterInner, exit, getOuter, inherit, inner
public static final Configurable<LogContext.Level> LEVEL
DEBUG
).
This level is configurable. For example, running with
the option -Djavolution.context.LogContext#LEVEL=WARNING
causes the debug/info not to be logged.public static void debug(Object... message)
public static LogContext enter()
public static void error(Object... message)
Throwable
instance).public static void info(Object... message)
public static void warning(Object... message)
public abstract void prefix(Object... prefixes)
public abstract void setLevel(LogContext.Level level)
public abstract void suffix(Object... suffixes)
protected abstract void log(LogContext.Level level, Object... message)
Copyright © 2005-2013 Javolution. All Rights Reserved.