summaryrefslogtreecommitdiff
path: root/README.adoc
blob: 6b479cae97427624c033030fa30d112100f611b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
//
// SPDX-License-Identifier: GFDL-1.3-or-later

= Quarkus Google Cloud JSON Logging
Matthias Andreas Benkard
// Meta
:experimental:
:data-uri:
:sectnums:
:toc:
:stem:
:toclevels: 2
:description: Quarkus Google Cloud JSON Logging Manual
:keywords: mulk
// Settings
:icons: font
:source-highlighter: rouge


Structured logging to standard output according to the Google Cloud
Logging specification.


== Summary

This package contains a log formatter for JBoss Logging in the form of
a Quarkus plugin that implements the
https://cloud.google.com/logging/docs/structured-logging[Google Cloud
Logging JSON format] on standard output.

It is possible to log unstructured text, structured data, or a mixture
of both depending on the situation.


== Activation (Quarkus)

Add the runtime POM to your dependency list. As long as the JAR is on
the classpath at both build time and runtime, the log formatter
automatically registers itself on startup.

If you are using Maven:

[source,xml]
----
<dependencies>

  <dependency>
    <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
    <artifactId>quarkus-googlecloud-jsonlogging</artifactId>
    <version>6.1.0</version>
  </dependency>

</dependencies>
----

If you are using Gradle:

[source,groovy]
----
dependencies {
  implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:6.1.0")
}
----

By default the extension is turned on.  You can turn the extension on
or off explicitly by configuring the `quarkus.log.console.google` key
in `application.properties`:

[source,properties]
----
quarkus.log.console.google = true
----


== Activation (Other Frameworks)

If you are not using Quarkus, you can still make use of the `-core`
module and wire it into your application in a custom way.  Read this
section for hints on how to do so.


=== Installation

If you are using Maven:

[source,xml]
----
<dependencies>

  <dependency>
    <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
    <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId>
    <version>6.1.0</version>
  </dependency>

</dependencies>
----

If you are using Gradle:

[source,groovy]
----
dependencies {
  implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0")
}
----


=== Wiring (java.util.logging)

This section explains how to use JBoss Log Manager as the log manager
for `java.util.logging` and how to configure it to use the log
formatter provided by this library.

Add a dependency on JBoss Log Manager and the `-core` module to your
project.

If you are using Maven:

[source,xml]
----
<dependencies>

  <dependency>
    <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
    <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId>
    <version>6.1.0</version>
  </dependency>

  <dependency>
    <groupId>org.jboss.logmanager</groupId>
    <artifactId>jboss-logmanager</artifactId>
    <version>3.0.2.Final</version>
  </dependency>

</dependencies>
----

If you are using Gradle:

[source,groovy]
----
dependencies {
  implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0")
  implementation("org.jboss.logmanager:jboss-logmanager:3.0.2.Final")
}
----

Create a text file called `org.jboss.logmanager.ConfiguratorFactory`
in your `resources/META-INF/services/` directory and put the fully
qualified name of this library's `DefaultConfiguratorFactory` into it:

[source]
----
eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConfiguratorFactory
----

Finally, set the `java.util.logging.manager` system property to
`org.jboss.logmanager.LogManager` when running your application:

[source,shell]
----
$ java -Djava.util.logging.manager=org.jboss.logmanager.LogManager ...
----


=== Wiring (Spring Boot)

If you are using Spring Boot, the easiest way to integrate the log
formatter is by relying on `spring-boot-starter-logging` (which is
pulled in by `spring-boot-starter`), excluding Logback, and pulling in
JBoss Log Manager as the back end for SLF4J.  In addition, configure
JBoss Log Manager as the log manager for `java.util.logging` by
setting the `java.util.logging.manager` system property to
`org.jboss.logmanager.LogManager`.

If you are using Maven:

[source,xml]
----
<project>

  <dependencies>

    <dependency>
      <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
      <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId>
      <version>6.1.0</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.slf4j</groupId>
      <artifactId>slf4j-jboss-logmanager</artifactId>
      <version>2.0.1.Final</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.logmanager</groupId>
      <artifactId>jboss-logmanager</artifactId>
      <version>3.0.2.Final</version>
    </dependency>

    <dependency>
      <groupId>org.jboss.logmanager</groupId>
      <artifactId>jboss-logmanager-embedded</artifactId>
      <version>1.2.0.Final</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <configuration>
          <systemPropertyVariables>
            <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
          </systemPropertyVariables>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>
----

If you are using Gradle:

[source,groovy]
----
configurations {
    all*.exclude(group: "ch.qos.logback", module: "logback-classic")
}

