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> |
Matthias Andreas Benkard | be47648 | 2023-09-27 18:06:56 +0200 | [diff] [blame^] | 205 | <groupId>org.jboss.logmanager</groupId> |
| 206 | <artifactId>jboss-logmanager-embedded</artifactId> |
| 207 | <version>1.2.0.Final</version> |
| 208 | </dependency> |
| 209 | |
| 210 | <dependency> |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 211 | <groupId>org.springframework.boot</groupId> |
| 212 | <artifactId>spring-boot-starter</artifactId> |
| 213 | <exclusions> |
| 214 | <exclusion> |
| 215 | <groupId>ch.qos.logback</groupId> |
| 216 | <artifactId>logback-classic</artifactId> |
| 217 | </exclusion> |
| 218 | </exclusions> |
| 219 | </dependency> |
| 220 | |
| 221 | </dependencies> |
| 222 | |
| 223 | <build> |
| 224 | <plugins> |
| 225 | |
| 226 | <plugin> |
| 227 | <groupId>org.springframework.boot</groupId> |
| 228 | <artifactId>spring-boot-maven-plugin</artifactId> |
| 229 | <version>${spring-boot.version}</version> |
| 230 | <configuration> |
| 231 | <systemPropertyVariables> |
| 232 | <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> |
| 233 | </systemPropertyVariables> |
| 234 | </configuration> |
| 235 | </plugin> |
| 236 | |
| 237 | </plugins> |
| 238 | </build> |
| 239 | |
| 240 | </project> |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 241 | ---- |
| 242 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 243 | If you are using Gradle: |
| 244 | |
| 245 | [source,groovy] |
| 246 | ---- |
| 247 | configurations { |
| 248 | all*.exclude(group: "ch.qos.logback", module: "logback-classic") |
| 249 | } |
| 250 | |
| 251 | dependencies { |
| 252 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging-core:6.1.0") |
| 253 | implementation("org.jboss.logmanager:jboss-logmanager:3.0.2.Final") |
Matthias Andreas Benkard | be47648 | 2023-09-27 18:06:56 +0200 | [diff] [blame^] | 254 | implementation("org.jboss.logmanager:jboss-logmanager-embedded:1.2.0.Final") |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 255 | implementation("org.jboss.slf4j:slf4j-jboss-logmanager:2.0.1.Final") |
| 256 | } |
| 257 | |
| 258 | tasks.named("bootRun") { |
| 259 | systemProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager") |
| 260 | } |
| 261 | ---- |
| 262 | |
| 263 | Create a text file called `org.jboss.logmanager.ConfiguratorFactory` |
| 264 | in your `resources/META-INF/services/` directory and put the fully |
| 265 | qualified name of this library's `DefaultConfiguratorFactory` into it: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 266 | |
| 267 | [source] |
| 268 | ---- |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 269 | eu.mulk.quarkus.googlecloud.jsonlogging.logmanager.DefaultConfiguratorFactory |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 270 | ---- |
| 271 | |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 272 | Because Spring Boot configures the system logger with a minimum log |
| 273 | level of `SEVERE` by default, you may also want to configure the |
| 274 | logger using a `logging.properties` file. To do so, first add an |
| 275 | entry to `application.properties` that points to the file: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 276 | |
| 277 | [source,properties] |
| 278 | ---- |
| 279 | logging.config = classpath:logging.properties |
| 280 | ---- |
| 281 | |
Matthias Andreas Benkard | be47648 | 2023-09-27 18:06:56 +0200 | [diff] [blame^] | 282 | Create the `logging.properties` file in your `resources` directory and |
| 283 | set the root logger level to something other than `SEVERE`: |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 284 | |
| 285 | [source,properties] |
| 286 | ---- |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 287 | logger.level = INFO |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 288 | ---- |
| 289 | |
Matthias Andreas Benkard | be47648 | 2023-09-27 18:06:56 +0200 | [diff] [blame^] | 290 | Finally, add a `static` block to your Spring Boot application class |
| 291 | that disables the Tomcat URL stream handler factory, which conflicts |
| 292 | with the URL stream handler factory registered by the JBoss Modules |
| 293 | library: |
| 294 | |
| 295 | [source,java] |
| 296 | ---- |
| 297 | @SpringBootApplication |
| 298 | public class Application { |
| 299 | |
| 300 | static { |
| 301 | TomcatURLStreamHandlerFactory.disable(); |
| 302 | } |
| 303 | |
| 304 | // ... |
| 305 | } |
| 306 | ---- |
Matthias Andreas Benkard | 348f205 | 2022-01-15 16:13:01 +0100 | [diff] [blame] | 307 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 308 | == Usage |
| 309 | |
| 310 | Logging unstructured data requires no code changes. All logs are |
| 311 | automatically converted to Google-Cloud-Logging-compatible JSON. |
| 312 | |
| 313 | 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] | 314 | 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] | 315 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 316 | 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] | 317 | as parameters to individual log entries, by supplying |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 318 | 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] | 319 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 320 | 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] | 321 | or by using the Mapped Diagnostic Context. |
| 322 | |
| 323 | |
| 324 | === Using Label and StructuredParameter |
| 325 | |
| 326 | Instances of |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 327 | 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] | 328 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 329 | 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] | 330 | can be passed as log parameters to the `*f` family of logging |
| 331 | functions on JBoss Logging's |
| 332 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger]. |
| 333 | |
| 334 | Simple key–value pairs are represented by |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 335 | 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] | 336 | |
| 337 | **Example:** |
| 338 | |
| 339 | [source,java] |
| 340 | ---- |
| 341 | logger.logf( |
| 342 | "Request rejected: unauthorized.", |
| 343 | Label.of("requestId", "123"), |
| 344 | KeyValueParameter.of("resource", "/users/mulk"), |
| 345 | KeyValueParameter.of("method", "PATCH"), |
| 346 | KeyValueParameter.of("reason", "invalid token")); |
| 347 | ---- |
| 348 | |
| 349 | Result: |
| 350 | |
| 351 | [source,json] |
| 352 | ---- |
| 353 | { |
| 354 | "jsonPayload": { |
| 355 | "message": "Request rejected: unauthorized.", |
| 356 | "resource": "/users/mulk", |
| 357 | "method": "PATCH", |
| 358 | "reason": "invalid token" |
| 359 | }, |
| 360 | "labels": { |
| 361 | "requestId": "123" |
| 362 | } |
| 363 | } |
| 364 | ---- |
| 365 | |
| 366 | |
| 367 | === Using LabelProvider and StructuredParameterProvider |
| 368 | |
| 369 | Any CDI beans that implement |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 370 | 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] | 371 | and |
Matthias Andreas Benkard | 663163d | 2023-09-24 13:46:22 +0200 | [diff] [blame] | 372 | 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] | 373 | are discovered at build time and consulted to provide labels and |
| 374 | parameters for each message that is logged. This can be used to |
| 375 | provide contextual information such as tracing and request IDs stored |
| 376 | in thread-local storage. |
| 377 | |
Matthias Andreas Benkard | 93ecfd1 | 2022-01-15 14:03:41 +0100 | [diff] [blame] | 378 | Alternatively, you can also register providers through the Java |
| 379 | https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ServiceLoader.html[ServiceLoader] |
| 380 | mechanism. |
| 381 | |
Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame] | 382 | **Example:** |
| 383 | |
| 384 | [source,java] |
| 385 | ---- |
| 386 | @Singleton |
| 387 | @Unremovable |
| 388 | public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider { |
| 389 | |
| 390 | @Override |
| 391 | public StructuredParameter getParameter() { |
| 392 | var b = Json.createObjectBuilder(); |
| 393 | b.add("traceId", Span.current().getSpanContext().getTraceId()); |
| 394 | b.add("spanId", Span.current().getSpanContext().getSpanId()); |
| 395 | return () -> b; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public Collection<Label> getLabels() { |
| 400 | return List.of(Label.of("requestId", "123")); |
| 401 | } |
| 402 | } |
| 403 | ---- |
| 404 | |
| 405 | Result: |
| 406 | |
| 407 | [source,json] |
| 408 | ---- |
| 409 | { |
| 410 | "jsonPayload": { |
| 411 | "message": "Request rejected: unauthorized.", |
| 412 | "traceId": "39f9a49a9567a8bd7087b708f8932550", |
| 413 | "spanId": "c7431b14630b633d" |
| 414 | }, |
| 415 | "labels": { |
| 416 | "requestId": "123" |
| 417 | } |
| 418 | } |
| 419 | ---- |
| 420 | |
| 421 | |
| 422 | === Using the Mapped Diagnostic Context |
| 423 | |
| 424 | Any key–value pairs in JBoss Logging's thread-local |
| 425 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC] |
| 426 | are added to the resulting JSON. |
| 427 | |
| 428 | **Example:** |
| 429 | |
| 430 | [source,java] |
| 431 | ---- |
| 432 | MDC.put("resource", "/users/mulk"); |
| 433 | MDC.put("method", "PATCH"); |
| 434 | logger.logf("Request rejected: unauthorized."); |
| 435 | ---- |
| 436 | |
| 437 | Result: |
| 438 | |
| 439 | [source,json] |
| 440 | ---- |
| 441 | { |
| 442 | "jsonPayload": { |
| 443 | "message": "Request rejected: unauthorized.", |
| 444 | "resource": "/users/mulk", |
| 445 | "method": "PATCH" |
| 446 | } |
| 447 | } |
| 448 | ---- |