mirror of
https://github.com/itzg/docker-minecraft-server.git
synced 2026-11-18 21:51:21 +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
@@ -130,6 +130,30 @@ For full control over how log messages are formatted, you can customize the Log4
|
||||
- `LOG_TERMINAL_FORMAT` : Format for interactive terminal console (used with `docker attach`)
|
||||
Default: `[%d{HH:mm:ss} %level]: %msg%n`
|
||||
|
||||
### JSON log output
|
||||
|
||||
Set a format variable to `json` to output one JSON object per line instead of text, which suits log pipelines that parse container output, such as Loki, Elasticsearch, or Datadog. For example, to get JSON in `docker logs` while `logs/latest.log` stays plain text:
|
||||
|
||||
```yaml
|
||||
environment:
|
||||
GENERATE_LOG4J2_CONFIG: "true"
|
||||
LOG_CONSOLE_FORMAT: json
|
||||
```
|
||||
|
||||
`LOG_FILE_FORMAT` and `LOG_TERMINAL_FORMAT` accept `json` as well. Each line uses field names from the [Elastic Common Schema](https://www.elastic.co/guide/en/ecs/current/index.html), the same ones as Log4j's `EcsLayout.json` template:
|
||||
|
||||
```json
|
||||
{"@timestamp":"2026-11-12T13:50:43.459Z","ecs.version":"1.2.0","log.level":"INFO","message":"Done (3.214s)! For help, type \"help\"","process.thread.name":"Server thread","log.logger":"net.minecraft.server.dedicated.DedicatedServer"}
|
||||
```
|
||||
|
||||
When a log event carries an exception, an `error.stack_trace` field holds the stack trace. Timestamps are in UTC.
|
||||
|
||||
!!! note
|
||||
|
||||
- JSON output requires Minecraft 1.18.1 or newer. For older versions the default text format is used and a warning is logged.
|
||||
- Only the server's log output is JSON. Lines written by the image's startup scripts (prefixed with `[init]`) and output that bypasses Log4j, such as JVM warnings, remain plain text, so a log shipper should skip lines that don't parse as JSON.
|
||||
- Minecraft server jars don't include Log4j's `log4j-layout-template-json` module, so the JSON is built with a pattern layout rather than `JsonTemplateLayout`, and custom event templates aren't supported. For a different JSON shape, set the format variable to your own pattern using `%enc{...}{JSON}` to escape values.
|
||||
|
||||
### Example configurations
|
||||
|
||||
Simple timestamp customization (most common use case):
|
||||
|
||||
+3
-3
@@ -65,19 +65,19 @@ alternatively, you can mount: <code>/etc/localtime:/etc/localtime:ro
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>LOG_CONSOLE_FORMAT</code></td>
|
||||
<td>Log4j2 pattern for console output (what you see in <code>docker logs</code>)</td>
|
||||
<td>Log4j2 pattern for console output (what you see in <code>docker logs</code>), or <code>json</code> for <a href="../configuration/misc-options/#json-log-output">JSON lines</a></td>
|
||||
<td><code>[%d{HH:mm:ss}] [%t/%level]: %msg%n</code></td>
|
||||
<td>⬜️</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>LOG_FILE_FORMAT</code></td>
|
||||
<td>Log4j2 pattern for file logs (written to <code>logs/latest.log</code>)</td>
|
||||
<td>Log4j2 pattern for file logs (written to <code>logs/latest.log</code>), or <code>json</code> for <a href="../configuration/misc-options/#json-log-output">JSON lines</a></td>
|
||||
<td><code>[%d{HH:mm:ss}] [%t/%level]: %msg%n</code></td>
|
||||
<td>⬜️</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>LOG_TERMINAL_FORMAT</code></td>
|
||||
<td>Log4j2 pattern for interactive terminal console (used with <code>docker attach</code>)</td>
|
||||
<td>Log4j2 pattern for interactive terminal console (used with <code>docker attach</code>), or <code>json</code> for <a href="../configuration/misc-options/#json-log-output">JSON lines</a></td>
|
||||
<td><code>[%d{HH:mm:ss} %level]: %msg%n</code></td>
|
||||
<td>⬜️</td>
|
||||
</tr>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"@timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}{UTC}","ecs.version":"1.2.0","log.level":"%level","message":"%enc{%msg}{JSON}","process.thread.name":"%enc{%t}{JSON}","log.logger":"%enc{%c}{JSON}"%notEmpty{,"error.stack_trace":"%enc{%replace{%ex}{^\s+|\s+$}{}}{JSON}"}}%ex{none}%n
|
||||
@@ -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
|
||||
@@ -0,0 +1,11 @@
|
||||
services:
|
||||
mc:
|
||||
restart: "no"
|
||||
image: ${IMAGE_TO_TEST:-itzg/minecraft-server}
|
||||
environment:
|
||||
EULA: "TRUE"
|
||||
SETUP_ONLY: "TRUE"
|
||||
GENERATE_LOG4J2_CONFIG: "true"
|
||||
LOG_CONSOLE_FORMAT: json
|
||||
volumes:
|
||||
- ./data:/data
|
||||
@@ -0,0 +1,5 @@
|
||||
mc-image-helper assert fileExists /data/log4j2.xml
|
||||
# console appender uses the JSON pattern
|
||||
grep -A1 '<Console name="SysOut"' /data/log4j2.xml | grep -q '"ecs.version"'
|
||||
# file appender keeps the default text pattern
|
||||
grep -A1 '<RollingRandomAccessFile name="File"' /data/log4j2.xml | grep -qF 'pattern="[%d{HH:mm:ss}] [%t/%level]: %msg%n"'
|
||||
Reference in new issue
Block a user