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