Class LoggerAdapter
-
- Direct Known Subclasses:
Log4J2Logger
,Log4JLogger
,LogbackLogger
public abstract class LoggerAdapter extends Logger
An adapter that redirect all Java logging events to an other logging framework. This class redefines thesevere
,warning
,info
,config
,fine
,finer
andfinest
methods as abstract ones. Subclasses should implement those methods in order to map Java logging levels to the backend logging framework.All
log
methods are overriden in order to redirect to one of the above-cited methods. Note that this is the opposite approach than the Java logging framework one, which implemented everything on top ofLogger.log(LogRecord)
. This adapter is defined in terms ofsevere
…finest
methods instead because external frameworks like Commons-logging don't work withLogRecord
, and sometime provides nothing else than convenience methods equivalent tosevere
…finest
.Restrictions
Because the configuration is expected to be fully controled by the external logging framework, every configuration methods inherited fromLogger
are disabled:addHandler(java.util.logging.Handler)
since the handling is performed by the external framework.setUseParentHandlers(boolean)
since this adapter never delegates to the parent handlers. This is consistent with the previous item and avoid mixing loggings from the external framework with Java loggings.setParent(java.util.logging.Logger)
since this adapter should not inherits any configuration from a parent logger using the Java logging framework.setFilter(java.util.logging.Filter)
for keeping thisLoggerAdapter
simple.
Since
LoggerAdapter
s do not hold any configuration by themself, it is not strictly necessary to add them to the log manager. The adapters can be created, garbage-collected and recreated again while preserving their behavior since their configuration is entirely contained in the external logging framework.Localization
This logger is always created without resource bundles. Localizations must be performed through explicit calls tologrb
orlog(LogRecord)
methods. This is sufficient for GeoTools needs, which performs all localizations through the latter. Note that these methods will be slower in thisLoggerAdapter
than the defaultLogger
because this adapter localizes and formats records immediately instead of letting the Handler performs this work only if needed.Logging levels
If a log record level is not one of the predefined ones, then this class maps to the first level below the specified one. For example if a log record has some level betweenFINE
andFINER
, then thefiner
method will be invoked. SeeisLoggable(java.util.logging.Level)
for implementation tips taking advantage of this rule.Subclasses
The abstract methods required for subclassing handle redirect message logging to target framework.Throwable: When adapting to a logging framework that supports logging of Throwables implement
log(Level, String, Throwable)
if you wish to support this functionality.Throwable: When adapting to a logging framework that supports logging of Throwables * implement
log(Level, String, Throwable)
if you wish to support this functionality.- Since:
- 2.4
- Author:
- Martin Desruisseaux
- See Also:
Logging
-
-
Field Summary
-
Fields inherited from class Logger
global, GLOBAL_LOGGER_NAME
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
LoggerAdapter(String name)
Creates a new logger.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addHandler(Handler handler)
Do nothing since this logger adapter does not supports handlers.abstract void
config(String message)
Logs anCONFIG
message.void
entering(String sourceClass, String sourceMethod)
Logs a method entry to the debug level.void
entering(String sourceClass, String sourceMethod, Object param)
Logs a method entry to the debug level with one parameter.void
entering(String sourceClass, String sourceMethod, Object[] params)
Logs a method entry to the debug level with many parameters.void
exiting(String sourceClass, String sourceMethod)
Logs a method return to the debug level.void
exiting(String sourceClass, String sourceMethod, Object result)
Logs a method return to the debug level.abstract void
fine(String message)
Logs aFINE
message.abstract void
finer(String message)
Logs aFINER
message.abstract void
finest(String message)
Logs aFINEST
message.protected Level
getDebugLevel()
Returns the level forentering(java.lang.String, java.lang.String)
,exiting(java.lang.String, java.lang.String)
andthrowing(java.lang.String, java.lang.String, java.lang.Throwable)
methods.abstract Level
getLevel()
Returns the level for this logger.abstract void
info(String message)
Logs anINFO
message.abstract boolean
isLoggable(Level level)
Returnstrue
if the specified level is loggable.void
log(Level level, String message)
Logs a record at the specified level.void
log(Level level, String message, Object param)
Logs a record at the specified level.void
log(Level level, String message, Object[] params)
Logs a record at the specified level.void
log(Level level, String message, Throwable thrown)
Logs a record at the specified level.void
log(LogRecord record)
Logs a record.void
logp(Level level, String sourceClass, String sourceMethod, String message)
Logs a record at the specified level.void
logp(Level level, String sourceClass, String sourceMethod, String message, Object param)
Logs a record at the specified level.void
logp(Level level, String sourceClass, String sourceMethod, String message, Object[] params)
Logs a record at the specified level.void
logp(Level level, String sourceClass, String sourceMethod, String message, Throwable thrown)
Logs a record at the specified level.void
logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message)
Logs a localizable record at the specified level.void
logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Object param)
Logs a localizable record at the specified level.void
logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Object[] params)
Logs a localizable record at the specified level.void
logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Throwable thrown)
Logs a localizable record at the specified level.void
removeHandler(Handler handler)
Do nothing since this logger adapter does not support handlers.void
setFilter(Filter filter)
Do nothing since this logger adapter does not support filters.abstract void
setLevel(Level level)
Sets the level for this logger.void
setParent(Logger parent)
Do nothing since this logger adapter does not support arbitrary parents.void
setUseParentHandlers(boolean useParentHandlers)
Do nothing since this logger never use parent handlers.abstract void
severe(String message)
Logs aSEVERE
message.void
throwing(String sourceClass, String sourceMethod, Throwable thrown)
Logs a method failure to the debug level.String
toString()
abstract void
warning(String message)
Logs aWARNING
message.-
Methods inherited from class Logger
config, fine, finer, finest, getAnonymousLogger, getAnonymousLogger, getFilter, getGlobal, getHandlers, getLogger, getLogger, getName, getParent, getResourceBundle, getResourceBundleName, getUseParentHandlers, info, log, log, logp, logp, logrb, logrb, logrb, logrb, setResourceBundle, severe, warning
-
-
-
-
Constructor Detail
-
LoggerAdapter
protected LoggerAdapter(String name)
Creates a new logger.- Parameters:
name
- The logger name.
-
-
Method Detail
-
setLevel
public abstract void setLevel(Level level)
Sets the level for this logger. Subclasses must redirect the call to the external logging framework, or do nothing if the level can not be changed programmatically.
-
getLevel
public abstract Level getLevel()
Returns the level for this logger. Subclasses shall get this level from the external logging framework.
-
getDebugLevel
protected Level getDebugLevel()
Returns the level forentering(java.lang.String, java.lang.String)
,exiting(java.lang.String, java.lang.String)
andthrowing(java.lang.String, java.lang.String, java.lang.Throwable)
methods. The default implementation returnsLevel.FINER
, which is consistent with the value used in the Java logging framework. Subclasses should override this method if a different debug level is wanted.
-
isLoggable
public abstract boolean isLoggable(Level level)
Returnstrue
if the specified level is loggable.Implementation tip
Given thatLevel.intValue()
for all predefined levels are documented in theLevel
specification and are multiple of 100, given that integer divisions are rounded toward zero and given rule documented in this class javadoc, then logging levels can be efficiently mapped to predefined levels usingswitch
statements as below. This statement has good chances to be compiled to thetableswitch
bytecode rather thanlookupswitch
(see Compiling Switches in The Java Virtual Machine Specification).- Overrides:
isLoggable
in classLogger
-
entering
public void entering(String sourceClass, String sourceMethod)
Logs a method entry to the debug level. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
entering
public void entering(String sourceClass, String sourceMethod, Object param)
Logs a method entry to the debug level with one parameter. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
entering
public void entering(String sourceClass, String sourceMethod, Object[] params)
Logs a method entry to the debug level with many parameters. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
exiting
public void exiting(String sourceClass, String sourceMethod)
Logs a method return to the debug level. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
exiting
public void exiting(String sourceClass, String sourceMethod, Object result)
Logs a method return to the debug level. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
throwing
public void throwing(String sourceClass, String sourceMethod, Throwable thrown)
Logs a method failure to the debug level. Compared to the defaultLogger
, this implementation bypass the level check in order to let the backing logging framework do its own check.
-
log
public void log(LogRecord record)
Logs a record. The default implementation delegates tologrb
.
-
log
public void log(Level level, String message, Throwable thrown)
Logs a record at the specified level. The default implementation discards the exception and delegates tolog(level, message)
.
-
log
public void log(Level level, String message, Object param)
Logs a record at the specified level. The defaut implementation delegates tolog(level, message, params)
where theparams
array is built from theparam
object.
-
log
public void log(Level level, String message, Object[] params)
Logs a record at the specified level. The defaut implementation formats the message immediately, then delegates tolog(level, message)
.
-
logp
public void logp(Level level, String sourceClass, String sourceMethod, String message)
Logs a record at the specified level. The defaut implementation discards the source class and source method, then delegates tolog(level, message)
.
-
logp
public void logp(Level level, String sourceClass, String sourceMethod, String message, Throwable thrown)
Logs a record at the specified level. The defaut implementation discards the source class and source method, then delegates tolog(level, message, thrown)
.
-
logp
public void logp(Level level, String sourceClass, String sourceMethod, String message, Object param)
-
logp
public void logp(Level level, String sourceClass, String sourceMethod, String message, Object[] params)
-
logrb
public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message)
Logs a localizable record at the specified level. The defaut implementation localizes the message immediately, then delegates tologp(level, sourceClass, sourceMethod, message)
.
-
logrb
public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Throwable thrown)
Logs a localizable record at the specified level. The defaut implementation localizes the message immediately, then delegates tologp(level, sourceClass, sourceMethod, message, thrown)
.
-
logrb
public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Object param)
Logs a localizable record at the specified level. The defaut implementation localizes the message immediately, then delegates tologp(level, sourceClass, sourceMethod, message, param)
.
-
logrb
public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String message, Object[] params)
Logs a localizable record at the specified level. The defaut implementation localizes the message immediately, then delegates tologp(level, sourceClass, sourceMethod, message, params)
.
-
addHandler
public void addHandler(Handler handler)
Do nothing since this logger adapter does not supports handlers. The configuration should be fully controlled by the external logging framework (e.g. Commons-logging) instead, which is not expected to useHandler
objects.- Overrides:
addHandler
in classLogger
-
removeHandler
public void removeHandler(Handler handler)
Do nothing since this logger adapter does not support handlers.- Overrides:
removeHandler
in classLogger
-
setUseParentHandlers
public void setUseParentHandlers(boolean useParentHandlers)
Do nothing since this logger never use parent handlers. This is consistent withaddHandler(java.util.logging.Handler)
not allowing to add any handlers, and avoid mixing loggings from the external framework with Java loggings.- Overrides:
setUseParentHandlers
in classLogger
-
setParent
public void setParent(Logger parent)
Do nothing since this logger adapter does not support arbitrary parents. More specifically, it should not inherits any configuration from a parent logger using the Java logging framework.
-
setFilter
public void setFilter(Filter filter)
Do nothing since this logger adapter does not support filters. It is difficult to query efficiently the filter in thisLoggerAdapter
architecture (e.g. we would need to make sure thatFilter.isLoggable(java.util.logging.LogRecord)
is invoked only once even if alog
call is cascaded into many otherlog
calls, and this test must works in multi-threads environment).
-
-