Spring Boot Logging Usingpropertiesconfig

Elena Vance
-
spring boot logging usingpropertiesconfig

Logging Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4j2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available. By default, if you use the starters, 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.

Log Format The default log output from Spring Boot resembles the following example: 2026-03-26T08:23:57.086Z INFO 132382 --- [myapp] [ main] o.s.b.d.f.logexample.MyApplication : Starting MyApplication using Java 25.0.2 with PID 132382 (/opt/apps/myapp.jar started by myuser in /opt/apps/) 2026-03-26T08:23:57.103Z INFO 132382 --- [myapp] [ main] o.s.b.d.f.logexample.MyApplication : No active profile set, falling back to 1 default profile: "default" 2026-03-26T08:23:59.592Z INFO 132382 --- [myapp] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2026-03-26T08:23:59.646Z INFO 132382 --- [myapp] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2026-03-26T08:23:59.649Z INFO 132382 --- [myapp] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.20] 2026-03-26T08:23:59.783Z INFO 132382 --- [myapp] [ main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 2465 ms 2026-03-26T08:24:01.088Z INFO 132382 --- [myapp] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2026-03-26T08:24:01.106Z INFO 132382 --- [myapp] [ main] o.s.b.d.f.logexample.MyApplication : Started MyApplication in 5.165 seconds (process running for 5.877) ����������������������������������������������������������������������������������������������������������2026-03-26T08:24:01.128Z INFO 132382 --- [myapp] [ionShutdownHook] o.s.boot.tomcat.GracefulShutdown : Commencing graceful shutdown.

Waiting for active requests to complete 2026-03-26T08:24:01.143Z INFO 132382 --- [myapp] [tomcat-shutdown] o.s.boot.tomcat.GracefulShutdown : Graceful shutdown complete The following items are output: - Date and Time: Millisecond precision and easily sortable. - Log Level: ERROR ,WARN ,INFO ,DEBUG , orTRACE . - Process ID. - A --- separator to distinguish the start of actual log messages.

Application name: Enclosed in square brackets (logged by default only if spring.application.name is set) - Application group: Enclosed in square brackets (logged by default only if spring.application.group is set) - Thread name: Enclosed in square brackets (may be truncated for console output). - Correlation ID: If tracing is enabled (not shown in the sample above) - Logger name: This is usually the source class name (often abbreviated). - The log message. Console Output The default log configuration echoes messages to the console as they are written.

By default, ERROR -level, WARN -level, and INFO -level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag. $ java -jar myapp.jar --debug When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG level.

Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties ). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio). If you want to disable console-based logging, you can set the logging.console.enabled property to false . Color-coded Output If your terminal supports ANSI, color output is used to aid readability. You can set spring.output.ansi.enabled to a supported value to override the auto-detection.

Color coding is configured by using the %clr conversion word. In its simplest form, the converter colors the output according to the log level, as shown in the following example: %clr(%5p) The following table describes the mapping of log levels to colors: Alternatively, you can specify the color or style that should be used by providing it as an option to the conversion.

For example, to make the text yellow, use the following setting: %clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){yellow} The following colors and styles are supported: - black - blue - bright_black - bright_blue - bright_cyan - bright_green - bright_magenta - bright_red - bright_white - bright_yellow - cyan - faint - green - magenta - red - white - yellow File Output By default, Spring Boot logs only to the console and does not write log files.

If you want to write log files in addition to the console output, you need to set a logging.file.name or logging.file.path property (for example, in your application.properties ). If both properties are set, logging.file.path is ignored and only logging.file.name is used. The following table shows how the logging.* properties can be used together: Log files rotate when they reach 10 MB and, as with console output, ERROR -level, WARN -level, and INFO -level messages are logged by default. Note that Log4J2 requires logging.file.path to be set with such configuration.

File Rotation If you are using the Logback, it is possible to fine-tune log rotation settings using your application.properties or application.yaml file. For all other logging system, you will need to configure rotation settings directly yourself (for example, if you use Log4j2 then you could add a log4j2.xml or log4j2-spring.xml file).

The following rotation policy properties are supported: Log Levels All the supported logging systems can have the logger levels set in the Spring Environment (for example, in application.properties ) by using logging.level.<logger-name>=<level> where level is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. The root logger can be configured by using logging.level.root .

