Matthias Andreas Benkard | 8090924 | 2022-02-03 20:47:47 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: GFDL-1.3-or-later |
| 4 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 5 | = Quarkus Google Cloud JSON Logging |
| 6 | Matthias Andreas Benkard |
| 7 | // Meta |
| 8 | :experimental: |
| 9 | :data-uri: |
| 10 | :sectnums: |
| 11 | :toc: |
| 12 | :stem: |
| 13 | :toclevels: 2 |
| 14 | :description: Quarkus Google Cloud JSON Logging Manual |
| 15 | :keywords: mulk |
| 16 | // Settings |
| 17 | :icons: font |
| 18 | :source-highlighter: rouge |
| 19 | |
| 20 | |
| 21 | Structured logging to standard output according to the Google Cloud |
| 22 | Logging specification. |
| 23 | |
| 24 | |
| 25 | == Summary |
| 26 | |
| 27 | This package contains a log formatter for JBoss Logging in the form of |
| 28 | a Quarkus plugin that implements the |
| 29 | https://cloud.google.com/logging/docs/structured-logging[Google Cloud |
| 30 | Logging JSON format] on standard output. |
| 31 | |
| 32 | It is possible to log unstructured text, structured data, or a mixture |
| 33 | of both depending on the situation. |
| 34 | |
| 35 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 36 | == Activation (Quarkus) |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 37 | |
| 38 | Add the runtime POM to your dependency list. As long as the JAR is on |
| 39 | the classpath at both build time and runtime, the log formatter |
| 40 | automatically registers itself on startup. |
| 41 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 42 | If you are using Maven: |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 43 | |
| 44 | [source,xml] |
| 45 | ---- |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 46 | <dependencies> |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 47 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 48 | <dependency> |
| 49 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 50 | <artifactId>quarkus-googlecloud-jsonlogging</artifactId> |
| 51 | <version>4.0.0</version> |
| 52 | </dependency> |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 53 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 54 | </dependencies> |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 55 | ---- |
| 56 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 57 | If you are using Gradle: |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 58 | |
| 59 | [source,groovy] |
| 60 | ---- |
| 61 | dependencies { |
Matthias Andreas Benkard | 2021024 | 2022-01-15 10:39:30 +0100 | [diff] [blame] | 62 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:4.0.0") |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 63 | } |
| 64 | ---- |
| 65 | |
| 66 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 67 | == Activation (Other Frameworks) |
| 68 | |
| 69 | If you are not using Quarkus, you can still make use of the `-core` |
| 70 | module and wire it into your application in a custom way. Read this |
| 71 | section for hints on how to do so. |
| 72 | |
| 73 | |
| 74 | === Installation |
| 75 | |
| 76 | If you are using Maven: |
| 77 | |
| 78 | [source,xml] |
| 79 | ---- |
| 80 | <dependencies> |
| 81 | |
| 82 | <dependency> |
| 83 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 84 | <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId> |
| 85 | <version>4.0.0</version> |
| 86 | </dependency> |
| 87 | |
| 88 | </dependencies> |
| 89 | ---- |
| 90 | |
| 91 | If you are using Gradle: |
| 92 | |
| 93 | [source,groovy] |
| 94 | ---- |
| 95 | dependencies { |
| 96 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:4.0.0") |
| 97 | } |
| 98 | ---- |
| 99 | |
| 100 | |
| 101 | === Wiring (Spring Boot) |
| 102 | |
| 103 | If you are using Spring Boot, the easiest way to integrate the log |
| 104 | formatter is by relying on `spring-boot-starter-logging` (which is |
| 105 | pulled in by `spring-boot-starter`), excluding Logback, and pulling in |
| 106 | JBoss Log Manager as the back end for SLF4J: |
| 107 | |
| 108 | [source,xml] |
| 109 | ---- |
| 110 | |
| 111 | <dependencies> |
| 112 | |
| 113 | <dependency> |
| 114 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 115 | <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId> |
| 116 | <version>4.0.0</version> |
| 117 | </dependency> |
| 118 | |
| 119 | <dependency> |
| 120 | <groupId>org.jboss.slf4j</groupId> |
| 121 | <artifactId>slf4j-jboss-logmanager</artifactId> |
| 122 | <version>1.1.0.Final</version> |
| 123 | </dependency> |
| 124 | |
| 125 | <dependency> |
| 126 | <groupId>org.springframework.boot</groupId> |
| 127 | <artifactId>spring-boot-starter</artifactId> |
| 128 | <exclusions> |
| 129 | <exclusion> |
| 130 | <groupId>ch.qos.logback</groupId> |
| 131 | <artifactId>logback-classic</artifactId> |
| 132 | </exclusion> |
| 133 | </exclusions> |
| 134 | </dependency> |
| 135 | |
| 136 | </dependencies> |
| 137 | ---- |
| 138 | |
| 139 | Create a text file called |
| 140 | `META-INF/services/org.jboss.logmanager.EmbeddedConfigurator` in your |
| 141 | `resources` directory and put the fully qualified name of |
| 142 | `DefaultEmbeddedConfigurator` into it: |
| 143 | |
| 144 | [source] |
| 145 | ---- |
| 146 | eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultEmbeddedConfigurator |
| 147 | ---- |
| 148 | |
| 149 | To configure `java.util.logging`, which is used by Tomcat by default, |
| 150 | create an entry in `application.properties` that points to |
| 151 | `logging.properties`: |
| 152 | |
| 153 | [source,properties] |
| 154 | ---- |
| 155 | logging.config = classpath:logging.properties |
| 156 | ---- |
| 157 | |
| 158 | Create the `logging.properties` file in your `resources` directory and |
| 159 | name `DefaultConsoleHandler` as a handler: |
| 160 | |
| 161 | [source,properties] |
| 162 | ---- |
| 163 | handlers = eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.ConsoleHandler |
| 164 | ---- |
| 165 | |
| 166 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 167 | == Usage |
| 168 | |
| 169 | Logging unstructured data requires no code changes. All logs are |
| 170 | automatically converted to Google-Cloud-Logging-compatible JSON. |
| 171 | |
| 172 | Structured data can be logged in one of 3 different ways: by passing |
| 173 | https://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 |
| 174 | and |
| 175 | https://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 |
| 176 | as parameters to individual log entries, by supplying |
| 177 | https://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 |
| 178 | and |
| 179 | https://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, |
| 180 | or by using the Mapped Diagnostic Context. |
| 181 | |
| 182 | |
| 183 | === Using Label and StructuredParameter |
| 184 | |
| 185 | Instances of |
| 186 | https://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] |
| 187 | and |
| 188 | https://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] |
| 189 | can be passed as log parameters to the `*f` family of logging |
| 190 | functions on JBoss Logging's |
| 191 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger]. |
| 192 | |
| 193 | Simple key–value pairs are represented by |
| 194 | https://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]. |
| 195 | |
| 196 | **Example:** |
| 197 | |
| 198 | [source,java] |
| 199 | ---- |
| 200 | logger.logf( |
| 201 | "Request rejected: unauthorized.", |
| 202 | Label.of("requestId", "123"), |
| 203 | KeyValueParameter.of("resource", "/users/mulk"), |
| 204 | KeyValueParameter.of("method", "PATCH"), |
| 205 | KeyValueParameter.of("reason", "invalid token")); |
| 206 | ---- |
| 207 | |
| 208 | Result: |
| 209 | |
| 210 | [source,json] |
| 211 | ---- |
| 212 | { |
| 213 | "jsonPayload": { |
| 214 | "message": "Request rejected: unauthorized.", |
| 215 | "resource": "/users/mulk", |
| 216 | "method": "PATCH", |
| 217 | "reason": "invalid token" |
| 218 | }, |
| 219 | "labels": { |
| 220 | "requestId": "123" |
| 221 | } |
| 222 | } |
| 223 | ---- |
| 224 | |
| 225 | |
| 226 | === Using LabelProvider and StructuredParameterProvider |
| 227 | |
| 228 | Any CDI beans that implement |
| 229 | https://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] |
| 230 | and |
| 231 | https://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] |
| 232 | are discovered at build time and consulted to provide labels and |
| 233 | parameters for each message that is logged. This can be used to |
| 234 | provide contextual information such as tracing and request IDs stored |
| 235 | in thread-local storage. |
| 236 | |
Matthias Andreas Benkard | 93ecfd1 | 2022-01-15 14:03:41 +0100 | [diff] [blame] | 237 | Alternatively, you can also register providers through the Java |
| 238 | https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ServiceLoader.html[ServiceLoader] |
| 239 | mechanism. |
| 240 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 241 | **Example:** |
| 242 | |
| 243 | [source,java] |
| 244 | ---- |
| 245 | @Singleton |
| 246 | @Unremovable |
| 247 | public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider { |
| 248 | |
| 249 | @Override |
| 250 | public StructuredParameter getParameter() { |
| 251 | var b = Json.createObjectBuilder(); |
| 252 | b.add("traceId", Span.current().getSpanContext().getTraceId()); |
| 253 | b.add("spanId", Span.current().getSpanContext().getSpanId()); |
| 254 | return () -> b; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public Collection<Label> getLabels() { |
| 259 | return List.of(Label.of("requestId", "123")); |
| 260 | } |
| 261 | } |
| 262 | ---- |
| 263 | |
| 264 | Result: |
| 265 | |
| 266 | [source,json] |
| 267 | ---- |
| 268 | { |
| 269 | "jsonPayload": { |
| 270 | "message": "Request rejected: unauthorized.", |
| 271 | "traceId": "39f9a49a9567a8bd7087b708f8932550", |
| 272 | "spanId": "c7431b14630b633d" |
| 273 | }, |
| 274 | "labels": { |
| 275 | "requestId": "123" |
| 276 | } |
| 277 | } |
| 278 | ---- |
| 279 | |
| 280 | |
| 281 | === Using the Mapped Diagnostic Context |
| 282 | |
| 283 | Any key–value pairs in JBoss Logging's thread-local |
| 284 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC] |
| 285 | are added to the resulting JSON. |
| 286 | |
| 287 | **Example:** |
| 288 | |
| 289 | [source,java] |
| 290 | ---- |
| 291 | MDC.put("resource", "/users/mulk"); |
| 292 | MDC.put("method", "PATCH"); |
| 293 | logger.logf("Request rejected: unauthorized."); |
| 294 | ---- |
| 295 | |
| 296 | Result: |
| 297 | |
| 298 | [source,json] |
| 299 | ---- |
| 300 | { |
| 301 | "jsonPayload": { |
| 302 | "message": "Request rejected: unauthorized.", |
| 303 | "resource": "/users/mulk", |
| 304 | "method": "PATCH" |
| 305 | } |
| 306 | } |
| 307 | ---- |