Class System.LoggerFinder
- Enclosing class:
System
LoggerFinder
service is responsible for creating, managing, and configuring loggers to the underlying framework it uses. A logger finder is a concrete implementation of this class that has a zero-argument constructor and implements the abstract methods defined by this class. The loggers returned from a logger finder are capable of routing log messages to the logging backend this provider supports. A given invocation of the Java Runtime maintains a single system-wide LoggerFinder instance that is loaded as follows:- First it finds any custom
LoggerFinder
provider using theServiceLoader
facility with thesystem class loader. - If no
LoggerFinder
provider is found, the system defaultLoggerFinder
implementation will be used.
An application can replace the logging backendeven when the java.logging module is present, by simply providing and declaring an implementation of theSystem.LoggerFinder
service.
Default Implementation
The system defaultLoggerFinder
implementation usesjava.util.logging
as the backend framework when thejava.logging
module is present. It returns alogger instance that will route log messages to ajava.util.logging.Logger
. Otherwise, ifjava.logging
is not present, the default implementation will return a simple logger instance that will route log messages ofINFO
level and above to the console (System.err
).
Logging Configuration
Logger instances obtained from theLoggerFinder
factory methods are not directly configurable by the application. Configuration is the responsibility of the underlying logging backend, and usually requires using APIs specific to that backend.
For the defaultLoggerFinder
implementation usingjava.util.logging
as its backend, refer tojava.util.logging
for logging configuration. For the defaultLoggerFinder
implementation returning simple loggers when thejava.logging
module is absent, the configuration is implementation dependent.
Usually an application that uses a logging framework will log messages through a logger facade defined (or supported) by that framework. Applications that wish to use an external framework should log through the facade associated with that framework.
A system class that needs to log messages will typically obtain aSystem.Logger
instance to route messages to the logging framework selected by the application.
Libraries and classes that only need loggers to produce log messages should not attempt to configure loggers by themselves, as that would make them dependent from a specific implementation of theLoggerFinder
service.
Message Levels and Mapping to backend levels
A logger finder is responsible for mapping from a System.Logger.Level
to a level supported by the logging backend it uses.
The default LoggerFinder usingjava.util.logging
as the backend mapsSystem.Logger
levels tojava.util.logging levels of corresponding severity - as described inLogger.Level
.
- Since:
- 9
- See Also:
Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptiongetLocalizedLogger
(String name,ResourceBundle bundle,Module module) Returns a localizable instance ofLogger
for the givenmodule
.abstractSystem.Logger
Returns an instance ofLogger
for the givenmodule
.staticSystem.LoggerFinder
Returns theLoggerFinder
instance.
Constructor Details
LoggerFinder
protected LoggerFinder()Creates a new instance ofLoggerFinder
.- Implementation Note:
- It is recommended that a
LoggerFinder
service implementation does not perform any heavy initialization in its constructor, in order to avoid possible risks of deadlock or class loading cycles during the instantiation of the service provider.
Method Details
getLogger
Returns an instance ofLogger
for the givenmodule
.- Parameters:
name
- the name of the logger.module
- the module for which the logger is being requested.- Returns:
- a
logger
suitable for use within the given module. - Throws:
NullPointerException
- ifname
isnull
ormodule
isnull
.
getLocalizedLogger
Returns a localizable instance ofLogger
for the givenmodule
. The returned logger will use the provided resource bundle for message localization.- Implementation Requirements:
- By default, this method calls
this.getLogger(name, module)
to obtain a logger, then wraps that logger in aSystem.Logger
instance where all methods that do not take aResourceBundle
as parameter are redirected to one which does - passing the givenbundle
for localization. So for instance, a call toLogger.log(Level.INFO, msg)
will end up as a call toLogger.log(Level.INFO, bundle, msg, (Object[])null)
on the wrapped logger instance. Note however that by default, string messages returned bySupplier<String>
will not be localized, as it is assumed that such strings are messages which are already constructed, rather than keys in a resource bundle.An implementation of
LoggerFinder
may override this method, for example, when the underlying logging backend provides its own mechanism for localizing log messages, then such aLoggerFinder
would be free to return a logger that makes direct use of the mechanism provided by the backend. - Parameters:
name
- the name of the logger.bundle
- a resource bundle; can benull
.module
- the module for which the logger is being requested.- Returns:
- an instance of
Logger
which will use the provided resource bundle for message localization. - Throws:
NullPointerException
- ifname
isnull
ormodule
isnull
.
getLoggerFinder
Returns theLoggerFinder
instance. There is one single system-wideLoggerFinder
instance in the Java Runtime. See the class specification of how theLoggerFinder
implementation is located and loaded.- Returns:
- the
LoggerFinder
instance.