Files
spring-boot-demo/log4j2-levels/README.md
T

3.8 KiB

log4j2-levels

Companion code for Log4j2 Logging Levels Explained: Configuration, Custom Levels and Changing Levels at Runtime on ankurm.com.

A small Spring Boot 4.1 application that uses Log4j2 instead of Logback and shows, with real output, how the level threshold works, how a level is inherited, why a line can print twice, where custom levels fit, what /actuator/loggers can and cannot change, and what a disabled log call still costs. There is no docs/ folder: the explanations live in the article, and every number in it comes from a file in output/.

Piece Version Checked against
Spring Boot 4.1.2 maven-metadata.xml for org.springframework.boot:spring-boot
Log4j2 2.25.5 what Spring Boot 4.1.2 manages (log4j2.version in spring-boot-dependencies); maven-metadata.xml lists 2.26.1 as newer
Logback (only in broken/) 1.5.38 what Spring Boot 4.1.2 manages
JDK 25 Temurin 25.0.5

Run it

export JAVA_HOME=/path/to/jdk-25
mvn -B test              # 9 tests, transcripts 01-08 and 13 written to output/
./scripts/run-all.sh     # tests, then the packaged jar under real JVMs (transcripts 09-12)

What is where

File What it is
src/main/resources/log4j2-spring.xml the configuration: two custom levels, a Console appender, five loggers (one per behaviour)
src/main/java/.../Levels.java DIAG (350) and PERF (450) registered with Level.forName
src/main/java/.../service/UserService.java the article's UserService, compilable, one real call per level
src/main/java/.../db/UserRepository.java in-memory repository whose package is configured at ERROR
src/main/java/.../billing/InvoiceService.java a package with no entry of its own in the XML
src/main/java/.../web/LogController.java GET /demo/log?name=... logs one line per level and reports which were enabled
src/test/java/.../ one test class per transcript; each asserts the numbers it prints
src/test/resources/props/log4j2-example.properties the properties-format version of the configuration, loaded by PropertiesConfigTest into its own LoggerContext
scripts/ run-all.sh, live-run.sh (jar plus curl), both-logging.sh (the broken classpath), lib.sh
broken/pom.xml the same sources with the Logback exclusion removed

Captured output

File Shows
00-test-summary.txt test counts
01-standard-levels.txt intLevel of the eight built-in levels and the isMoreSpecificThan rule
02-threshold-matrix.txt every threshold against every event level, with the lines the real appender printed
03-effective-levels.txt which configured logger each name inherits its level from
04-additivity.txt one line printed twice (appender on the logger plus default additivity)
05-user-service.txt UserService at DEBUG and at INFO
06-custom-levels.txt where DIAG and PERF sit and what each threshold lets through
07-argument-evaluation.txt how often toString() ran for six call shapes, with the level off and on
08-actuator-loggers.txt /actuator/loggers GET and POST, reset, rejected values, the listing
09-live-run.txt the same behaviour from the packaged jar under Tomcat
10-property-overrides.txt logging.level.* against log4j2-spring.xml, and a custom level in a property
11-both-logging.txt Logback and Log4j2 both on the classpath
12-dependency-tree.txt mvn dependency:tree for both builds
13-properties-config.txt the .properties format giving the same levels and additivity as the XML

11-both-logging.txt contains timestamps, a PID and a thread name; those differ on every run.