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
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Transcripts 11 and 12: what happens when Logback and Log4j2 are both on the classpath, and the
# dependency tree of the working build next to the broken one.
set -uo pipefail
source "$(dirname "$0")/lib.sh"
mkdir -p output
[ -f "$JAR" ] || mvn -B -q package -DskipTests
mvn -B -q -f broken/pom.xml package -DskipTests 2>&1 | grep -E 'ERROR|BUILD' | head -5
{
echo "# the same sources and the same dependencies as the module, minus the <exclusions> on spring-boot-starter-logging"
echo "\$ java -jar broken/target/log4j2-levels-both-logging-1.0.0.jar"
java -jar broken/target/log4j2-levels-both-logging-1.0.0.jar --server.port=18097 > /tmp/l4j-both.log 2>&1 &
BOTH=$!
for _ in $(seq 1 30); do kill -0 "$BOTH" 2>/dev/null || break; sleep 1; done
if kill -0 "$BOTH" 2>/dev/null; then
echo "(still running after 30 s: the application started)"
echo "\$ curl 'localhost:18097/demo/log?name=com.ankurm.log4j2levels.service.Probe' (the XML sets this package to DEBUG)"
curl -s --noproxy '*' "localhost:18097/demo/log?name=com.ankurm.log4j2levels.service.Probe" ; echo
echo "\$ curl localhost:18097/actuator/loggers/com.ankurm.log4j2levels.service"
curl -s --noproxy '*' "localhost:18097/actuator/loggers/com.ankurm.log4j2levels.service" ; echo
kill "$BOTH"; wait "$BOTH" 2>/dev/null
else
wait "$BOTH"; echo "exit code: $?"
fi
echo; echo '# what the application printed (SLF4J startup notes, then the lines from the request above)'
grep -E 'SLF4J|Probe' /tmp/l4j-both.log
} > output/11-both-logging.txt 2>&1
{
FILTER='org.apache.logging.log4j,ch.qos.logback,org.slf4j'
echo "# mvn dependency:tree for the module (Log4j2 only)"
mvn -B -f pom.xml dependency:tree -Dincludes="$FILTER" -DoutputFile=/tmp/l4j-tree-good.txt > /dev/null 2>&1
sed 's/^/ /' /tmp/l4j-tree-good.txt
echo
echo "# mvn dependency:tree for broken/pom.xml (exclusions removed)"
mvn -B -f broken/pom.xml dependency:tree -Dincludes="$FILTER" -DoutputFile=/tmp/l4j-tree-bad.txt > /dev/null 2>&1
sed 's/^/ /' /tmp/l4j-tree-bad.txt
} > output/12-dependency-tree.txt 2>&1
echo "wrote output/11-both-logging.txt and output/12-dependency-tree.txt"
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Shared helpers for the live scripts. Source this, do not run it.
set -uo pipefail
cd "$(dirname "$0")/.."
JAR=target/log4j2-levels-1.0.0.jar
PID=""
start_app() { # start_app <port> <logfile> [spring args...]
local port=$1 log=$2; shift 2
setsid nohup java -jar "$JAR" --server.port="$port" "$@" > "$log" 2>&1 < /dev/null &
PID=$!
for _ in $(seq 1 60); do
curl -s --noproxy '*' -o /dev/null "localhost:$port/actuator/health" && return 0
kill -0 "$PID" 2>/dev/null || return 1
sleep 1
done
return 1
}
stop_app() {
[ -n "$PID" ] && kill "$PID" 2>/dev/null && wait "$PID" 2>/dev/null
PID=""
}
# Print the console lines written since the last call (our pattern has no timestamp).
new_lines() { # new_lines <logfile>
local log=$1 seen=${SEEN:-0} total
total=$(wc -l < "$log")
tail -n +"$((seen + 1))" "$log"
SEEN=$total
}
http() { # http <method> <port> <path> [json body] -> "status body"
local method=$1 port=$2 path=$3 body=${4:-}
if [ -n "$body" ]; then
curl -s --noproxy '*' -X "$method" -H 'Content-Type: application/json' -d "$body" -w ' [HTTP %{http_code}]' "localhost:$port$path"
else
curl -s --noproxy '*' -X "$method" -w ' [HTTP %{http_code}]' "localhost:$port$path"
fi
}
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
# Transcripts 09 and 10: the real packaged jar under a real Tomcat, driven with curl.
# 09 levels per logger, Actuator changes, the ROOT lookup
# 10 logging.level.* properties and environment variables against log4j2-spring.xml
set -uo pipefail
source "$(dirname "$0")/lib.sh"
P=com.ankurm.log4j2levels
mkdir -p output
[ -f "$JAR" ] || mvn -B -q package -DskipTests
OUT=output/09-live-run.txt
{
start_app 18091 /tmp/l4j-live.log || { echo "app did not start"; cat /tmp/l4j-live.log; exit 1; }
SEEN=$(wc -l < /tmp/l4j-live.log)
echo "# one request per logger name: JSON says which levels are enabled, the console shows what printed"
for name in $P.service.Probe $P.billing.Probe $P.db.Probe $P.diag.Probe; do
echo "\$ curl 'localhost:18091/demo/log?name=$name'"
http GET 18091 "/demo/log?name=$name"; echo
new_lines /tmp/l4j-live.log
echo
done
echo "# raise billing to TRACE with Actuator, repeat the request"
echo "\$ curl -X POST -H 'Content-Type: application/json' -d '{\"configuredLevel\":\"TRACE\"}' localhost:18091/actuator/loggers/$P.billing"
http POST 18091 /actuator/loggers/$P.billing '{"configuredLevel":"TRACE"}'; echo
http GET 18091 "/demo/log?name=$P.billing.Probe"; echo
new_lines /tmp/l4j-live.log | grep -c 'TRACE' | sed 's/^/TRACE lines printed: /'
echo
echo "# the ROOT logger over real HTTP"
http GET 18091 /actuator/loggers/ROOT; echo
http GET 18091 /actuator/loggers/root; echo
echo "# a level the endpoint does not know"
http POST 18091 /actuator/loggers/$P.billing '{"configuredLevel":"DIAG"}'; echo
stop_app
} > "$OUT" 2>&1
OUT=output/10-property-overrides.txt
{
P2=$P.service
echo "# log4j2-spring.xml says: $P2 = DEBUG, root = INFO"
echo
echo "# 1. --logging.level.$P2=WARN on the command line"
start_app 18092 /tmp/l4j-p1.log --logging.level.$P2=WARN || { echo "did not start"; head -5 /tmp/l4j-p1.log; }
http GET 18092 /actuator/loggers/$P2; echo; stop_app
echo
echo "# 2. the same override as an environment variable"
LOGGING_LEVEL_COM_ANKURM_LOG4J2LEVELS_SERVICE=TRACE start_app 18093 /tmp/l4j-p2.log || { echo "did not start"; head -5 /tmp/l4j-p2.log; }
http GET 18093 /actuator/loggers/$P2; echo; stop_app
echo
echo "# 3. --logging.level.root=ERROR, which the XML's root level should be competing with"
start_app 18094 /tmp/l4j-p3.log --logging.level.root=ERROR || { echo "did not start"; head -5 /tmp/l4j-p3.log; }
http GET 18094 /actuator/loggers/$P.billing.InvoiceService; echo; stop_app
echo
echo "# 4. a custom level through the property: --logging.level.$P2=DIAG"
if start_app 18095 /tmp/l4j-p4.log --logging.level.$P2=DIAG; then
echo "app started"; http GET 18095 /actuator/loggers/$P2; echo; stop_app
else
echo "app did not start; first lines of its output:"
grep -vE '^\s+at |JAVA_TOOL|^$' /tmp/l4j-p4.log | head -16
fi
echo
echo "# 5. a custom level in a separate logger written as log4j2 itself reads it (see diag in the XML)"
start_app 18096 /tmp/l4j-p5.log || echo "did not start"
http GET 18096 /actuator/loggers/$P.diag; echo; stop_app
} > "$OUT" 2>&1
echo "wrote output/09-live-run.txt and output/10-property-overrides.txt"
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Regenerates every file under output/.
#
# ./scripts/run-all.sh
#
# Needs a JDK 25 and Maven 3.9. Transcripts 01-08 and 13 come out of the test suite, so each number in
# them is an assertion that fails the build if it stops being true. 09-12 are the packaged jar
# under real JVMs, driven with curl.
set -euo pipefail
cd "$(dirname "$0")/.."
mkdir -p output
echo "== test suite (transcripts 01-08 and 13)"
mvn -B test | tee /tmp/log4j2-levels-mvn.log | grep -E '^\[(INFO|WARNING|ERROR)\] Tests run:' | sed 's/^\[[A-Z]*\] //' > output/00-test-summary.txt
echo "== packaged jar"
mvn -B -q package -DskipTests
echo "== live runs (transcripts 09-10)"
./scripts/live-run.sh
echo "== both loggers on the classpath, dependency trees (transcripts 11-12)"
./scripts/both-logging.sh
echo
echo "output:"
ls -1 output