Skip to content

Commit 029aab6

Browse files
committed
Document Log4j2 extensions
See gh-32578
1 parent 916dddf commit 029aab6

File tree

1 file changed

+76
-3
lines changed
  • spring-boot-project/spring-boot-docs/src/docs/asciidoc/features

1 file changed

+76
-3
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/logging.adoc

+76-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[features.logging]]
22
== Logging
33
Spring Boot uses https://commons.apache.org/logging[Commons Logging] for all internal logging but leaves the underlying log implementation open.
4-
Default configurations are provided for {java-api}/java/util/logging/package-summary.html[Java Util Logging], https://logging.apache.org/log4j/2.x/[Log4J2], and https://logback.qos.ch/[Logback].
4+
Default configurations are provided for {java-api}/java/util/logging/package-summary.html[Java Util Logging], https://logging.apache.org/log4j/2.x/[Log4j2], and https://logback.qos.ch/[Logback].
55
In each case, loggers are pre-configured to use console output with optional file output also available.
66

77
By default, if you use the "`Starters`", Logback is used for logging.
@@ -158,7 +158,7 @@ As a result, specific configuration keys (such as `logback.configurationFile` fo
158158
[[features.logging.file-rotation]]
159159
=== File Rotation
160160
If you are using the Logback, it is possible to fine-tune log rotation settings using your `application.properties` or `application.yaml` file.
161-
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).
161+
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).
162162

163163
The following rotation policy properties are supported:
164164

@@ -433,7 +433,7 @@ Profile sections are supported anywhere within the `<configuration>` element.
433433
Use the `name` attribute to specify which profile accepts the configuration.
434434
The `<springProfile>` tag can contain a profile name (for example `staging`) or a profile expression.
435435
A profile expression allows for more complicated profile logic to be expressed, for example `production & (eu-central | eu-west)`.
436-
Check the {spring-framework-docs}/core.html#beans-definition-profiles-java[reference guide] for more details.
436+
Check the {spring-framework-docs}/core.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
437437
The following listing shows three sample profiles:
438438

439439
[source,xml,subs="verbatim",indent=0]
@@ -475,3 +475,76 @@ The following example shows how to expose properties for use within Logback:
475475

476476
NOTE: The `source` must be specified in kebab case (such as `my.property-name`).
477477
However, properties can be added to the `Environment` by using the relaxed rules.
478+
479+
480+
481+
[[features.logging.log4j2-extensions]]
482+
=== Log4j2 Extensions
483+
Spring Boot includes a number of extensions to Log4j2 that can help with advanced configuration.
484+
You can use these extensions in any `log4j2-spring.xml` configuration file.
485+
486+
NOTE: Because the standard `log4j2.xml` configuration file is loaded too early, you cannot use extensions in it.
487+
You need to either use `log4j2-spring.xml` or define a configprop:logging.config[] property.
488+
489+
NOTE: The extensions supersede the https://logging.apache.org/log4j/2.x/log4j-spring-boot/index.html[Spring Boot support] provided by Log4J.
490+
You should make sure not include the `org.apache.logging.log4j:log4j-spring-boot` module in your build.
491+
492+
493+
494+
[[features.logging.log4j2-extensions.profile-specific]]
495+
==== Profile-specific Configuration
496+
The `<SpringProfile>` tag lets you optionally include or exclude sections of configuration based on the active Spring profiles.
497+
Profile sections are supported anywhere within the `<Configuration>` element.
498+
Use the `name` attribute to specify which profile accepts the configuration.
499+
The `<SpringProfile>` tag can contain a profile name (for example `staging`) or a profile expression.
500+
A profile expression allows for more complicated profile logic to be expressed, for example `production & (eu-central | eu-west)`.
501+
Check the {spring-framework-docs}/core.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
502+
The following listing shows three sample profiles:
503+
504+
[source,xml,subs="verbatim",indent=0]
505+
----
506+
<SpringProfile name="staging">
507+
<!-- configuration to be enabled when the "staging" profile is active -->
508+
</SpringProfile>
509+
510+
<SpringProfile name="dev | staging">
511+
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
512+
</SpringProfile>
513+
514+
<SpringProfile name="!production">
515+
<!-- configuration to be enabled when the "production" profile is not active -->
516+
</SpringProfile>
517+
----
518+
519+
520+
521+
[[features.logging.log4j2-extensions.environment-properties-lookup]]
522+
==== Environment Properties Lookup
523+
If you want to refer to properties from your Spring `Environment` within your Log4j2 configuration you can use `spring:` prefixed https://logging.apache.org/log4j/2.x/manual/lookups.html[lookups].
524+
Doing so can be useful if you want to access values from your `application.properties` file in your Log4j2 configuration.
525+
526+
The following example shows how to set a Log4j2 property named `applicationName` that reads `spring.application.name` from the Spring `Environment`:
527+
528+
[source,xml,subs="verbatim",indent=0]
529+
----
530+
<Properties>
531+
<Property name="applicationName">${spring:spring.application.name}</property>
532+
</Properties>
533+
----
534+
535+
NOTE: The lookup key should be specified in kebab case (such as `my.property-name`).
536+
537+
538+
539+
[[features.logging.log4j2-extensions.environment-peroperty-source]]
540+
==== Log4j2 System Properties
541+
Log4j2 supports a number of https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties[System Properties] that can be used configure various items.
542+
For example, the `log4j2.skipJansi` system property can be used to configure if the `ConsoleAppender` will try to use a https://github.com/fusesource/jansi[Jansi] output stream on Windows.
543+
544+
All system properties that are loaded after the Log4J initialization can be obtained from the Spring `Environment`.
545+
For example, you could add `log4j2.skipJansi=false` to your `application.properties` file to have the `ConsoleAppender` use a Jansi on Windows.
546+
547+
NOTE: The Spring `Environment` is only considered when system properties and OS environment variables do not contain the value being loaded.
548+
549+
WARNING: System properties that are loaded during early Log4j2 initialization cannot reference the Spring `Environment`.
550+
For example, the property Log4j2 uses to allow the default Log4j2 implementation to be chosen is used before the Spring Environment is available.

0 commit comments

Comments
 (0)