Skip to content

Commit c013c03

Browse files
m1a2stPaulRMellor
andauthored
doc: Document the JSON logging functionality in the proxy (kroxylicious#3667)
* add the json log doc Co-authored-by: PaulRMellor <47596553+PaulRMellor@users.noreply.github.com> Signed-off-by: Ken Huang <s7133700@gmail.com>
1 parent 58ca20f commit c013c03

3 files changed

Lines changed: 109 additions & 22 deletions

File tree

kroxylicious-docs/docs/_assemblies/assembly-proxy-monitoring.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ include::../_modules/monitoring/con-prometheus-metrics-proxy.adoc[leveloffset=+1
2828
include::../_modules/monitoring/con-{guide}-ingesting-metrics.adoc[leveloffset=+1]
2929
include::../_modules/monitoring/con-{guide}-integrating-micrometer.adoc[leveloffset=+1]
3030
include::../_modules/monitoring/proc-{guide}-setting-log-levels.adoc[leveloffset=+1]
31+
include::../_modules/monitoring/con-proxy-structured-log-fields.adoc[leveloffset=+1]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
////
2+
// Copyright Kroxylicious Authors.
3+
//
4+
// Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
////
6+
7+
:_mod-docs-content-type: CONCEPT
8+
9+
// file included in the following:
10+
//
11+
// assembly-proxy-monitoring.adoc
12+
13+
[id='con-proxy-structured-log-fields-{context}']
14+
= Structured log fields
15+
16+
[role="_abstract"]
17+
18+
Kroxylicious uses structured logging, attaching context as key-value pairs that are visible in both text and JSON output.
19+
In text format, they appear at the end of each log line.
20+
In JSON format, they appear as top-level fields.
21+
22+
The following table lists commonly emitted fields.
23+
24+
.Structured log fields
25+
[cols="1,3",options="header"]
26+
|===
27+
|Field |Description
28+
29+
|`sessionId`
30+
|Unique identifier for the client-to-proxy connection. Use this field to correlate all log messages for a single client session.
31+
32+
|`clientId`
33+
|The Kafka `client.id` value supplied by the connecting Kafka client.
34+
35+
|`apiKey`
36+
|The Kafka API key that identifies the request type (for example, `Produce`, `Fetch`, `Metadata`).
37+
38+
|`apiVersion`
39+
|The Kafka API version used in the request.
40+
41+
|`kekRef`
42+
|Reference to the Key Encryption Key (KEK) used by the Record Encryption filter. This field is emitted only when the Record Encryption filter is configured with the HashiCorp Vault KMS provider.
43+
44+
|`error`
45+
|Error message associated with a failure condition. This field is emitted across the proxy runtime, filters, and KMS providers when an error is encountered.
46+
|===

kroxylicious-docs/docs/_modules/monitoring/proc-proxy-setting-log-levels.adoc

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,82 @@
1818
1919
The Kroxylicious {github-release}[binary distribution^] includes https://logging.apache.org/log4j/2.x[log4j2] as the default logging backend.
2020
21-
When using the `bin/kroxylicious-start.sh` script from the binary distribution, you can set an environment variable to load a custom `log4j2` configuration file or change the root logging level.
22-
23-
.Environment variable to load a custom `log4j2` file
21+
When you use the `bin/kroxylicious-start.sh` script from the binary distribution, you can configure logging behavior using the following environment variables.
22+
23+
.Logging environment variables
24+
[cols="2,1,2,3",options="header"]
25+
|===
26+
|Environment variable |Default |Valid values |Description
27+
28+
|`KROXYLICIOUS_ROOT_LOG_LEVEL`
29+
|`WARN`
30+
|`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `OFF`
31+
|Log level for all loggers that are not otherwise configured.
32+
33+
|`KROXYLICIOUS_APP_LOG_LEVEL`
34+
|`INFO`
35+
|`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`, `OFF`
36+
|Log level for Kroxylicious loggers (`io.kroxylicious.*`). This setting takes precedence over `KROXYLICIOUS_ROOT_LOG_LEVEL` for application code.
37+
38+
|`KROXYLICIOUS_LOG_FORMAT`
39+
|`text`
40+
|`text`, `json`
41+
|Output format for log messages. Set to `json` to enable structured JSON output.
42+
43+
|`KROXYLICIOUS_LOG_JSON_TEMPLATE`
44+
|`classpath:LogstashJsonEventLayoutV1.json`
45+
|Any valid https://logging.apache.org/log4j/2.x/manual/json-template-layout.html[Log4j2 JsonTemplateLayout] event template URI.
46+
|JSON layout template used when `KROXYLICIOUS_LOG_FORMAT=json`. For example, use `classpath:EcsLayout.json` for Elastic Common Schema or `classpath:GelfLayout.json` for GELF format.
47+
48+
|`KROXYLICIOUS_LOGGING_OPTIONS`
49+
|(built-in defaults)
50+
|Any valid log4j2 JVM system properties.
51+
|Overrides the entire set of log4j2 JVM system properties. When set, the default `-Dlog4j2.configurationFile` and `-Dlog4j2.contextSelector` options are replaced. You must include all required options explicitly.
52+
|===
53+
54+
The following examples show how to set the application log level and configure a custom Log4j2 configuration.
55+
56+
.Setting the application log level to DEBUG
2457
[source,properties]
2558
----
26-
KROXYLICIOUS_LOGGING_OPTIONS="-Dlog4j2.configurationFile=/path/to/custom/log4j2.yaml"
59+
KROXYLICIOUS_APP_LOG_LEVEL="DEBUG"
2760
----
2861
29-
.Environment variable to change the root logging level
30-
[source,properties]
62+
NOTE: Setting the log level to `DEBUG` or `TRACE` produces very verbose logs.
63+
64+
.Loading a custom log4j2 configuration file
65+
[source,bash]
3166
----
32-
KROXYLICIOUS_ROOT_LOG_LEVEL="DEBUG"
67+
export KROXYLICIOUS_LOGGING_OPTIONS="-Dlog4j2.configurationFile=/path/to/custom/log4j2.yaml \
68+
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"
3369
----
3470
35-
NOTE: Setting the root log level to `DEBUG` or `TRACE` will produce very verbose logs.
71+
WARNING: Setting `KROXYLICIOUS_LOGGING_OPTIONS` replaces the built-in defaults.
72+
Always include `-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector` to preserve asynchronous logging.
3673
3774
[id='proc-proxy-json-log-format-{context}']
3875
== Switching to JSON log format
3976
40-
By default, Kroxylicious outputs logs in human-readable text format. You can switch to structured JSON output for integration with log aggregation systems.
77+
By default, Kroxylicious outputs logs in human-readable text format.
78+
You can switch to structured JSON output to integrate with log aggregation systems such as the Elastic Stack (ELK) or Splunk.
79+
80+
The JSON format uses https://logging.apache.org/log4j/2.x/manual/json-template-layout.html[Log4j2's JsonTemplateLayout] with the Logstash JSON Event V1 format.
81+
Structured context fields emitted by Kroxylicious appear as top-level fields alongside standard log metadata.
4182
4283
.Procedure
4384
44-
. Set the `KROXYLICIOUS_LOG_FORMAT` environment variable:
85+
. Set the `KROXYLICIOUS_LOG_FORMAT` environment variable before starting the proxy:
4586
+
4687
[source,bash]
4788
----
4889
export KROXYLICIOUS_LOG_FORMAT=json
49-
----
50-
51-
. Start Kroxylicious:
52-
+
53-
[source,bash]
54-
----
5590
bin/kroxylicious-start.sh
5691
----
5792
58-
.Expected output (JSON format)
93+
94+
+
95+
Example JSON log output:
96+
+
5997
6098
[source,json]
6199
----
@@ -67,17 +105,19 @@ bin/kroxylicious-start.sh
67105
"thread_name": "kafka-request-handler-1",
68106
"level": "INFO",
69107
"level_value": 20000,
70-
"stack_trace": null,
71108
"sessionId": "abc-123",
72-
"clientId": "producer-1"
109+
"clientId": "producer-1",
110+
"apiKey": "Produce"
73111
}
74112
----
75113
76-
NOTE: Structured logging parameters like `sessionId` and `clientId` appear as top-level fields in JSON output.
77114
78-
.Switching back to text format
115+
+
116+
When an exception is logged, a `stack_trace` field is included in the output.
117+
118+
. To switch back to text format, remove the environment variable or set it to `text`:
119+
+
79120
80-
Remove the environment variable or set it to `text`:
81121
82122
[source,bash]
83123
----

0 commit comments

Comments
 (0)