The following example shows potential logging settings in application.properties : - Properties - YAML logging.level.root=warn logging.level.org.springframework.web=debug logging.level.org.hibernate=error logging: level: root: "warn" org.springframework.web: "debug" org.hibernate: "error" It is also possible to set logging levels using environment variables. For example, LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG will set org.springframework.web to DEBUG . Log Groups It is often useful to be able to group related loggers together so that they can all be configured at the same time.

For example, you might commonly change the logging levels for all Tomcat related loggers, but you can not easily remember top level packages. To help with this, Spring Boot allows you to define logging groups in your Spring Environment .

For example, here is how you could define a “tomcat” group by adding it to your application.properties : - Properties - YAML logging.group.tomcat=org.apache.catalina,org.apache.coyote,org.apache.tomcat logging: group: tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat" Once defined, you can change the level for all the loggers in the group with a single line: - Properties - YAML logging.level.tomcat=trace logging: level: tomcat: "trace" Spring Boot includes the following pre-defined logging groups that can be used out-of-the-box: Using a Log Shutdown Hook In order to release logging resources when your application terminates, a shutdown hook that will trigger log system cleanup when the JVM exits is provided.

This shutdown hook is registered automatically unless your application is deployed as a war file. If your application has complex context hierarchies the shutdown hook may not meet your needs. If it does not, disable the shutdown hook and investigate the options provided directly by the underlying logging system. For example, Logback offers context selectors which allow each Logger to be created in its own context. You can use the logging.register-shutdown-hook property to disable the shutdown hook. Setting it to false will disable the registration.

You can set the property in your application.properties or application.yaml file: - Properties - YAML logging.register-shutdown-hook=false logging: register-shutdown-hook: false Custom Log Configuration The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config . You can force Spring Boot to use a particular logging system by using the org.springframework.boot.logging.LoggingSystem system property.

The value should be the fully qualified class name of a LoggingSystem implementation. You can also disable Spring Boot’s logging configuration entirely by using a value of none . Depending on your logging system, the following files are loaded: To help with the customization, some other properties are transferred from the Spring Environment to System properties. This allows the properties to be consumed by logging system configuration. For example, setting logging.file.name in application.properties or LOGGING_FILE_NAME as an environment variable will result in the LOG_FILE System property being set.

The properties that are transferred are described in the following table: If you use Logback, the following properties are also transferred: All the supported logging systems can consult System properties when parsing their configuration files. See the default configurations in spring-boot.jar for examples: Structured Logging Structured logging is a technique where the log output is written in a well-defined, often machine-readable format.

Spring Boot supports structured logging and has support for the following JSON formats out of the box: To enable structured logging, set the property logging.structured.format.console (for console output) or logging.structured.format.file (for file output) to the id of the format you want to use. If you are using Custom Log Configuration, update your configuration to respect CONSOLE_LOG_STRUCTURED_FORMAT and FILE_LOG_STRUCTURED_FORMAT system properties.

Take CONSOLE_LOG_STRUCTURED_FORMAT for example: - Logback - Log4j2 <!-- replace your encoder with StructuredLogEncoder --> <encoder class="org.springframework.boot.logging.logback.StructuredLogEncoder"> <format>${CONSOLE_LOG_STRUCTURED_FORMAT}</format> <charset>${CONSOLE_LOG_CHARSET}</charset> </encoder> You can also refer to the default configurations included in Spring Boot: <!-- replace your PatternLayout with StructuredLogLayout --> <StructuredLogLayout format="${sys:CONSOLE_LOG_STRUCTURED_FORMAT}" charset="${sys:CONSOLE_LOG_CHARSET}"/> You can also refer to the default configurations included in Spring Boot: Elastic Common Schema Elastic Common Schema is a JSON based logging format.

To enable the Elastic Common Schema log format, set the appropriate format property to ecs : - Properties - YAML logging.structured.format.console=ecs logging.structured.format.file=ecs logging: structured: format: console: ecs file: ecs A log line looks like this: {"@timestamp":"2024-01-01T10:15:00.067462556Z","log":{"level":"INFO","logger":"org.example.Application"},"process":{"pid":39599,"thread":{"name":"main"}},"service":{"name":"simple"},"message":"No active profile set, falling back to 1 default profile: \"default\"","ecs":{"version":"8.11"}} This format also adds every key value pair contained in the MDC to the JSON object. You can also use the SLF4J fluent logging API to add key value pairs to the logged JSON object with the addKeyValue method.

