mirror of
https://github.com/itzg/docker-minecraft-server.git
synced 2026-11-19 05:55:20 +00:00
Support json as a value of LOG_CONSOLE_FORMAT / LOG_FILE_FORMAT for JSON log lines (#4393)
This commit is contained in:
1 parent
58fa0ad96a
commit
30b26426f4
6 files changed
+73
-3
No files matched your search
@@ -67,6 +67,30 @@ elif ${useGeneratedLogs}; then
|
||||
ROLLING_LOG_FILE_PATTERN='logs/%d{yyyy-MM-dd}-%i.log.gz'
|
||||
fi
|
||||
|
||||
# A format of "json" selects one JSON object per line with ECS field names.
|
||||
# Minecraft server jars don't bundle log4j-layout-template-json, and their launchers
|
||||
# load Log4j in an isolated class loader, so the JSON is produced by PatternLayout
|
||||
# with JSON-encoded fields. %enc{...}{JSON} is available in all Log4j versions
|
||||
# bundled since Minecraft 1.18.1 (Log4j 2.17).
|
||||
LOG_JSON_FORMAT=$(</image/log4j2-json-pattern.txt)
|
||||
# The format goes into an XML attribute, so escape it.
|
||||
# Replacements are quoted so bash 5.2's patsub_replacement doesn't expand "&".
|
||||
LOG_JSON_FORMAT=${LOG_JSON_FORMAT//&/"&"}
|
||||
LOG_JSON_FORMAT=${LOG_JSON_FORMAT//</"<"}
|
||||
LOG_JSON_FORMAT=${LOG_JSON_FORMAT//>/">"}
|
||||
LOG_JSON_FORMAT=${LOG_JSON_FORMAT//\"/"""}
|
||||
|
||||
for logFormatVar in LOG_CONSOLE_FORMAT LOG_TERMINAL_FORMAT LOG_FILE_FORMAT; do
|
||||
if [[ ${!logFormatVar:-} && ${!logFormatVar,,} == json ]]; then
|
||||
if versionLessThan 1.18.1; then
|
||||
logWarning "${logFormatVar}=json requires Minecraft 1.18.1 or newer, using the default format"
|
||||
printf -v "$logFormatVar" '%s' ''
|
||||
else
|
||||
printf -v "$logFormatVar" '%s' "$LOG_JSON_FORMAT"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Pattern format defaults (compatible with vanilla Minecraft)
|
||||
# Note: Can't use ${VAR:=default} syntax because } in the value breaks parsing
|
||||
if [ -z "${LOG_CONSOLE_FORMAT}" ]; then
|
||||
@@ -126,5 +150,10 @@ EOF
|
||||
# Apply the log4j2 configuration
|
||||
JVM_OPTS="-Dlog4j.configurationFile=log4j2.xml ${JVM_OPTS}"
|
||||
else
|
||||
for logFormatVar in LOG_CONSOLE_FORMAT LOG_TERMINAL_FORMAT LOG_FILE_FORMAT; do
|
||||
if [[ ${!logFormatVar:-} && ${!logFormatVar,,} == json ]]; then
|
||||
logWarning "${logFormatVar}=json is ignored since the image is not generating the log4j2 configuration. Set GENERATE_LOG4J2_CONFIG=true to use it."
|
||||
fi
|
||||
done
|
||||
rm -f "${LOGFILE}"
|
||||
fi
|
||||
Reference in new issue
Block a user