JEP 158: Unified JVM Logging
Summary
Introduce a common logging system for all components of the JVM.
Goals
- Common command-line options for all logging
- Log messages are categorized using tags (e.g. compiler, gc, classload, metaspace, svc, jfr, ...). One message can have multiple tags (tag-set)
- Logging is performed at different levels:
error, warning, info, debug, trace, develop
. - Possible to select what messages that are logged based on level.
- Possible to redirect logging to console or file.
- The default configuration is that all messages using warning and error level are output to stderr.
- File rotation of log files by size and number of files to keep (similar to what is available for GC logs today)
- Print line-at-a-time (no interleaving within same line)
- Logging messages are in human-readable plain text
- Messages can be "decorated". The default decorations are:
uptime, level, tags
. - Ability to configure which decorations that will be printed.
- Existing '
tty->print...
' logging should use unified logging as output - Logging can be configured dynamically at runtime via jcmd or MBeans
- Tested and supported -- should not crash if/when enabled by user/customer
Stretch goals:
- Multi-line logging: several lines can be logged in a way that keeps them together (non interleaved) when output
- Enable/disable individual log messages (for example by using
__FILE__
/__LINE__
) - Implement syslog and Windows Event Viewer output
- Ability to configure in which order decorations should be printed
Non-Goals
It is outside the scope of this JEP to add the actual logging calls from all JVM components. This JEP will only provide the infrastructure to do the logging.
It is also outside the scope of the JEP to enforce a logging format, apart from the format of the decorations and the use of human-readable plain text.
This JEP will not add logging to Java code in the JDK.
Motivation
The JVM is complex system-level component where root-cause analysis is often a difficult and time-consuming task. Without extensive serviceability features it is often close to impossible to find the root cause of intermittent crashes or performance quirks in a production environment. Fine-grained, easy-to-configure JVM logging available for use by support and sustaining engineering is one such feature.
JRockit has a similar feature and it has been instrumental in providing support to customers.
Description
Tags
The logging framework defines a set of tags in the JVM. Each tag is identified by its name (for example: gc
, compiler
, threads
, etc). The set of tags can be changed in the source code as required. When a log message is added it should be associated with a tag-set classifying the information logged. A tag-set consists of one or more tags.
Levels
Each log message has a logging level associated with it. The available levels are error
, warning
, info
, debug
, trace
and develop
in increasing order of verbosity. The develop
level is only available in non-product builds.
For each output, a logging level can be configured to control the amount of information written to that output. The alternative off
disables logging completely.
Decorations
Logging messages are decorated with information about the message. Here is a list of the possible decorations:
time
-- Current time and date in ISO-8601 formatuptime
-- Time since the start of the JVM in seconds and milliseconds (e.g.,6.567s
)timemillis
-- The same value as generated bySystem.currentTimeMillis()
uptimemillis
-- Milliseconds since the JVM startedtimenanos
-- The same value as generated by System.nanoTime()uptimenanos
-- Nanoseconds since the JVM startedpid
-- The process identifiertid
-- The thread identifierlevel
-- The level associated with the log messagetags
-- The tag-set associated with the log message
Each output can be configured to use a custom set of decorators. The order of them is always the one above though. The decorations to be used can be configured by the user in runtime. Decorations will be prepended to the log message
Example: [6.567s][info][gc,old] Old collection complete
Output
There are currently three types of output supported:
-
stdout -- Outputs to stdout.
-
stderr -- Outputs to stderr.
-
text file -- Outputs to text file(s).
Can be configured to handle file rotation based on written size and a number of files to rotate. Example: rotate log file each 10MB, keep 5 files in rotation. The files names will be appended with their number in the rotation. Example:
hotspot.log.1, hotspot.log.2, ..., hotspot.log.5
Currently open file will not have any number appended. Example:hotspot.log
. The size of the files is not guaranteed to be exactly the size configured. The size can overflow at most the size of the last log message written.
Some output types may require additional configuration. Additional output types could be easily implemented using a simple and well defined interface.
Command-line options
A new command-line option will be added, to control logging from all components of the JVM.
-Xlog
Multiple arguments will be applied in the order they appear on command line. Multiple ´-Xlog´ arguments for the same output will override each other in their given order. Last configuration rules.
The following syntax will be used to configure the logging:
-Xlog[:option]
option := [<what>][:[<output>][:[<decorators>][:<output-options>]]]
'help'
'disable'
what := <selector>[,...]
selector := <tag-set>[*][=<level>]
tag-set := <tag>[+...]
'all'
tag := name of tag
level := trace
debug
info
warning
error
output := 'stderr'
'stdout'
[file=]<filename>
decorators := <decorator>[,...]
'none'
decorator := time
uptime
timemillis
uptimemillis
timenanos
uptimenanos
pid
tid
level
tags
output-options := <output_option>[,...]
output-option := filecount=<file count>
filesize=<file size>
parameter=value
The 'all
' tag is a meta tag consisting of all tag-sets available. '*
' in ´tag-set´ definition denotes 'wildcard' tag match. Not using '*
' means 'all messages matching exactly the specified tags'.
Omitting 'what' alltogether will default to tag-set all
and level info
.
Omitting 'level' will default to info
Omitting 'output' will default to stdout
Omitting 'decorators' will default to uptime, level, tags
The 'none
' decorator is special and used to turn off all decorations.
Levels provided are aggregated. Example, if an output is configured to use ´level´ info
. All messages matching tags in ´what´ with log level info
, warning
and error
will be output.
-Xlog:disable
this turns off all logging and clears all configuration of the
logging framework. Even warnings and errors.
-Xlog:help
prints -Xlog usage syntax and available tags, levels, decorators
along with some example command lines.