dependencies {
  implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0")
  implementation("org.jboss.logmanager:jboss-logmanager:3.0.2.Final")
  implementation("org.jboss.logmanager:jboss-logmanager-embedded:1.2.0.Final")
  implementation("org.jboss.slf4j:slf4j-jboss-logmanager:2.0.1.Final")
}

tasks.named("bootRun") {
  systemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager")
}
----

Create a text file called `org.jboss.logmanager.ConfiguratorFactory`
in your `resources/META-INF/services/` directory and put the fully
qualified name of this library's `DefaultConfiguratorFactory` into it:

[source]
----
eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConfiguratorFactory
----

Because Spring Boot configures the system logger with a minimum log
level of `SEVERE` by default, you may also want to configure the
logger using a `logging.properties` file.  To do so, first add an
entry to `application.properties` that points to the file:

[source,properties]
----
logging.config = classpath:logging.properties
----

Create the `logging.properties` file in your `resources` directory and
set the root logger level to something other than `SEVERE`:

[source,properties]
----
logger.level = INFO
----

Finally, add a `static` block to your Spring Boot application class
that disables the Tomcat URL stream handler factory, which conflicts
with the URL stream handler factory registered by the JBoss Modules
library:

[source,java]
----
@SpringBootApplication
public class Application {

  static {
    TomcatURLStreamHandlerFactory.disable();
  }

  // ...
}
----

== Usage

Logging unstructured data requires no code changes. All logs are
automatically converted to Google-Cloud-Logging-compatible JSON.

Structured data can be logged in one of 3 different ways: by passing
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/Label.html[Label]s
and
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.html[StructuredParameter]s
as parameters to individual log entries, by supplying
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.html[LabelProvider]s
and
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.html[StructuredParameterProvider]s,
or by using the Mapped Diagnostic Context.


=== Using Label and StructuredParameter

Instances of
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/Label.html[Label]
and
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameter.html[StructuredParameter]
can be passed as log parameters to the `*f` family of logging
functions on JBoss Logging's
https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger].

Simple key–value pairs are represented by
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.html[KeyValueParameter].

**Example:**

[source,java]
----
logger.logf(
  "Request rejected: unauthorized.",
  Label.of("requestId", "123"),
  KeyValueParameter.of("resource", "/users/mulk"),
  KeyValueParameter.of("method", "PATCH"),
  KeyValueParameter.of("reason", "invalid token"));
----

Result:

[source,json]
----
{
  "jsonPayload": {
    "message": "Request rejected: unauthorized.",
    "resource": "/users/mulk",
    "method": "PATCH",
    "reason": "invalid token"
  },
  "labels": {
    "requestId": "123"
  }
}
----


=== Using LabelProvider and StructuredParameterProvider

Any CDI beans that implement
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/LabelProvider.html[LabelProvider]
and
https://javadocs.dev/eu.mulk.quarkus-googlecloud-jsonlogging/quarkus-googlecloud-jsonlogging-core/6.1.0/eu.mulk.quarkus.googlecloud.jsonlogging.core/eu/mulk/quarkus/googlecloud/jsonlogging/StructuredParameterProvider.html[StructuredParameterProvider]
are discovered at build time and consulted to provide labels and
parameters for each message that is logged.  This can be used to
provide contextual information such as tracing and request IDs stored
in thread-local storage.

Alternatively, you can also register providers through the Java
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ServiceLoader.html[ServiceLoader]
mechanism.

**Example:**

[source,java]
----
@Singleton
@Unremovable
public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider {

  @Override
  public StructuredParameter getParameter() {
    var b = Json.createObjectBuilder();
    b.add("traceId", Span.current().getSpanContext().getTraceId());
    b.add("spanId", Span.current().getSpanContext().getSpanId());
    return () -> b;
  }

  @Override
  public Collection<Label> getLabels() {
    return List.of(Label.of("requestId", "123"));
  }
}
----

Result:

[source,json]
----
{
  "jsonPayload": {
    "message": "Request rejected: unauthorized.",
    "traceId": "39f9a49a9567a8bd7087b708f8932550",
    "spanId": "c7431b14630b633d"
  },
  "labels": {
    "requestId": "123"
  }
}
----


=== Using the Mapped Diagnostic Context

Any key–value pairs in JBoss Logging's thread-local
https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC]
are added to the resulting JSON.

**Example:**

[source,java]
----
MDC.put("resource", "/users/mulk");
MDC.put("method", "PATCH");
logger.logf("Request rejected: unauthorized.");
----

Result:

[source,json]
----
{
  "jsonPayload": {
    "message": "Request rejected: unauthorized.",
    "resource": "/users/mulk",
    "method": "PATCH"
  }
}
----