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 | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 51 | <version>6.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 | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 62 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:6.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 | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 75 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 76 | == Activation (Other Frameworks) |
| 77 | |
| 78 | If you are not using Quarkus, you can still make use of the `-core` |
| 79 | module and wire it into your application in a custom way. Read this |
| 80 | section for hints on how to do so. |
| 81 | |
| 82 | |
| 83 | === Installation |
| 84 | |
| 85 | If you are using Maven: |
| 86 | |
| 87 | [source,xml] |
| 88 | ---- |
| 89 | <dependencies> |
| 90 | |
| 91 | <dependency> |
| 92 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 93 | <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId> |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 94 | <version>6.1.0</version> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 95 | </dependency> |
| 96 | |
| 97 | </dependencies> |
| 98 | ---- |
| 99 | |
| 100 | If you are using Gradle: |
| 101 | |
| 102 | [source,groovy] |
| 103 | ---- |
| 104 | dependencies { |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 105 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0") |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 106 | } |
| 107 | ---- |
| 108 | |
| 109 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 110 | === Wiring (java.util.logging) |
| 111 | |
| 112 | This section explains how to use JBoss Log Manager as the log manager |
| 113 | for `java.util.logging` and how to configure it to use the log |
| 114 | formatter provided by this library. |
| 115 | |
| 116 | Add a dependency on JBoss Log Manager and the `-core` module to your |
| 117 | project. |
| 118 | |
| 119 | If you are using Maven: |
| 120 | |
| 121 | [source,xml] |
| 122 | ---- |
| 123 | <dependencies> |
| 124 | |
| 125 | <dependency> |
| 126 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 127 | <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId> |
| 128 | <version>6.1.0</version> |
| 129 | </dependency> |
| 130 | |
| 131 | <dependency> |
| 132 | <groupId>org.jboss.logmanager</groupId> |
| 133 | <artifactId>jboss-logmanager</artifactId> |
| 134 | <version>3.0.2.Final</version> |
| 135 | </dependency> |
| 136 | |
| 137 | </dependencies> |
| 138 | ---- |
| 139 | |
| 140 | If you are using Gradle: |
| 141 | |
| 142 | [source,groovy] |
| 143 | ---- |
| 144 | dependencies { |
| 145 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0") |
| 146 | implementation("org.jboss.logmanager:jboss-logmanager:3.0.2.Final") |
| 147 | } |
| 148 | ---- |
| 149 | |
| 150 | Create a text file called `org.jboss.logmanager.ConfiguratorFactory` |
| 151 | in your `resources/META-INF/services/` directory and put the fully |
| 152 | qualified name of this library's `DefaultConfiguratorFactory` into it: |
| 153 | |
| 154 | [source] |
| 155 | ---- |
| 156 | eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConfiguratorFactory |
| 157 | ---- |
| 158 | |
| 159 | Finally, set the `java.util.logging.manager` system property to |
| 160 | `org.jboss.logmanager.LogManager` when running your application: |
| 161 | |
| 162 | [source,shell] |
| 163 | ---- |
| 164 | $ java -Djava.util.logging.manager=org.jboss.logmanager.LogManager ... |
| 165 | ---- |
| 166 | |
| 167 | |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 168 | === Wiring (Spring Boot) |
| 169 | |
| 170 | If you are using Spring Boot, the easiest way to integrate the log |
| 171 | formatter is by relying on `spring-boot-starter-logging` (which is |
| 172 | pulled in by `spring-boot-starter`), excluding Logback, and pulling in |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 173 | JBoss Log Manager as the back end for SLF4J. In addition, configure |
| 174 | JBoss Log Manager as the log manager for `java.util.logging` by |
| 175 | setting the `java.util.logging.manager` system property to |
| 176 | `org.jboss.logmanager.LogManager`. |
| 177 | |
| 178 | If you are using Maven: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 179 | |
| 180 | [source,xml] |
| 181 | ---- |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 182 | <project> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 183 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 184 | <dependencies> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 185 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 186 | <dependency> |
| 187 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 188 | <artifactId>quarkus-googlecloud-jsonlogging-core</artifactId> |
| 189 | <version>6.1.0</version> |
| 190 | </dependency> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 191 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 192 | <dependency> |
| 193 | <groupId>org.jboss.slf4j</groupId> |
| 194 | <artifactId>slf4j-jboss-logmanager</artifactId> |
| 195 | <version>2.0.1.Final</version> |
| 196 | </dependency> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 197 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 198 | <dependency> |
| 199 | <groupId>org.jboss.logmanager</groupId> |
| 200 | <artifactId>jboss-logmanager</artifactId> |
| 201 | <version>3.0.2.Final</version> |
| 202 | </dependency> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 203 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 204 | <dependency> |
| 205 | <groupId>org.springframework.boot</groupId> |
| 206 | <artifactId>spring-boot-starter</artifactId> |
| 207 | <exclusions> |
| 208 | <exclusion> |
| 209 | <groupId>ch.qos.logback</groupId> |
| 210 | <artifactId>logback-classic</artifactId> |
| 211 | </exclusion> |
| 212 | </exclusions> |
| 213 | </dependency> |
| 214 | |
| 215 | </dependencies> |
| 216 | |
| 217 | <build> |
| 218 | <plugins> |
| 219 | |
| 220 | <plugin> |
| 221 | <groupId>org.springframework.boot</groupId> |
| 222 | <artifactId>spring-boot-maven-plugin</artifactId> |
| 223 | <version>${spring-boot.version}</version> |
| 224 | <configuration> |
| 225 | <systemPropertyVariables> |
| 226 | <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> |
| 227 | </systemPropertyVariables> |
| 228 | </configuration> |
| 229 | </plugin> |
| 230 | |
| 231 | </plugins> |
| 232 | </build> |
| 233 | |
| 234 | </project> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 235 | ---- |
| 236 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 237 | If you are using Gradle: |
| 238 | |
| 239 | [source,groovy] |
| 240 | ---- |
| 241 | configurations { |
| 242 | all*.exclude(group: "ch.qos.logback", module: "logback-classic") |
| 243 | } |
| 244 | |
| 245 | dependencies { |
| 246 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0") |
| 247 | implementation("org.jboss.logmanager:jboss-logmanager:3.0.2.Final") |
| 248 | implementation("org.jboss.slf4j:slf4j-jboss-logmanager:2.0.1.Final") |
| 249 | } |
| 250 | |
| 251 | tasks.named("bootRun") { |
| 252 | systemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager") |
| 253 | } |
| 254 | ---- |
| 255 | |
| 256 | Create a text file called `org.jboss.logmanager.ConfiguratorFactory` |
| 257 | in your `resources/META-INF/services/` directory and put the fully |
| 258 | qualified name of this library's `DefaultConfiguratorFactory` into it: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 259 | |
| 260 | [source] |
| 261 | ---- |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 262 | eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConfiguratorFactory |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 263 | ---- |
| 264 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 265 | Because Spring Boot configures the system logger with a minimum log |
| 266 | level of `SEVERE` by default, you may also want to configure the |
| 267 | logger using a `logging.properties` file. To do so, first add an |
| 268 | entry to `application.properties` that points to the file: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 269 | |
| 270 | [source,properties] |
| 271 | ---- |
| 272 | logging.config = classpath:logging.properties |
| 273 | ---- |
| 274 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 275 | Finally, create the `logging.properties` file in your `resources` |
| 276 | directory and set the root logger level to something other than |
| 277 | `SEVERE`: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 278 | |
| 279 | [source,properties] |
| 280 | ---- |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 281 | logger.level = INFO |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 282 | ---- |
| 283 | |
| 284 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 285 | == Usage |
| 286 | |
| 287 | Logging unstructured data requires no code changes. All logs are |
| 288 | automatically converted to Google-Cloud-Logging-compatible JSON. |
| 289 | |
| 290 | Structured data can be logged in one of 3 different ways: by passing |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 291 | 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 |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 292 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 293 | 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 |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 294 | as parameters to individual log entries, by supplying |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 295 | 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 |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 296 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 297 | 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, |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 298 | or by using the Mapped Diagnostic Context. |
| 299 | |
| 300 | |
| 301 | === Using Label and StructuredParameter |
| 302 | |
| 303 | Instances of |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 304 | 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] |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 305 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 306 | 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] |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 307 | can be passed as log parameters to the `*f` family of logging |
| 308 | functions on JBoss Logging's |
| 309 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger]. |
| 310 | |
| 311 | Simple key–value pairs are represented by |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 312 | 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]. |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 313 | |
| 314 | **Example:** |
| 315 | |
| 316 | [source,java] |
| 317 | ---- |
| 318 | logger.logf( |
| 319 | "Request rejected: unauthorized.", |
| 320 | Label.of("requestId", "123"), |
| 321 | KeyValueParameter.of("resource", "/users/mulk"), |
| 322 | KeyValueParameter.of("method", "PATCH"), |
| 323 | KeyValueParameter.of("reason", "invalid token")); |
| 324 | ---- |
| 325 | |
| 326 | Result: |
| 327 | |
| 328 | [source,json] |
| 329 | ---- |
| 330 | { |
| 331 | "jsonPayload": { |
| 332 | "message": "Request rejected: unauthorized.", |
| 333 | "resource": "/users/mulk", |
| 334 | "method": "PATCH", |
| 335 | "reason": "invalid token" |
| 336 | }, |
| 337 | "labels": { |
| 338 | "requestId": "123" |
| 339 | } |
| 340 | } |
| 341 | ---- |
| 342 | |
| 343 | |
| 344 | === Using LabelProvider and StructuredParameterProvider |
| 345 | |
| 346 | Any CDI beans that implement |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 347 | 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] |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 348 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 349 | 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] |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 350 | are discovered at build time and consulted to provide labels and |
| 351 | parameters for each message that is logged. This can be used to |
| 352 | provide contextual information such as tracing and request IDs stored |
| 353 | in thread-local storage. |
| 354 | |
Matthias Andreas Benkard | 93ecfd1 | 2022-01-15 14:03:41 +0100 | [diff] [blame] | 355 | Alternatively, you can also register providers through the Java |
| 356 | https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ServiceLoader.html[ServiceLoader] |
| 357 | mechanism. |
| 358 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 359 | **Example:** |
| 360 | |
| 361 | [source,java] |
| 362 | ---- |
| 363 | @Singleton |
| 364 | @Unremovable |
| 365 | public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider { |
| 366 | |
| 367 | @Override |
| 368 | public StructuredParameter getParameter() { |
| 369 | var b = Json.createObjectBuilder(); |
| 370 | b.add("traceId", Span.current().getSpanContext().getTraceId()); |
| 371 | b.add("spanId", Span.current().getSpanContext().getSpanId()); |
| 372 | return () -> b; |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public Collection<Label> getLabels() { |
| 377 | return List.of(Label.of("requestId", "123")); |
| 378 | } |
| 379 | } |
| 380 | ---- |
| 381 | |
| 382 | Result: |
| 383 | |
| 384 | [source,json] |
| 385 | ---- |
| 386 | { |
| 387 | "jsonPayload": { |
| 388 | "message": "Request rejected: unauthorized.", |
| 389 | "traceId": "39f9a49a9567a8bd7087b708f8932550", |
| 390 | "spanId": "c7431b14630b633d" |
| 391 | }, |
| 392 | "labels": { |
| 393 | "requestId": "123" |
| 394 | } |
| 395 | } |
| 396 | ---- |
| 397 | |
| 398 | |
| 399 | === Using the Mapped Diagnostic Context |
| 400 | |
| 401 | Any key–value pairs in JBoss Logging's thread-local |
| 402 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC] |
| 403 | are added to the resulting JSON. |
| 404 | |
| 405 | **Example:** |
| 406 | |
| 407 | [source,java] |
| 408 | ---- |
| 409 | MDC.put("resource", "/users/mulk"); |
| 410 | MDC.put("method", "PATCH"); |
| 411 | logger.logf("Request rejected: unauthorized."); |
| 412 | ---- |
| 413 | |
| 414 | Result: |
| 415 | |
| 416 | [source,json] |
| 417 | ---- |
| 418 | { |
| 419 | "jsonPayload": { |
| 420 | "message": "Request rejected: unauthorized.", |
| 421 | "resource": "/users/mulk", |
| 422 | "method": "PATCH" |
| 423 | } |
| 424 | } |
| 425 | ---- |