blob: 3136880bb0b4e46ced2c8c95397ee689413a30e1 [file] [log] [blame]
Matthias Andreas Benkard37e804b2021-09-04 22:38:08 +02001= Quarkus Google Cloud JSON Logging
2Matthias Andreas Benkard
3// Meta
4:experimental:
5:data-uri:
6:sectnums:
7:toc:
8:stem:
9:toclevels: 2
10:description: Quarkus Google Cloud JSON Logging Manual
11:keywords: mulk
12// Settings
13:icons: font
14:source-highlighter: rouge
15
16
17Structured logging to standard output according to the Google Cloud
18Logging specification.
19
20
21== Summary
22
23This package contains a log formatter for JBoss Logging in the form of
24a Quarkus plugin that implements the
25https://cloud.google.com/logging/docs/structured-logging[Google Cloud
26Logging JSON format] on standard output.
27
28It is possible to log unstructured text, structured data, or a mixture
29of both depending on the situation.
30
31
32
33To run the application with live reloading enabled:
34
35[source,console]
36----
37$ ./mvnw quarkus:dev
38----
39
40
41== Activation
42
43Add the runtime POM to your dependency list. As long as the JAR is on
44the classpath at both build time and runtime, the log formatter
45automatically registers itself on startup.
46
47
48=== Activation with Maven
49
50[source,xml]
51----
52<project>
53 ...
54
55 <dependencies>
56 ...
57
58 <dependency>
59 <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
60 <artifactId>quarkus-googlecloud-jsonlogging</artifactId>
61 <version>3.1.0</version>
62 </dependency>
63
64 ...
65 </dependencies>
66
67 ...
68</project>
69----
70
71
72=== Activation with Gradle
73
74[source,groovy]
75----
76dependencies {
77 ...
78
79 implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:3.1.0")
80
81 ...
82}
83----
84
85
86== Usage
87
88Logging unstructured data requires no code changes. All logs are
89automatically converted to Google-Cloud-Logging-compatible JSON.
90
91Structured data can be logged in one of 3 different ways: by passing
92https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/Label.html[Label]s
93and
94https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.html[StructuredParameter]s
95as parameters to individual log entries, by supplying
96https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.html[LabelProvider]s
97and
98https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.html[StructuredParameterProvider]s,
99or by using the Mapped Diagnostic Context.
100
101
102=== Using Label and StructuredParameter
103
104Instances of
105https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/Label.html[Label]
106and
107https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.html[StructuredParameter]
108can be passed as log parameters to the `*f` family of logging
109functions on JBoss Logging's
110https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger].
111
112Simple key–value pairs are represented by
113https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.html[KeyValueParameter].
114
115**Example:**
116
117[source,java]
118----
119logger.logf(
120 "Request rejected: unauthorized.",
121 Label.of("requestId", "123"),
122 KeyValueParameter.of("resource", "/users/mulk"),
123 KeyValueParameter.of("method", "PATCH"),
124 KeyValueParameter.of("reason", "invalid token"));
125----
126
127Result:
128
129[source,json]
130----
131{
132 "jsonPayload": {
133 "message": "Request rejected: unauthorized.",
134 "resource": "/users/mulk",
135 "method": "PATCH",
136 "reason": "invalid token"
137 },
138 "labels": {
139 "requestId": "123"
140 }
141}
142----
143
144
145=== Using LabelProvider and StructuredParameterProvider
146
147Any CDI beans that implement
148https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.html[LabelProvider]
149and
150https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging/3.1.0/eu.mulk.quarkus.googlecloud.jsonlogging/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.html[StructuredParameterProvider]
151are discovered at build time and consulted to provide labels and
152parameters for each message that is logged. This can be used to
153provide contextual information such as tracing and request IDs stored
154in thread-local storage.
155
156**Example:**
157
158[source,java]
159----
160@Singleton
161@Unremovable
162public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider {
163
164 @Override
165 public StructuredParameter getParameter() {
166 var b = Json.createObjectBuilder();
167 b.add("traceId", Span.current().getSpanContext().getTraceId());
168 b.add("spanId", Span.current().getSpanContext().getSpanId());
169 return () -> b;
170 }
171
172 @Override
173 public Collection<Label> getLabels() {
174 return List.of(Label.of("requestId", "123"));
175 }
176}
177----
178
179Result:
180
181[source,json]
182----
183{
184 "jsonPayload": {
185 "message": "Request rejected: unauthorized.",
186 "traceId": "39f9a49a9567a8bd7087b708f8932550",
187 "spanId": "c7431b14630b633d"
188 },
189 "labels": {
190 "requestId": "123"
191 }
192}
193----
194
195
196=== Using the Mapped Diagnostic Context
197
198Any key–value pairs in JBoss Logging's thread-local
199https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC]
200are added to the resulting JSON.
201
202**Example:**
203
204[source,java]
205----
206MDC.put("resource", "/users/mulk");
207MDC.put("method", "PATCH");
208logger.logf("Request rejected: unauthorized.");
209----
210
211Result:
212
213[source,json]
214----
215{
216 "jsonPayload": {
217 "message": "Request rejected: unauthorized.",
218 "resource": "/users/mulk",
219 "method": "PATCH"
220 }
221}
222----