0% found this document useful (0 votes)
10 views15 pages

Logging Spring

The document provides an overview of the Java logging API, detailing its core components: Loggers, Appenders, and Layouts. It explains how Loggers capture events, Appenders record them to various destinations, and Layouts format the log events. Additionally, it discusses log levels, configuration options, and logging practices in Spring Boot, including default settings and dependency management.

Uploaded by

eramchegyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Logging Spring

The document provides an overview of the Java logging API, detailing its core components: Loggers, Appenders, and Layouts. It explains how Loggers capture events, Appenders record them to various destinations, and Layouts format the log events. Additionally, it discusses log levels, configuration options, and logging practices in Spring Boot, including default settings and dependency management.

Uploaded by

eramchegyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Logging

Agenda

• Intro
• Loggers
• Appenders
• Layouts
• Levels
Logging intro
The Java logging API consists of three core
components:
Loggers are responsible for capturing events
(called LogRecords) and passing them to the
appropriate Appender.
Appenders (also called Handlers in some logging
frameworks) are responsible for recording log
events to a destination. Appenders use Layouts to
format events before sending them to an output.
Layouts (also called Formatters in some logging frameworks) are responsible for
converting and formatting the data in a log event. Layouts determine how the data
looks when it appears in a log entry.
Loggers

Loggers are objects that trigger log events. Loggers


are created and called in the code of your Java
application, where they generate events before
passing them to an Appender. A class can have
multiple independent Loggers responding to
different events, and you can nest Loggers under
other Loggers
Logger logger to create a hierarchy.
= Logger.getLogger(ClassName.class);
Loggers
Loggers provide several methods for triggering log events. However,
before you can log an event, you need to assign a level. Log levels
determine the severity of the log and can be used to filter the event
or send it to a different Appender(for more information on log levels,
see the Log Levels section). The Logger.log() method requires a level
in addition to a message.
logger.log(Level.WARNING, “This is a
warning!”);
Most logging frameworks provide shorthand methods for logging at a
particular level. For example, the following statement produces the
same output as the previous statement.
logger.debug("Debug message");
logger.warn("Warning message");
logger.error("Error message");
logger.info("Info message");
Appenders

Appenders forward logs from Loggers to an output


destination. During this process, log messages are
formatted using a Layout before being delivered to
their final destination. Multiple Appenders can be
combined to write log events to multiple
destinations. For instance, a single event can be
simultaneously displayed in a console and written to
a file.
Type of Appenders
The ConsoleAppender writes its output to either System.out or System.err with System.out
being the default target. A Layout must be provided to format the LogEvent.
The StreamAppender provides the base for many of the other Appenders such as the File and
Socket appenders that write the event to an Output Stream. It cannot be directly configured.
The FileAppender can either write to a specified file, or it can write to a rotating set of files.
For a rotating set of files, as each file reaches a given size limit, it is closed, rotated out, and
a new file opened.
The SocketAppender is an StreamAppender that writes its output to a remote destination
specified by a host and port. LogRecords are published to a network stream connection. The
data can be sent over either TCP or UDP and can be sent in any format.
The MemoryAppender buffers requests in a circular buffer in memory. Normally this
Appender simply stores incoming LogRecords into its memory buffer and discards earlier
records. This buffering is very cheap and avoids formatting costs.
Layouts

An Appender uses a Layout to format a LogEvent


into a form that meets the needs of whatever will
be consuming the log event
Layouts convert the contents of a log entry from
one data type into another. Logging frameworks
provide Layouts for plain text, HTML, syslog, XML,
JSON, serialized, and other logs.
Log Levels
Log levels provide a way to categorize logs by their severity, or their impact on the
overall health and stability of the application.

log4j java.util.logging
• OFF • SEVERE(HIGHEST
• FATAL LEVEL)

• ERROR • WARNING

• WARN • INFO

• INFO • CONFIG

• DEBUG • FINE

• TRACE • FINER

• ALL • FINEST(LOWEST
LEVEL)
Logging configuration

The Java Logging API can be configured in two ways:


Configuration class: You can use a Java class to configure the Java Logging API. You
do so by specifying the name of the class in the JVM parameter
java.util.logging.config.class. It is the constructor of that class that should load the
configuration and apply it to the Logger's in the hierarchy.
Configuration file: The Java Logging API has a default logging configuration file
located at "lib/logging.properties", inside the JRE directory. If you edit this file, you
edit the default logging settings for the entire JRE, for every program executed.
This is most often not what you want to do, though.
Logging configuration. Example

handlers =java.util.logging.FileHandler,java.util.logging.ConsoleHandler
.level = INFO
# File Logging
java.util.logging.FileHandler.pattern = ${LOG_FILE}
java.util.logging.FileHandler.formatter =
org.springframework.boot.logging.java.SimpleFormatter
java.util.logging.FileHandler.level = ALL
java.util.logging.FileHandler.limit = 10485760
java.util.logging.FileHandler.count = 10

java.util.logging.ConsoleHandler.formatter =
org.springframework.boot.logging.java.SimpleFormatter
java.util.logging.ConsoleHandler.level = ALL
Logging in Spring
Spring Boot is a very helpful framework — it allows us to forget about the majority
of the configuration settings, many of which it opinionatedly autotunes.
By default Logback is used for logging. Appropriate Logback routing is also included
to ensure that dependent libraries that use Java Util Logging, Commons Logging,
Log4J, or SLF4J all work correctly.
The default logging level of the Logger is preset to INFO, meaning that TRACE and
DEBUG messages are not visible.
When you deploy your application to a servlet container or application server,
logging performed via the Java Util Logging API is not routed into your application’s
logs. This prevents logging performed by the container or other applications that
have been deployed to it from appearing in your application’s logs.
Logging in Spring

When using Spring 4, to manually exclude commons-logging in pom.xml, to avoid


potential clashes between the logging libraries. Spring 5 instead handles it
automatically, hence we don't need to do anything when using Spring Boot 2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
References​

https://www.baeldung.com/spring-boot-logging
https://www.loggly.com/ultimate-guide/java-logging-basics/
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.
html#boot-features-logging
https://www.tutorialspoint.com/log4j/log4j_overview.htm
SoftServe Confidential

You might also like