The service values can be customized using logging.structured.ecs.service properties: - Properties - YAML logging.structured.ecs.service.name=MyService logging.structured.ecs.service.version=1 logging.structured.ecs.service.environment=Production logging.structured.ecs.service.node-name=Primary logging: structured: ecs: service: name: MyService version: 1.0 environment: Production node-name: Primary Graylog Extended Log Format (GELF) Graylog Extended Log Format is a JSON based logging format for the Graylog log analytics platform.

To enable the Graylog Extended Log Format, set the appropriate format property to gelf : - Properties - YAML logging.structured.format.console=gelf logging.structured.format.file=gelf logging: structured: format: console: gelf file: gelf A log line looks like this: {"version":"1.1","short_message":"No active profile set, falling back to 1 default profile: \"default\"","timestamp":1725958035.857,"level":6,"_level_name":"INFO","_process_pid":47649,"_process_thread_name":"main","_log_logger":"org.example.Application"} This format also adds every key value pair contained in the MDC to the JSON object. You can also use the SLF4J fluent logging API to add key value pairs to the logged JSON object with the addKeyValue method.

Several fields can be customized using logging.structured.gelf properties: - Properties - YAML logging.structured.gelf.host=MyService logging.structured.gelf.service.version=1 logging: structured: gelf: host: MyService service: version: 1.0 Logstash JSON format The Logstash JSON format is a JSON based logging format.

To enable the Logstash JSON log format, set the appropriate format property to logstash : - Properties - YAML logging.structured.format.console=logstash logging.structured.format.file=logstash logging: structured: format: console: logstash file: logstash A log line looks like this: {"@timestamp":"2024-01-01T10:15:00.111037681+02:00","@version":"1","message":"No active profile set, falling back to 1 default profile: \"default\"","logger_name":"org.example.Application","thread_name":"main","level":"INFO","level_value":20000} This format also adds every key value pair contained in the MDC to the JSON object. You can also use the SLF4J fluent logging API to add key value pairs to the logged JSON object with the addKeyValue method.

If you add markers, these will show up in a tags string array in the JSON. Customizing Structured Logging JSON Spring Boot attempts to pick sensible defaults for the JSON names and values output for structured logging. Sometimes, however, you may want to make small adjustments to the JSON for your own needs. For example, it’s possible that you might want to change some of the names to match the expectations of your log ingestion system.

You might also want to filter out certain members since you don’t find them useful. The following properties allow you to change the way that structured logging JSON is written: For example, the following will exclude log.level , rename process.id to procid and add a fixed corpname field: - Properties - YAML logging.structured.json.exclude=log.level logging.structured.json.rename.process.id=procid logging.structured.json.add.corpname=mycorp logging: structured: json: exclude: log.level rename: process.id: procid add: corpname: mycorp Customizing Structured Logging Stack Traces Complete stack traces are included in the JSON output whenever a message is logged with an exception.

This amount of information may be costly to process by your log ingestion system, so you may want to tune the way that stack traces are printed. To do this, you can use one or more of the following properties: For example, the following will use root first stack traces, limit their length, and include hashes.

Properties - YAML logging.structured.json.stacktrace.root=first logging.structured.json.stacktrace.max-length=1024 logging.structured.json.stacktrace.include-common-frames=true logging.structured.json.stacktrace.include-hashes=true logging: structured: json: stacktrace: root: first max-length: 1024 include-common-frames: true include-hashes: true Supporting Other Structured Logging Formats The structured logging support in Spring Boot is extensible, allowing you to define your own custom format. To do this, implement the StructuredLogFormatter interface. The generic type argument has to be ILoggingEvent when using Logback and LogEvent when using Log4j2 (that means your implementation is tied to a specific logging system).

Your implementation is then called with the log event and returns the String to be logged, as seen in this example: - Java - Kotlin import ch.qos.logback.classic.spi.ILoggingEvent; import org.springframework.boot.logging.structured.StructuredLogFormatter; class MyCustomFormat implements StructuredLogFormatter<ILoggingEvent> { @Override public String format(ILoggingEvent event) { return "time=" + event.getInstant() + " level=" + event.getLevel() + " message=" + event.getMessage() + "\n"; } } import ch.qos.logback.classic.spi.ILoggingEvent import org.springframework.boot.logging.structured.StructuredLogFormatter class MyCustomFormat : StructuredLogFormatter<ILoggingEvent> { override fun format(event: ILoggingEvent): String { return "time=${event.instant} level=${event.level} message=${event.message}\n" } } As you can see in the example, you can return any format, it doesn’t have to be JSON.

