Package org.apache.sis.util.logging
Class Logging
A set of utilities method for configuring loggings in SIS. Library implementers should fetch
their loggers using the
getLogger(String) static method defined in this Logging
class rather than the one defined in the standard Logger class, in order to give SIS a
chance to redirect the logs to an other framework like
Commons-logging or
Log4J.
This class provides also some convenience static methods, including:
log(Class, String, LogRecord)for setting the logger name, source class name and source method name of the given record before to log it.unexpectedException(Logger, Class, String, Throwable)for reporting an anomalous but nevertheless non-fatal exception.
- Since:
- 0.3
Defined in the sis-utility module
-
Method Summary
Modifier and TypeMethodDescriptionstatic LoggergetLogger(Class<?> source) Returns a logger for the package of the specified class.static LoggergetLogger(String name) Deprecated.static LoggerFactory<?>Returns the factory used for obtainingLoggerinstances, ornullif none.static booleanignorableException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when an ignorable error occurred.static voidlog(Class<?> classe, String method, LogRecord record) Logs the given record to the logger associated to the given class.static booleanrecoverableException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when a recoverable error occurred.static voidsetLoggerFactory(LoggerFactory<?> factory) Sets a new factory to use for obtainingLoggerinstances.static booleansevereException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when a severe error occurred.static booleanunexpectedException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when an unexpected error occurred.
-
Method Details
-
setLoggerFactory
Sets a new factory to use for obtainingLoggerinstances. If the givenfactoryargument isnull(the default), then the standard Logging framework will be used.Limitation
SIS classes typically declare a logger constant like below:
Factory changes will take effect only if this method is invoked before the initialization of such classes.public static final Logger LOGGER = Logging.getLogger("the.logger.name");- Parameters:
factory- the new logger factory, ornullif none.
-
getLoggerFactory
Returns the factory used for obtainingLoggerinstances, ornullif none.- Returns:
- the current logger factory, or
nullif none.
-
getLogger
Deprecated.Use the standardLogger.getLogger(String)method instead. See SIS-531.Returns a logger for the specified name. If a logger factory has been set, then this method first asks to the factory. This rule gives SIS a chance to redirect logging events to commons-logging or some equivalent framework. Only if no factory was found or if the factory choose to not redirect the loggings, then this method delegate toLogger.getLogger(name).- Parameters:
name- the logger name.- Returns:
- a logger for the specified name.
-
getLogger
Returns a logger for the package of the specified class. This convenience method invokesgetLogger(String)with the package name of the given class taken as the logger name.- Parameters:
source- the class which will emit a logging message.- Returns:
- a logger for the specified class.
- Since:
- 1.0
-
log
Logs the given record to the logger associated to the given class. This convenience method performs the following steps:- Unconditionally set the source class name to the canonical name of the given class;
- Unconditionally set the source method name to the given value;
- Get the logger for the logger name if specified,
or for the
classepackage name otherwise; - Set the logger name of the given record, if not already set;
- Log the modified record.
- Parameters:
classe- the class to report as the source of the logging message.method- the method to report as the source of the logging message.record- the record to log.
-
unexpectedException
public static boolean unexpectedException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when an unexpected error occurred. This method logs a message atLevel.WARNINGto the specified logger. The originating class name and method name can optionally be specified. If any of them isnull, then it will be inferred from the error stack trace as described below.Recommended usage: explicit value for class and method names are preferred to automatic inference for the following reasons:If the- Automatic inference is not 100% reliable, since the Java Virtual Machine is free to omit stack frame in optimized code.
- When an exception occurred in a private method used internally by a public method, we sometime want to log the warning for the public method instead, since the user is not expected to know anything about the existence of the private method. If a developer really want to know about the private method, the stack trace is still available anyway.
classeormethodarguments are null, then the originating class name and method name are inferred from the givenerrorusing the first stack trace element for which the class name is inside a package or sub-package of the same name than the logger name.Example: if the logger name is"org.apache.sis.image", then this method will uses the first stack trace element where the fully qualified class name starts with"org.apache.sis.image"or"org.apache.sis.image.io", but not"org.apache.sis.imageio".- Parameters:
logger- where to log the error, ornullfor inferring a default value from other arguments.classe- the class where the error occurred, ornullfor inferring a default value from other arguments.method- the method where the error occurred, ornullfor inferring a default value from other arguments.error- the error, ornullif none.- Returns:
trueif the error has been logged, orfalseif the givenerrorwas null or if the logger does not log anything atLevel.WARNING.- See Also:
-
recoverableException
public static boolean recoverableException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when a recoverable error occurred. This method is similar tounexpectedException(…)except that it does not log the stack trace and uses a lower logging level.- Parameters:
logger- where to log the error, ornullfor inferring a default value from other arguments.classe- the class where the error occurred, ornullfor inferring a default value from other arguments.method- the method name where the error occurred, ornullfor inferring a default value from other arguments.error- the error, ornullif none.- Returns:
trueif the error has been logged, orfalseif the givenerrorwas null or if the logger does not log anything atLevel.FINE.- See Also:
-
ignorableException
public static boolean ignorableException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when an ignorable error occurred. This method is similar tounexpectedException(…)except that it uses a lower logging level.- Parameters:
logger- where to log the error, ornullfor inferring a default value from other arguments.classe- the class where the error occurred, ornullfor inferring a default value from other arguments.method- the method name where the error occurred, ornullfor inferring a default value from other arguments.error- the error, ornullif none.- Returns:
trueif the error has been logged, orfalseif the givenerrorwas null or if the logger does not log anything atLevel.FINER.- Since:
- 1.0
-
severeException
public static boolean severeException(Logger logger, Class<?> classe, String method, Throwable error) Invoked when a severe error occurred. This method is similar tounexpectedExceptionexcept that it logs the message at theSEVERElevel.- Parameters:
logger- where to log the error, ornullfor inferring a default value from other arguments.classe- the class where the error occurred, ornullfor inferring a default value from other arguments.method- the method name where the error occurred, ornullfor inferring a default value from other arguments.error- the error, ornullif none.- Returns:
trueif the error has been logged, orfalseif the givenerrorwas null or if the logger does not log anything atLevel.SEVERE.- See Also:
-
Logger.getLogger(String)method instead.