Matthias Andreas Benkard | 37e804b | 2021-09-04 22:38:08 +0200 | [diff] [blame^] | 1 | = Quarkus Google Cloud JSON Logging |
| 2 | Matthias Andreas Benkard |
| 3 | // Meta |
| 4 | :experimental: |
| 5 | :data-uri: |
| 6 | :sectnums: |
| 7 | :toc: |
| 8 | :stem: |
| 9 | :toclevels: 2 |
| 10 | :description: Quarkus Google Cloud JSON Logging Manual |
| 11 | :keywords: mulk |
| 12 | // Settings |
| 13 | :icons: font |
| 14 | :source-highlighter: rouge |
| 15 | |
| 16 | |
| 17 | Structured logging to standard output according to the Google Cloud |
| 18 | Logging specification. |
| 19 | |
| 20 | |
| 21 | == Summary |
| 22 | |
| 23 | This package contains a log formatter for JBoss Logging in the form of |
| 24 | a Quarkus plugin that implements the |
| 25 | https://cloud.google.com/logging/docs/structured-logging[Google Cloud |
| 26 | Logging JSON format] on standard output. |
| 27 | |
| 28 | It is possible to log unstructured text, structured data, or a mixture |
| 29 | of both depending on the situation. |
| 30 | |
| 31 | |
| 32 | |
| 33 | To run the application with live reloading enabled: |
| 34 | |
| 35 | [source,console] |
| 36 | ---- |
| 37 | $ ./mvnw quarkus:dev |
| 38 | ---- |
| 39 | |
| 40 | |
| 41 | == Activation |
| 42 | |
| 43 | Add the runtime POM to your dependency list. As long as the JAR is on |
| 44 | the classpath at both build time and runtime, the log formatter |
| 45 | automatically registers itself on startup. |
| 46 | |
| 47 | |
| 48 | === Activation with Maven |
| 49 | |
| 50 | [source,xml] |
| 51 | ---- |
| 52 | <project> |
| 53 | ... |
| 54 | |
| 55 | <dependencies> |
| 56 | ... |
| 57 | |
| 58 | <dependency> |
| 59 | <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId> |
| 60 | <artifactId>quarkus-googlecloud-jsonlogging</artifactId> |
| 61 | <version>3.1.0</version> |
| 62 | </dependency> |
| 63 | |
| 64 | ... |
| 65 | </dependencies> |
| 66 | |
| 67 | ... |
| 68 | </project> |
| 69 | ---- |
| 70 | |
| 71 | |
| 72 | === Activation with Gradle |
| 73 | |
| 74 | [source,groovy] |
| 75 | ---- |
| 76 | dependencies { |
| 77 | ... |
| 78 | |
| 79 | implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:3.1.0") |
| 80 | |
| 81 | ... |
| 82 | } |
| 83 | ---- |
| 84 | |
| 85 | |
| 86 | == Usage |
| 87 | |
| 88 | Logging unstructured data requires no code changes. All logs are |
| 89 | automatically converted to Google-Cloud-Logging-compatible JSON. |
| 90 | |
| 91 | Structured data can be logged in one of 3 different ways: by passing |
| 92 | 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 |
| 93 | and |
| 94 | 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 |
| 95 | as parameters to individual log entries, by supplying |
| 96 | 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 |
| 97 | and |
| 98 | 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, |
| 99 | or by using the Mapped Diagnostic Context. |
| 100 | |
| 101 | |
| 102 | === Using Label and StructuredParameter |
| 103 | |
| 104 | Instances of |
| 105 | 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] |
| 106 | and |
| 107 | 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] |
| 108 | can be passed as log parameters to the `*f` family of logging |
| 109 | functions on JBoss Logging's |
| 110 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html[Logger]. |
| 111 | |
| 112 | Simple key–value pairs are represented by |
| 113 | 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]. |
| 114 | |
| 115 | **Example:** |
| 116 | |
| 117 | [source,java] |
| 118 | ---- |
| 119 | logger.logf( |
| 120 | "Request rejected: unauthorized.", |
| 121 | Label.of("requestId", "123"), |
| 122 | KeyValueParameter.of("resource", "/users/mulk"), |
| 123 | KeyValueParameter.of("method", "PATCH"), |
| 124 | KeyValueParameter.of("reason", "invalid token")); |
| 125 | ---- |
| 126 | |
| 127 | Result: |
| 128 | |
| 129 | [source,json] |
| 130 | ---- |
| 131 | { |
| 132 | "jsonPayload": { |
| 133 | "message": "Request rejected: unauthorized.", |
| 134 | "resource": "/users/mulk", |
| 135 | "method": "PATCH", |
| 136 | "reason": "invalid token" |
| 137 | }, |
| 138 | "labels": { |
| 139 | "requestId": "123" |
| 140 | } |
| 141 | } |
| 142 | ---- |
| 143 | |
| 144 | |
| 145 | === Using LabelProvider and StructuredParameterProvider |
| 146 | |
| 147 | Any CDI beans that implement |
| 148 | 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] |
| 149 | and |
| 150 | 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] |
| 151 | are discovered at build time and consulted to provide labels and |
| 152 | parameters for each message that is logged. This can be used to |
| 153 | provide contextual information such as tracing and request IDs stored |
| 154 | in thread-local storage. |
| 155 | |
| 156 | **Example:** |
| 157 | |
| 158 | [source,java] |
| 159 | ---- |
| 160 | @Singleton |
| 161 | @Unremovable |
| 162 | public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider { |
| 163 | |
| 164 | @Override |
| 165 | public StructuredParameter getParameter() { |
| 166 | var b = Json.createObjectBuilder(); |
| 167 | b.add("traceId", Span.current().getSpanContext().getTraceId()); |
| 168 | b.add("spanId", Span.current().getSpanContext().getSpanId()); |
| 169 | return () -> b; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Collection<Label> getLabels() { |
| 174 | return List.of(Label.of("requestId", "123")); |
| 175 | } |
| 176 | } |
| 177 | ---- |
| 178 | |
| 179 | Result: |
| 180 | |
| 181 | [source,json] |
| 182 | ---- |
| 183 | { |
| 184 | "jsonPayload": { |
| 185 | "message": "Request rejected: unauthorized.", |
| 186 | "traceId": "39f9a49a9567a8bd7087b708f8932550", |
| 187 | "spanId": "c7431b14630b633d" |
| 188 | }, |
| 189 | "labels": { |
| 190 | "requestId": "123" |
| 191 | } |
| 192 | } |
| 193 | ---- |
| 194 | |
| 195 | |
| 196 | === Using the Mapped Diagnostic Context |
| 197 | |
| 198 | Any key–value pairs in JBoss Logging's thread-local |
| 199 | https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/MDC.html[MDC] |
| 200 | are added to the resulting JSON. |
| 201 | |
| 202 | **Example:** |
| 203 | |
| 204 | [source,java] |
| 205 | ---- |
| 206 | MDC.put("resource", "/users/mulk"); |
| 207 | MDC.put("method", "PATCH"); |
| 208 | logger.logf("Request rejected: unauthorized."); |
| 209 | ---- |
| 210 | |
| 211 | Result: |
| 212 | |
| 213 | [source,json] |
| 214 | ---- |
| 215 | { |
| 216 | "jsonPayload": { |
| 217 | "message": "Request rejected: unauthorized.", |
| 218 | "resource": "/users/mulk", |
| 219 | "method": "PATCH" |
| 220 | } |
| 221 | } |
| 222 | ---- |