org.geotoolkit.util.logging
Class Logging

Object
  extended by Static
      extended by Logging

public final class Logging
extends Static

A set of utilities method for configuring loggings in Geotk. Library implementors shall fetch their logger using the getLogger(Class) static method provided in this class rather than Logger.getLogger(String), in order to give Geotk a chance to redirect the logging to an other framework like Commons-logging or Log4J.

This method provides also a log(Class, String, LogRecord) convenience static method, which set the logger name of the given record before to log it. An other worthy static method is unexpectedException(Class, String, Throwable), for reporting an anomalous but nevertheless non-fatal exception.


Configuration
The log records can be redirected explicitly to an other logging framework using the following method call (replace LoggerFactory.COMMONS_LOGGING by LoggerFactory.LOG4J or an other framework if desired):

Logging.GEOTOOLKIT.setLoggerFactory(LoggerFactory.COMMONS_LOGGING);
Note however that the above method invocation is performed automatically if the geotk-logging-commons.jar or the geotk-logging-log4j.jar file is found on the classpath, so it usually doesn't need to be invoked explicitely. See the scanLoggerFactory() method for more details on automatic logger factory registration.

Since:
2.4
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys)
Module:
utility/geotk-utility (download)    View source code for this class

Field Summary
static Logging ALL
          Logging configuration that apply to all packages.
static Logging GEOTOOLKIT
          Logging configuration that apply only to org.geotoolkit packages.
 String name
          The name of the base package.
 
Method Summary
 void flush()
          Flushes all handlers used by the logger named name.
 void forceMonolineConsoleOutput(Level level)
          Configures the default console handler in order to log records on a single line instead of two lines.
static Logger getLogger(Class<?> classe)
          Returns a logger for the specified class.
static Logger getLogger(String name)
          Returns a logger for the specified name.
 LoggerFactory<?> getLoggerFactory()
          Returns the logger factory, or null if none.
static Logging getLogging(String name)
          Returns a Logging instance for the specified base logger.
static void log(Class<?> classe, LogRecord record)
          Deprecated. Replaced by log(Class, String, LogRecord), because experience suggests that letting the caller specifies himself the source class and method names is error prone.
static void log(Class<?> classe, String method, LogRecord record)
          Logs the given record to the logger associated to the given class.
static boolean recoverableException(Class<?> classe, String method, Throwable error)
          Invoked when a recoverable error occurs.
static boolean recoverableException(Logger logger, Class<?> classe, String method, Throwable error)
          Invoked when a recoverable error occurs.
 void scanLoggerFactory()
          Scans the classpath for logger factories.
 void setLoggerFactory(LoggerFactory<?> factory)
          Sets a new logger factory for this Logging instance and every children.
 void setLoggerFactory(String className)
          Deprecated. The META-INF/services mechanism should be sufficient. This method will be removed in order to reduce the weight of the Geotk library.
static boolean severeException(Logger logger, Class<?> classe, String method, Throwable error)
          Invoked when a severe error occurs.
static boolean unexpectedException(Class<?> classe, String method, Throwable error)
          Invoked when an unexpected error occurs.
static boolean unexpectedException(Logger logger, Class<?> classe, String method, Throwable error)
          Invoked when an unexpected error occurs.
static boolean unexpectedException(Logger logger, Throwable error)
          Invoked when an unexpected error occurs.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALL

public static final Logging ALL
Logging configuration that apply to all packages.


GEOTOOLKIT

public static final Logging GEOTOOLKIT
Logging configuration that apply only to org.geotoolkit packages.


name

public final String name
The name of the base package.

Since:
3.15
Method Detail

log

public static void log(Class<?> classe,
                       String method,
                       LogRecord record)
Logs the given record to the logger associated to the given class. This convenience method performs the following steps:

Parameters:
classe - The class for which to obtain a logger.
method - The name of the method which is logging a record.
record - The record to log.
Since:
3.20 (derived from 3.00)

log

@Deprecated
public static void log(Class<?> classe,
                                  LogRecord record)
