Add log4j2-levels module: threshold rule, inheritance, additivity, custom levels, Actuator runtime changes and argument evaluation on Spring Boot 4.1.2 / Log4j2 2.25.5, with real transcripts

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b5SFkZoaMCukJNvZCV
This commit is contained in:
asmhatre
2026-11-17 23:20:53 +05:30
co-authored by Claude Sonnet 5.5
parent fe3fc689a6
commit c9a2b766bb
44 changed files with 1508 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.511 s -- in com.ankurm.log4j2levels.LevelsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 s -- in com.ankurm.log4j2levels.ArgumentEvaluationTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 s -- in com.ankurm.log4j2levels.ConfigTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.862 s -- in com.ankurm.log4j2levels.ActuatorLoggersTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 s -- in com.ankurm.log4j2levels.UserServiceTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 s -- in com.ankurm.log4j2levels.PropertiesConfigTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 s -- in com.ankurm.log4j2levels.CustomLevelsTest
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0
@@ -0,0 +1,16 @@
# Level.intLevel() for the eight built-in levels, from the log4j-api on the classpath
OFF intLevel=0
FATAL intLevel=100
ERROR intLevel=200
WARN intLevel=300
INFO intLevel=400
DEBUG intLevel=500
TRACE intLevel=600
ALL intLevel=2147483647
# the rule: an event is enabled when event.isMoreSpecificThan(threshold), i.e. its int is <= the threshold's
INFO.isMoreSpecificThan(DEBUG) = true
DEBUG.isMoreSpecificThan(INFO) = false
INFO.isMoreSpecificThan(INFO) = true
FATAL.isMoreSpecificThan(OFF) = false
TRACE.isMoreSpecificThan(ALL) = true
@@ -0,0 +1,10 @@
# rows: the logger's threshold. columns: the event's level. 'yes' = isEnabled(level).
# last column: lines the real Console appender printed when one event of each level was logged
threshold FATAL ERROR WARN INFO DEBUG TRACE printed
TRACE yes yes yes yes yes yes 6
DEBUG yes yes yes yes yes - 5
INFO yes yes yes yes - - 4
WARN yes yes yes - - - 3
ERROR yes yes - - - - 2
FATAL yes - - - - - 1
OFF - - - - - - 0
@@ -0,0 +1,9 @@
# configuration file Spring Boot handed to Log4j2: log4j2-spring.xml
logger effective level comes from
com.ankurm.log4j2levels.service.UserService DEBUG com.ankurm.log4j2levels.service
com.ankurm.log4j2levels.service.sub.Deeper DEBUG com.ankurm.log4j2levels.service
com.ankurm.log4j2levels.db.UserRepository ERROR com.ankurm.log4j2levels.db
com.ankurm.log4j2levels.billing.InvoiceService INFO root
org.example.Unrelated INFO root
com.ankurm.log4j2levels.diag.Whatever DIAG com.ankurm.log4j2levels.diag
+12
View File
@@ -0,0 +1,12 @@
# logger 'dup': <AppenderRef ref="Console"/> and additivity left at its default (true)
INFO c.a.l.dup - order 7 shipped
INFO c.a.l.dup - order 7 shipped
lines printed: 2
# logger 'service': <AppenderRef ref="Console"/> and additivity="false"
INFO c.a.l.service - order 7 shipped
lines printed: 1
# logger 'db': no appender of its own, so it uses the root's through additivity
ERROR c.a.l.db - order 7 failed
lines printed: 1
+45
View File
@@ -0,0 +1,45 @@
# service package at DEBUG (what log4j2-spring.xml configures); db package stays at ERROR
DEBUG c.a.l.s.UserService - Entering getUserById with userId: 1
INFO c.a.l.s.UserService - Fetching user with ID: 1
DEBUG c.a.l.s.UserService - Successfully retrieved user: asha
DEBUG c.a.l.s.UserService - Entering getUserById with userId: 99
INFO c.a.l.s.UserService - Fetching user with ID: 99
INFO c.a.l.s.UserService - User not found for ID: 99
DEBUG c.a.l.s.UserService - Entering getUserById with userId: null
WARN c.a.l.s.UserService - getUserById called with null userId
DEBUG c.a.l.s.UserService - Entering getUserById with userId: -1
INFO c.a.l.s.UserService - Fetching user with ID: -1
ERROR c.a.l.d.UserRepository - connection reset while loading user -1
ERROR c.a.l.s.UserService - Database error while fetching user with ID: -1
com.ankurm.log4j2levels.db.DatabaseException: connection reset
at com.ankurm.log4j2levels.db.UserRepository.findById(UserRepository.java:24)
INFO c.a.l.s.UserService - Starting batch user processing
DEBUG c.a.l.s.UserService - Processed 2 users in batch
INFO c.a.l.s.UserService - Batch processing completed, 2 processed
INFO c.a.l.s.UserService - Starting batch user processing
ERROR c.a.l.d.UserRepository - connection reset while loading user -1
FATAL c.a.l.s.UserService - Critical failure in batch processing, giving up.
com.ankurm.log4j2levels.db.DatabaseException: connection reset
at com.ankurm.log4j2levels.db.UserRepository.findById(UserRepository.java:24)
INFO c.a.l.s.UserService - Batch processing completed, 1 processed
lines: 23
# the same calls with the service package raised to INFO
INFO c.a.l.s.UserService - Fetching user with ID: 1
INFO c.a.l.s.UserService - Fetching user with ID: 99
INFO c.a.l.s.UserService - User not found for ID: 99
WARN c.a.l.s.UserService - getUserById called with null userId
INFO c.a.l.s.UserService - Fetching user with ID: -1
ERROR c.a.l.d.UserRepository - connection reset while loading user -1
ERROR c.a.l.s.UserService - Database error while fetching user with ID: -1
com.ankurm.log4j2levels.db.DatabaseException: connection reset
at com.ankurm.log4j2levels.db.UserRepository.findById(UserRepository.java:24)
INFO c.a.l.s.UserService - Starting batch user processing
INFO c.a.l.s.UserService - Batch processing completed, 2 processed
INFO c.a.l.s.UserService - Starting batch user processing
ERROR c.a.l.d.UserRepository - connection reset while loading user -1
FATAL c.a.l.s.UserService - Critical failure in batch processing, giving up.
com.ankurm.log4j2levels.db.DatabaseException: connection reset
at com.ankurm.log4j2levels.db.UserRepository.findById(UserRepository.java:24)
INFO c.a.l.s.UserService - Batch processing completed, 1 processed
lines: 17
+36
View File
@@ -0,0 +1,36 @@
# every registered level, most severe first (OFF and ALL left out)
FATAL 100
ERROR 200
WARN 300
DIAG 350
INFO 400
PERF 450
DEBUG 500
TRACE 600
# lookups
Level.getLevel("DIAG") = DIAG
Level.getLevel("NOPE") = null
Level.forName("DIAG", 999) = DIAG (intLevel 350)
# which of DIAG and PERF are enabled at each threshold
threshold DIAG PERF
WARN - -
DIAG yes -
INFO yes -
PERF yes yes
DEBUG yes yes
# logger configured in XML with level="diag" (lower case)
ERROR yes
WARN yes
DIAG yes
INFO -
PERF -
DEBUG -
# UserService.diagnose() with the service package at DEBUG: both custom lines print
DIAG c.a.l.s.UserService - cache size = 42
PERF c.a.l.s.UserService - lookup took 3 ms
# and with it at INFO: only DIAG prints
DIAG c.a.l.s.UserService - cache size = 42
@@ -0,0 +1,8 @@
# times user.toString() ran for one call, threshold INFO (debug off) vs DEBUG (debug on)
call debug off debug on
debug("User: " + user) 1 1
debug("User: {}", user) 0 1
debug("User: {}", user.toString()) 1 1
debug(() -> "User: " + user) 0 1
debug("User: {}", () -> user.toString()) 0 1
if (isDebugEnabled()) debug("User: " + user) 0 1
@@ -0,0 +1,40 @@
# 1. a logger the XML configures, and one that only inherits from it
GET /actuator/loggers/com.ankurm.log4j2levels.service -> 200 {"configuredLevel":"DEBUG","effectiveLevel":"DEBUG"}
GET /actuator/loggers/com.ankurm.log4j2levels.service.UserService -> 200 {"effectiveLevel":"DEBUG"}
# 2. raise the service package to TRACE and log a trace line through UserService's logger
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"TRACE"} -> 204
GET /actuator/loggers/com.ankurm.log4j2levels.service.UserService -> 200 {"effectiveLevel":"TRACE"}
printed: TRACE c.a.l.s.UserService - probe
# 3. reset it with configuredLevel null
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":null} -> 204
GET /actuator/loggers/com.ankurm.log4j2levels.service -> 200 {"configuredLevel":"INFO","effectiveLevel":"INFO"}
GET /actuator/loggers/com.ankurm.log4j2levels.service.UserService -> 200 {"effectiveLevel":"INFO"}
a debug line after the reset printed: nothing
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"DEBUG"} -> 204
# 4. a logger with no entry in the XML: set, then reset
GET /actuator/loggers/com.ankurm.log4j2levels.billing.InvoiceService -> 200 {"effectiveLevel":"INFO"}
POST /actuator/loggers/com.ankurm.log4j2levels.billing {"configuredLevel":"TRACE"} -> 204
GET /actuator/loggers/com.ankurm.log4j2levels.billing.InvoiceService -> 200 {"effectiveLevel":"TRACE"}
POST /actuator/loggers/com.ankurm.log4j2levels.billing {"configuredLevel":null} -> 204
GET /actuator/loggers/com.ankurm.log4j2levels.billing.InvoiceService -> 200 {"effectiveLevel":"INFO"}
# 5. what the endpoint will not take
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"DIAG"} -> 400
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"debug"} -> 204
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"LOUD"} -> 400
POST /actuator/loggers/com.ankurm.log4j2levels.service {"configuredLevel":"DEBUG"} -> 204
# 6. the root logger
GET /actuator/loggers/ROOT -> 404
POST /actuator/loggers/ROOT {"configuredLevel":"WARN"} -> 204
GET /actuator/loggers/com.ankurm.log4j2levels.billing.InvoiceService -> 200 {"effectiveLevel":"WARN"}
POST /actuator/loggers/ROOT {"configuredLevel":"INFO"} -> 204
GET /actuator/loggers/ROOT -> 404
# 7. the full listing: the levels it offers, and how the root logger is named
GET /actuator/loggers -> "levels":["OFF","FATAL","ERROR","WARN","INFO","DEBUG","TRACE"]
entry: "ROOT":{"effectiveLevel":"INFO"}
lists com.ankurm.log4j2levels.service: true
+42
View File
@@ -0,0 +1,42 @@
# one request per logger name: JSON says which levels are enabled, the console shows what printed
$ curl 'localhost:18091/demo/log?name=com.ankurm.log4j2levels.service.Probe'
{"FATAL":true,"ERROR":true,"WARN":true,"DIAG":true,"INFO":true,"PERF":true,"DEBUG":true,"TRACE":false} [HTTP 200]
FATAL c.a.l.s.Probe - demo line at FATAL
ERROR c.a.l.s.Probe - demo line at ERROR
WARN c.a.l.s.Probe - demo line at WARN
DIAG c.a.l.s.Probe - demo line at DIAG
INFO c.a.l.s.Probe - demo line at INFO
PERF c.a.l.s.Probe - demo line at PERF
DEBUG c.a.l.s.Probe - demo line at DEBUG
$ curl 'localhost:18091/demo/log?name=com.ankurm.log4j2levels.billing.Probe'
{"FATAL":true,"ERROR":true,"WARN":true,"DIAG":true,"INFO":true,"PERF":false,"DEBUG":false,"TRACE":false} [HTTP 200]
FATAL c.a.l.b.Probe - demo line at FATAL
ERROR c.a.l.b.Probe - demo line at ERROR
WARN c.a.l.b.Probe - demo line at WARN
DIAG c.a.l.b.Probe - demo line at DIAG
INFO c.a.l.b.Probe - demo line at INFO
$ curl 'localhost:18091/demo/log?name=com.ankurm.log4j2levels.db.Probe'
{"FATAL":true,"ERROR":true,"WARN":false,"DIAG":false,"INFO":false,"PERF":false,"DEBUG":false,"TRACE":false} [HTTP 200]
FATAL c.a.l.d.Probe - demo line at FATAL
ERROR c.a.l.d.Probe - demo line at ERROR
$ curl 'localhost:18091/demo/log?name=com.ankurm.log4j2levels.diag.Probe'
{"FATAL":true,"ERROR":true,"WARN":true,"DIAG":true,"INFO":false,"PERF":false,"DEBUG":false,"TRACE":false} [HTTP 200]
FATAL c.a.l.d.Probe - demo line at FATAL
ERROR c.a.l.d.Probe - demo line at ERROR
WARN c.a.l.d.Probe - demo line at WARN
DIAG c.a.l.d.Probe - demo line at DIAG
# raise billing to TRACE with Actuator, repeat the request
$ curl -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"TRACE"}' localhost:18091/actuator/loggers/com.ankurm.log4j2levels.billing
[HTTP 204]
{"FATAL":true,"ERROR":true,"WARN":true,"DIAG":true,"INFO":true,"PERF":true,"DEBUG":true,"TRACE":true} [HTTP 200]
TRACE lines printed: 1
# the ROOT logger over real HTTP
[HTTP 404]
[HTTP 404]
# a level the endpoint does not know
{"timestamp":"2026-11-17T17:49:52.029Z","status":400,"error":"Bad Request","path":"/actuator/loggers/com.ankurm.log4j2levels.billing"} [HTTP 400]
@@ -0,0 +1,32 @@
# log4j2-spring.xml says: com.ankurm.log4j2levels.service = DEBUG, root = INFO
# 1. --logging.level.com.ankurm.log4j2levels.service=WARN on the command line
{"configuredLevel":"WARN","effectiveLevel":"WARN"} [HTTP 200]
# 2. the same override as an environment variable
{"configuredLevel":"TRACE","effectiveLevel":"TRACE"} [HTTP 200]
# 3. --logging.level.root=ERROR, which the XML's root level should be competing with
{"effectiveLevel":"ERROR"} [HTTP 200]
# 4. a custom level through the property: --logging.level.com.ankurm.log4j2levels.service=DIAG
app did not start; first lines of its output:
ERROR o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'logging.level.com.ankurm.log4j2levels.service' to org.springframework.boot.logging.LogLevel:
Property: logging.level.com.ankurm.log4j2levels.service
Value: "DIAG"
Origin: "logging.level.com.ankurm.log4j2levels.service" from property source "commandLineArgs"
Reason: failed to convert java.lang.String to org.springframework.boot.logging.LogLevel (caused by java.lang.IllegalArgumentException: No enum constant org.springframework.boot.logging.LogLevel.DIAG)
Action:
Update your application's configuration. The following values are valid:
DEBUG
ERROR
FATAL
INFO
# 5. a custom level in a separate logger written as log4j2 itself reads it (see diag in the XML)
{"configuredLevel":"DIAG","effectiveLevel":"DIAG"} [HTTP 200]
+20
View File
@@ -0,0 +1,20 @@
# the same sources and the same dependencies as the module, minus the <exclusions> on spring-boot-starter-logging
$ java -jar broken/target/log4j2-levels-both-logging-1.0.0.jar
(still running after 30 s: the application started)
$ curl 'localhost:18097/demo/log?name=com.ankurm.log4j2levels.service.Probe' (the XML sets this package to DEBUG)
{"FATAL":true,"ERROR":true,"WARN":true,"DIAG":true,"INFO":true,"PERF":true,"DEBUG":false,"TRACE":false}
$ curl localhost:18097/actuator/loggers/com.ankurm.log4j2levels.service
{"effectiveLevel":"INFO"}
# what the application printed (SLF4J startup notes, then the lines from the request above)
SLF4J(W): Class path contains multiple SLF4J providers.
SLF4J(W): Found provider [ch.qos.logback.classic.spi.LogbackServiceProvider@14acaea5]
SLF4J(W): Found provider [org.apache.logging.slf4j.SLF4JServiceProvider@46d56d67]
SLF4J(W): See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J(I): Actual provider is of type [ch.qos.logback.classic.spi.LogbackServiceProvider@14acaea5]
2026-11-17T23:20:47.393+05:30 ERROR 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at FATAL
2026-11-17T23:20:47.394+05:30 ERROR 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at ERROR
2026-11-17T23:20:47.394+05:30 WARN 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at WARN
2026-11-17T23:20:47.394+05:30 WARN 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at DIAG
2026-11-17T23:20:47.394+05:30 INFO 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at INFO
2026-11-17T23:20:47.394+05:30 INFO 3732 --- [log4j2-levels] [io-18097-exec-1] com.ankurm.log4j2levels.service.Probe : demo line at PERF
@@ -0,0 +1,24 @@
# mvn dependency:tree for the module (Log4j2 only)
com.ankurm:log4j2-levels:jar:1.0.0
\- org.springframework.boot:spring-boot-starter-log4j2:jar:4.1.2:compile
+- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.25.5:compile
| +- org.apache.logging.log4j:log4j-api:jar:2.25.5:compile
| \- org.slf4j:slf4j-api:jar:2.0.20:compile
+- org.apache.logging.log4j:log4j-core:jar:2.25.5:compile
\- org.apache.logging.log4j:log4j-jul:jar:2.25.5:compile
# mvn dependency:tree for broken/pom.xml (exclusions removed)
com.ankurm:log4j2-levels-both-logging:jar:1.0.0
+- org.springframework.boot:spring-boot-starter-webmvc:jar:4.1.2:compile
| \- org.springframework.boot:spring-boot-starter:jar:4.1.2:compile
| \- org.springframework.boot:spring-boot-starter-logging:jar:4.1.2:compile
| +- ch.qos.logback:logback-classic:jar:1.5.38:compile
| | \- ch.qos.logback:logback-core:jar:1.5.38:compile
| +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.25.5:compile
| \- org.slf4j:jul-to-slf4j:jar:2.0.20:compile
\- org.springframework.boot:spring-boot-starter-log4j2:jar:4.1.2:compile
+- org.apache.logging.log4j:log4j-slf4j2-impl:jar:2.25.5:compile
| +- org.apache.logging.log4j:log4j-api:jar:2.25.5:compile
| \- org.slf4j:slf4j-api:jar:2.0.20:compile
+- org.apache.logging.log4j:log4j-core:jar:2.25.5:compile
\- org.apache.logging.log4j:log4j-jul:jar:2.25.5:compile
@@ -0,0 +1,9 @@
# loaded from log4j2-example.properties
logger effective
com.ankurm.log4j2levels.service.UserService DEBUG
com.ankurm.log4j2levels.db.UserRepository ERROR
com.ankurm.log4j2levels.billing.InvoiceService INFO
org.example.Unrelated INFO
service additivity = false
db additivity = true
appenders: [Console]