Class ConfigurationLogger
- java.lang.Object
-
- org.apache.commons.configuration2.io.ConfigurationLogger
-
public class ConfigurationLogger extends java.lang.Object
A class providing basic logging capabilities.
When reading configuration files in complex scenarios having log output is useful for diagnostic purposes. Therefore, Commons Configuration produces some logging output. As concrete projects have different requirements on the amount and detail of logging, there is a way of configuring logging: All classes derived from
AbstractConfiguration
can be assigned a logger which is then used for all log statements generated.Allowing a logger object to be passed to a configuration creates a direct dependency to a concrete logging framework in the configuration API. This would make it impossible to switch to an alternative logging framework without breaking backwards compatibility. To avoid this, the
ConfigurationLogger
class is introduced. It is a minimum abstraction over a logging framework offering only very basic logging capabilities. The methods defined in this class are used by configuration implementations to produce their logging statements. Client applications can create specialized instances and pass them to configuration objects without having to deal with a concrete logging framework. It is even possible to create a subclass that uses a completely different logging framework.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private org.apache.commons.logging.Log
log
The internal logger.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ConfigurationLogger()
Creates a new, uninitialized instance ofConfigurationLogger
.ConfigurationLogger(java.lang.Class<?> logCls)
Creates a new instance ofConfigurationLogger
that uses a logger whose name is derived from the provided class.ConfigurationLogger(java.lang.String loggerName)
Creates a new instance ofConfigurationLogger
that uses the specified logger name.(package private)
ConfigurationLogger(org.apache.commons.logging.Log wrapped)
Creates a new instance ofConfigurationLogger
which wraps the specified logger.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static org.apache.commons.logging.Log
createLoggerForClass(java.lang.Class<?> cls)
Creates an internal logger for the given class.private static org.apache.commons.logging.Log
createLoggerForName(java.lang.String name)
Creates an internal logger for the given name.void
debug(java.lang.String msg)
Logs the specified message on debug level.void
error(java.lang.String msg)
Logs the specified message on error level.void
error(java.lang.String msg, java.lang.Throwable ex)
Logs the specified exception on error level.(package private) org.apache.commons.logging.Log
getLog()
Gets the internal logger.void
info(java.lang.String msg)
Logs the specified message on info level.boolean
isDebugEnabled()
Returns a flag whether logging on debug level is enabled.boolean
isInfoEnabled()
Returns a flag whether logging on info level is enabled.static ConfigurationLogger
newDummyLogger()
Creates a new dummy logger which produces no output.void
warn(java.lang.String msg)
Logs the specified message on warn level.void
warn(java.lang.String msg, java.lang.Throwable ex)
Logs the specified exception on warn level.
-
-
-
Constructor Detail
-
ConfigurationLogger
protected ConfigurationLogger()
Creates a new, uninitialized instance ofConfigurationLogger
. This constructor can be used by derived classes that implement their own specific logging mechanism. Such classes must override all methods because the default implementations do not work in this uninitialized state.
-
ConfigurationLogger
public ConfigurationLogger(java.lang.Class<?> logCls)
Creates a new instance ofConfigurationLogger
that uses a logger whose name is derived from the provided class.- Parameters:
logCls
- the class whose name is to be used for logging (must not be null)- Throws:
java.lang.IllegalArgumentException
- if the logger class is null
-
ConfigurationLogger
ConfigurationLogger(org.apache.commons.logging.Log wrapped)
Creates a new instance ofConfigurationLogger
which wraps the specified logger.- Parameters:
wrapped
- the logger to be wrapped
-
ConfigurationLogger
public ConfigurationLogger(java.lang.String loggerName)
Creates a new instance ofConfigurationLogger
that uses the specified logger name.- Parameters:
loggerName
- the logger name (must not be null)- Throws:
java.lang.IllegalArgumentException
- if the logger name is null
-
-
Method Detail
-
createLoggerForClass
private static org.apache.commons.logging.Log createLoggerForClass(java.lang.Class<?> cls)
Creates an internal logger for the given class. Throws an exception if the class is undefined.- Parameters:
cls
- the logger class- Returns:
- the logger object
- Throws:
java.lang.IllegalArgumentException
- if the logger class is undefined
-
createLoggerForName
private static org.apache.commons.logging.Log createLoggerForName(java.lang.String name)
Creates an internal logger for the given name. Throws an exception if the name is undefined.- Parameters:
name
- the name of the logger- Returns:
- the logger object
- Throws:
java.lang.IllegalArgumentException
- if the logger name is undefined
-
newDummyLogger
public static ConfigurationLogger newDummyLogger()
Creates a new dummy logger which produces no output. If such a logger is passed to a configuration object, logging is effectively disabled.- Returns:
- the new dummy logger
-
debug
public void debug(java.lang.String msg)
Logs the specified message on debug level.- Parameters:
msg
- the message to be logged
-
error
public void error(java.lang.String msg)
Logs the specified message on error level.- Parameters:
msg
- the message to be logged
-
error
public void error(java.lang.String msg, java.lang.Throwable ex)
Logs the specified exception on error level.- Parameters:
msg
- the message to be loggedex
- the exception to be logged
-
getLog
org.apache.commons.logging.Log getLog()
Gets the internal logger.- Returns:
- the internal logger
-
info
public void info(java.lang.String msg)
Logs the specified message on info level.- Parameters:
msg
- the message to be logged
-
isDebugEnabled
public boolean isDebugEnabled()
Returns a flag whether logging on debug level is enabled.- Returns:
- true if debug logging is enabled, false otherwise
-
isInfoEnabled
public boolean isInfoEnabled()
Returns a flag whether logging on info level is enabled.- Returns:
- true if debug logging is enabled, false otherwise
-
warn
public void warn(java.lang.String msg)
Logs the specified message on warn level.- Parameters:
msg
- the message to be logged
-
warn
public void warn(java.lang.String msg, java.lang.Throwable ex)
Logs the specified exception on warn level.- Parameters:
msg
- the message to be loggedex
- the exception to be logged
-
-