Logging
The Microservice API provides build-in but configurable and overridable support for logging.
The method for configuring logging is as follows:
- {@link oaj.microservice.MicroserviceBuilder}
- {@link oaj.microservice.MicroserviceBuilder#logConfig(LogConfig) logConfig(LogConfig)}
- {@link oaj.microservice.MicroserviceBuilder#logger(Logger) logger(Logger)}
If not specified, the logging configuration is pulled in from the configuration file:
#=======================================================================================================================
# Logger settings
#-----------------------------------------------------------------------------------------------------------------------
# See FileHandler Java class for details.
#=======================================================================================================================
[Logging]
# The directory where to create the log file.
# Default is "."
logDir = logs
# The name of the log file to create for the main logger.
# The logDir and logFile make up the pattern that's passed to the FileHandler
# constructor.
# If value is not specified, then logging to a file will not be set up.
logFile = microservice.%g.log
# Whether to append to the existing log file or create a new one.
append = false
# The SimpleDateFormat format to use for dates.
dateFormat = yyyy.MM.dd hh:mm:ss
# The log message format.
# The value can contain any of the following variables:
# {date} - The date, formatted per dateFormat.
# {class} - The class name.
# {method} - The method name.
# {logger} - The logger name.
# {level} - The log level name.
# {msg} - The log message.
# {threadid} - The thread ID.
# {exception} - The localized exception message.
format = [{date} {level}] {msg}%n
# The maximum log file size.
# Suffixes available for numbers.
# See Config.getInt(String,int) for details.
limit = 1M
# Max number of log files.
count = 5
# Default log levels.
# Format is lax-JSON.
# Keys are logger names.
# Values are serialized Level POJOs (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
levels =
{
'': 'WARNING',
org.apache.juneau: 'WARNING',
org.eclipse.jetty: 'WARNING'
}
# Only print unique stack traces once and then refer to them by a simple 8 character hash identifier.
# Useful for preventing log files from filling up with duplicate stack traces.
useStackTraceHashes = true
# The default level for the console logger.
# Values are serialized Level POJOs (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
consoleLevel = WARNING
# The default level for the file logger.
# Values are serialized Level POJOs (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
fileLevel = INFO
The logging configuration can also be defined programmatically through the following API:
- {@link oaj.microservice.LogConfig}
- {@link oaj.microservice.LogConfig#create() create()}
- {@link oaj.microservice.LogConfig#append() append()}
- {@link oaj.microservice.LogConfig#consoleLevel(Level) consoleLevel(Level)}
- {@link oaj.microservice.LogConfig#count(int) count(int)}
- {@link oaj.microservice.LogConfig#fileLevel(Level) fileLevel(Level)}
- {@link oaj.microservice.LogConfig#formatter(Formatter) formatter(Formatter)}
- {@link oaj.microservice.LogConfig#level(String,Level) level(String,Level)}
- {@link oaj.microservice.LogConfig#levels(Map) levels(Map)}
- {@link oaj.microservice.LogConfig#limit(int) limit(int)}
- {@link oaj.microservice.LogConfig#logDir(String) logDir(String)}
- {@link oaj.microservice.LogConfig#logFile(String) logFile(String)}
public static void main(String[] args) {
LogConfig logConfig = LogConfig
.create()
.append(true)
.consoleLevel(FINE)
.level("org.mylogger", FINER)
.logDir("my-log-files");
Microservice
.create()
.args(args)
.logConfig(logConfig)
.build()
.start()
.join()
;
}
If you wish to bypass the default logging configuration entirely, you can pass in your own logger via
the {@link oaj.microservice.MicroserviceBuilder#logger(Logger)} method.
In addition to configuring the built-in Java logging framework, the following convenience methods are also
provided on the {@link oaj.microservice.Microservice} class for logging.
- {@link oaj.microservice.Microservice}
- {@link oaj.microservice.Microservice#getLogger() getLogger()}
- {@link oaj.microservice.Microservice#out(MessageBundle,String,Object...) out(MessageBundle,String,Object...)}
- {@link oaj.microservice.Microservice#err(MessageBundle,String,Object...) err(MessageBundle,String,Object...)}