Week 104 — What options are there for logging information in Java applications?
Question of the Week #104
What options are there for logging information in Java applications?
5 Replies
The easiest way to log data is to use
System.out
which normally forwards everything to the console associated with the application (stdout and stderr for System.err
).
However, this does not allow specifying log levels and cannot be configured apart from redirecting it to a different PrintStream
or by redirecting stdout for the entire application and does not differentiate between log levels/severities.
Because of this, the java.util.logging
package in the java.logging
module has been introduced.
These loggers can be configured in a logging.properties
file which can be specified using the java.util.logging.config.file
parameter.
Other than that, SLF4J (Simple Logging Farcade for Java) provides a general API that can be used with many different loggers. To use the API, one needs to add the slf4j-api
dependency.
When an SLF4J logger implementation dependency like logback is added at runtime, these messages will be logged using that dependency.📖 Sample answer from dan1st
you can use the native logging library (java.logging.Logger)
Submission from klone_king
⭐ Submission from mercy_17083
The most basic option for logging is simply writing to
System.out
and System.err
. However, anything more than a trivial application will want to make use of a proper logging framework.
The java.util.logging
package is a logging framework built directly into the JDK (for JDK 9+, the JDK module java.logging
is required). There are 3 main components -- Logger
, Handler
, and Formatter
.
Loggers are the component that client code typically works with. Applications use them to send log messages into the logging subsystem. Each message has a level associated with it. The level is hierarchical, and allows the verbosity of the log messages to be controlled at runtime. For instance, if the logging system is set to INFO
, then only messages at the SEVERE
, WARNING
, or INFO
levels will be output to the log; messages at CONFIG
or lower will be suppressed.
Handlers provide a destination for the log messages. For instance, the ConsoleHandler
will direct messages to System.out
or System.err
. Multiple handlers can be configured, each with their own settings, and the subsystem will take care of sending each log message to all applicable handlers.
Formatters are attached to handlers and control the format of the log messages. In addition to the log message from application code, formatters can add metadata like the timestamp of the message, the level, etc. They can even output the message in special formats such as JSON or XML.Third-party logging frameworks also exist, and can provide advantages over the JDK's logging facilities. Log4J and Logback are two of the more popular ones. SLF4J (Simple Logging Facade for Java) is also an interesting option. It provides a common facade, along with adapters, for working with other logging frameworks, including
java.util.logging
.⭐ Submission from dangerously_casual