To enable your custom format, set the property logging.structured.format.console or logging.structured.format.file to the fully qualified class name of your implementation. Your implementation can use some constructor parameters, which are injected automatically. Please see the JavaDoc of StructuredLogFormatter for more details. Logback Extensions Spring Boot includes a number of extensions to Logback that can help with advanced configuration. You can use these extensions in your logback-spring.xml configuration file.

ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]] ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]] Profile-specific Configuration The <springProfile> tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration> element. Use the name attribute to specify which profile accepts the configuration. The <springProfile> tag can contain a profile name (for example staging ) or a profile expression.

A profile expression allows for more complicated profile logic to be expressed, for example production & (eu-central | eu-west) . Check the Spring Framework reference guide for more details.

The following listing shows three sample profiles: <springProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> </springProfile> <springProfile name="dev | staging"> <!-- configuration to be enabled when the "dev" or "staging" profiles are active --> </springProfile> <springProfile name="!production"> <!-- configuration to be enabled when the "production" profile is not active --> </springProfile> Environment Properties The <springProperty> tag lets you expose properties from the Spring Environment for use within Logback.

Doing so can be useful if you want to access values from your application.properties file in your Logback configuration. The tag works in a similar way to Logback’s standard <property> tag. However, rather than specifying a direct value , you specify the source of the property (from the Environment ). If you need to store the property somewhere other than in local scope, you can use the scope attribute.

If you need a fallback value (in case the property is not set in the Environment ), you can use the defaultValue attribute. The following example shows how to expose properties for use within Logback: <springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/> <appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender"> <remoteHost>${fluentHost}</remoteHost> ... </appender> Log4j2 Extensions Spring Boot includes a number of extensions to Log4j2 that can help with advanced configuration. You can use these extensions in any log4j2-spring.xml configuration file.

Profile-specific Configuration The <SpringProfile> tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <Configuration> element. Use the name attribute to specify which profile accepts the configuration. The <SpringProfile> tag can contain a profile name (for example staging ) or a profile expression. A profile expression allows for more complicated profile logic to be expressed, for example production & (eu-central | eu-west) . Check the Spring Framework reference guide for more details.

The following listing shows three sample profiles: <SpringProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> </SpringProfile> <SpringProfile name="dev | staging"> <!-- configuration to be enabled when the "dev" or "staging" profiles are active --> </SpringProfile> <SpringProfile name="!production"> <!-- configuration to be enabled when the "production" profile is not active --> </SpringProfile> Environment Properties Lookup If you want to refer to properties from your Spring Environment within your Log4j2 configuration you can use spring: prefixed lookups.

Doing so can be useful if you want to access values from your application.properties file in your Log4j2 configuration. The following example shows how to set Log4j2 properties named applicationName and applicationGroup that read spring.application.name and spring.application.group from the Spring Environment : <Properties> <Property name="applicationName">${spring:spring.application.name}</Property> <Property name="applicationGroup">${spring:spring.application.group}</Property> </Properties> Log4j2 System Properties Log4j2 supports a number of System Properties that can be used to configure various items. For example, the log4j2.skipJansi system property can be used to configure if the ConsoleAppender will try to use a Jansi output stream on Windows.

All system properties that are loaded after the Log4j2 initialization can be obtained from the Spring Environment . For example, you could add log4j2.skipJansi=false to your application.properties file to have the ConsoleAppender use Jansi on Windows.

People Also Asked

Logging :: Spring Boot?

For example, you might commonly change the logging levels for all Tomcat related loggers, but you can not easily remember top level packages. To help with this, Spring Boot allows you to define logging groups in your Spring Environment .

Spring Boot Logging using Properties Config - HowToDoInJava?

You can set the property in your application.properties or application.yaml file: - Properties - YAML logging.register-shutdown-hook=false logging: register-shutdown-hook: false Custom Log Configuration The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the clas...

Logging in Spring Boot - Baeldung?

For example, you might commonly change the logging levels for all Tomcat related loggers, but you can not easily remember top level packages. To help with this, Spring Boot allows you to define logging groups in your Spring Environment .

How to Set the Logging Level with application.properties?

If you want to write log files in addition to the console output, you need to set a logging.file.name or logging.file.path property (for example, in your application.properties ). If both properties are set, logging.file.path is ignored and only logging.file.name is used. The following table shows how the logging.* properties can be used together: Log files rotate when they reach 10 MB and, as wit...

How to Configure Logging in Spring Boot - Logback and SLF4J Basics and ...?

Logging Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4j2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available. By default, if you use the starters, Logback is used for logging. Appropriate Logback routing is a...