Deprecated. Replaced by log(Class, String, LogRecord), because experience suggests that letting the caller specifies himself the source class and method names is error prone.

Logs the given record to the logger for the given class. This convenience method performs the following steps:

Note: This method does not set the source class name because LogRecord can infer it automatically. However callers are encouraged to set the source class and method names themselves for more reliability.

Parameters:
classe - The class for which to obtain a logger.
record - The record to log.
Since:
3.00

getLogger

public static Logger getLogger(Class<?> classe)
Returns a logger for the specified class. This convenience method invokes getLogger(String) with the package name as the logger name.

Parameters:
classe - The class for which to obtain a logger.
Returns:
A logger for the specified class.
Since:
2.5

getLogger

public static Logger getLogger(String name)
Returns a logger for the specified name. If a logger factory has been set, then this method first ask to the factory. It gives Geotk a chance to redirect logging events to commons-logging or some equivalent framework.

If no factory was found or if the factory choose to not redirect the loggings, then this method returns the usual Logger.getLogger(name).

Parameters:
name - The logger name.
Returns:
A logger for the specified name.

getLogging

public static Logging getLogging(String name)
Returns a Logging instance for the specified base logger. This instance is used for controlling logging configuration in Geotk. For example methods like forceMonolineConsoleOutput(java.util.logging.Level) are invoked on a Logging instance.

Logging instances follow the same hierarchy than Logger, i.e. "org.geotoolkit" is the parent of "org.geotoolkit.referencing", "org.geotoolkit.metadata", etc.

Parameters:
name - The base logger name.
Returns:
The logging instance for the given name.

getLoggerFactory

public LoggerFactory<?> getLoggerFactory()
Returns the logger factory, or null if none. This method returns the logger set by the last call to setLoggerFactory(org.geotoolkit.util.logging.LoggerFactory) on this Logging instance or on one of its parent.

Returns:
The current logger factory.

setLoggerFactory

@Configuration
public void setLoggerFactory(LoggerFactory<?> factory)
Sets a new logger factory for this Logging instance and every children. The specified factory will be used by getLogger(name) when name is this Logging name or one of its children.

If the factory is set to null (the default), then the standard Logging framework will be used.

Parameters:
factory - The new logger factory, or null if none.

setLoggerFactory

@Deprecated
@Configuration
public void setLoggerFactory(String className)
                      throws ClassNotFoundException,
                             IllegalArgumentException
Deprecated. The META-INF/services mechanism should be sufficient. This method will be removed in order to reduce the weight of the Geotk library.

Sets a new logger factory from a fully qualified class name. This method should be preferred to setLoggerFactory(LoggerFactory) when the underlying logging framework is not guaranteed to be on the classpath.

Parameters:
className - The fully qualified factory class name.
Throws:
ClassNotFoundException - if the specified class was not found.
IllegalArgumentException - if the specified class is not a subclass of LoggerFactory, or if no public static getInstance() method has been found or can be executed.
See Also:
LoggerFactory.COMMONS_LOGGING, LoggerFactory.LOG4J

scanLoggerFactory

@Configuration
public void scanLoggerFactory()
Scans the classpath for logger factories. The fully qualified factory classname shall be declared in the following file:
META-INF/services/org.geotoolkit.util.logging.LoggerFactory
The first factory found on the classpath is given to setLoggerFactory(String). If it can't be used (for example because of missing dependency), then the second factory is tried, etc. until an acceptable factory is found.

This method usually doesn't need to be invoked explicitly, since it is automatically invoked on Logging class initialization. However developers may invoke it if new LoggerFactorys are added later on the classpath of a running JVM.

Since:
3.00

forceMonolineConsoleOutput

@Configuration
public void forceMonolineConsoleOutput(Level level)
Configures the default console handler in order to log records on a single line instead of two lines. More specifically, for each ConsoleHandler using a SimpleFormatter, this method replaces the simple formatter by an instance of MonolineFormatter. If no ConsoleHandler are found, then a new one is created.
Note: This method may have no effect if the loggings are redirected to an other logging framework than the standard Java one.
In addition, this method can set the handler levels. If the level is non-null, then every Handlers using the monoline formatter may be set to the specified level. Whatever the given level is used or not depends on current configuration. The choice is based on heuristic rules that may change in any future version. Developers are encouraged to avoid non-null level except for debugging purpose, since a user trying to configure his logging properties file may find confusing to see his setting ignored.

