Setting up and configuring logging should be easy, whether you want to do it with configuration files or in code. Log Events is a small (265kb, no dependencies) logging framework built on top of SLF4J - the logging lingua franka for Java.
For detailed instructions, see the manual.
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.logevents</groupId>
<artifactId>logevents</artifactId>
<version>0.5.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
root=WARN file,console
logger.org.example=INFO
logger.org.example.myapp=DEBUG,TRACE@marker=HTTP_REQUEST&mdc:user=admin
logevents.status=CONFIG
By default, the file
observer will log to a file named logs/your-app-name-%date.log
and console
logs ANSI-colored logs to the console.
The default level for loggers with this configuration will be WARN
, by org.example
will log at INFO
and org.example.myapp
will log at DEBUG
, or trace for HTTP_REQUEST when the user
is admin
. See LogEventFilter for details. Use level NONE
to turn off logging.
Logevents will output configuration information to system err.
Configuration in a Twelve-Factor setting
Here is an example setup in a cloud environment. Logevents can be configured with environment variables and use stdout as the main channel for logging, outputting JSON logs for more powerful downstream parsing
# Output logevents configuration debug to stderr
LOGEVENTS_STATUS=CONFIG
# Skip sun.reflect packages in stack traces
LOGEVENTS_PACKAGEFILTER=sun.reflect
# By default only output to console at DEBUG
LOGEVENTS_ROOT=DEBUG console
# Console format should be JSON for easier log parsing
LOGEVENTS_OBSERVER_CONSOLE_FORMATTER=ConsoleJsonLogEventFormatter
# Install logging to Microsoft Teams for all ERROR messages
LOGEVENTS_ROOT_OBSERVER_TEAMS=ERROR
LOGEVENTS_OBSERVER_TEAMS=MicrosoftTeamsLogEventObserver
LOGEVENTS_OBSERVER_TEAMS_URL=https://example.webhook.office.com/webhookb2/...
# Turn down logging for selected packages
LOGEVENTS_LOGGER_ORG_ECLIPSE_JETTY=WARN
# Turn up logging to TRACE for com.example message for selected users are
LOGEVENTS_LOGGER_COM_EXAMPLE=DEBUG,TRACE@mdc:user=superuser|admin|tester
- Console logging
with good default colors (also on Windows). Use
observer.console.format=ConsoleJsonLogEventFormatter
to output single-line JSON logs, suitable for log parsing - File logging with reasonable defaults
- JUnit support to easy assert on what is logged
- Email logging, including throttling to reduce spamming when get lots of log messages
- Slack to send log messages to you favorite chat channel
- Microsoft Teams
- Logging to database
- Display logs on a web dashboard
- Elasticsearch . Logging directly to Elastic search Index API avoids edge cases when writing and parsing log files
- Humio . Logging directly to Humio via their Elastic search Bulk API.
- Azure Application Insights ( requires optional com.microsoft.azure:applicationinsights-core dependency)
- JMX integration to view the configuration and tweak log levels
- Filter loggers on
markers and MDC values (e.g.
logger.org.example.app=INFO,DEBUG@mdc:user=superuser|admin
) - Filter observers on markers and MDC values