Parameters:
level - The desired logging level, or null to left it unchanged.
See Also:
Java Logging Overview, Setup

flush

public void flush()
Flushes all handlers used by the logger named name. If that logger uses parent handlers, then those handlers will be flushed as well.

If the log records seem to be interleaved with the content of System.out or System.err, invoking this method before to write to the standard streams may help.

Since:
3.18
See Also:
Handler.flush()

unexpectedException

public static boolean unexpectedException(Logger logger,
                                          Throwable error)
Invoked when an unexpected error occurs. This method logs a message at the WARNING level to the specified logger. The originating class name and method name are inferred from the error stack trace, using the first stack trace element for which the class name is inside a package or sub-package of the logger name. For example if the logger name is "org.geotoolkit.image", then this method will uses the first stack trace element where the fully qualified class name starts with "org.geotoolkit.image" or "org.geotoolkit.image.io", but not "org.geotoolkit.imageio".

Parameters:
logger - Where to log the error.
error - The error that occurred.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the WARNING level.

unexpectedException

public static boolean unexpectedException(Logger logger,
                                          Class<?> classe,
                                          String method,
                                          Throwable error)
Invoked when an unexpected error occurs. This method logs a message at the WARNING level to the specified logger. The originating class name and method name can optionally be specified. If any of them is null, then it will be inferred from the error stack trace as in unexpectedException(Logger, Throwable).

Explicit value for class and method names are sometime preferred to automatic inference for the following reasons:

Parameters:
logger - Where to log the error.
classe - The class where the error occurred, or null.
method - The method where the error occurred, or null.
error - The error.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the WARNING level.
See Also:
recoverableException(Logger, Class, String, Throwable), severeException(Logger, Class, String, Throwable)

unexpectedException

public static boolean unexpectedException(Class<?> classe,
                                          String method,
                                          Throwable error)
Invoked when an unexpected error occurs. This method logs a message at the WARNING level to a logger inferred from the given class.

Parameters:
classe - The class where the error occurred.
method - The method where the error occurred, or null.
error - The error.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the WARNING level.
Since:
2.5
See Also:
recoverableException(Class, String, Throwable)

recoverableException

public static boolean recoverableException(Class<?> classe,
                                           String method,
                                           Throwable error)
Invoked when a recoverable error occurs. This method is similar to unexpectedException except that it doesn't log the stack trace and uses a lower logging level.

Parameters:
classe - The class where the error occurred.
method - The method name where the error occurred.
error - The error.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the FINE level.
Since:
2.5
See Also:
unexpectedException(Class, String, Throwable)

recoverableException

public static boolean recoverableException(Logger logger,
                                           Class<?> classe,
                                           String method,
                                           Throwable error)
Invoked when a recoverable error occurs. This method is similar to unexpectedException except that it doesn't log the stack trace and uses a lower logging level.

Parameters:
logger - Where to log the error.
classe - The class where the error occurred.
method - The method name where the error occurred.
error - The error.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the FINE level.
Since:
2.5
See Also:
unexpectedException(Logger, Class, String, Throwable), severeException(Logger, Class, String, Throwable)

severeException

public static boolean severeException(Logger logger,
                                      Class<?> classe,
                                      String method,
                                      Throwable error)
Invoked when a severe error occurs. This method is similar to unexpectedException except that it logs the message at the SEVERE level.

Parameters:
logger - Where to log the error.
classe - The class where the error occurred.
method - The method name where the error occurred.
error - The error.
Returns:
true if the error has been logged, or false if the logger doesn't log anything at the SEVERE level.
Since:
3.00
See Also:
unexpectedException(Logger, Class, String, Throwable), recoverableException(Logger, Class, String, Throwable)


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.