blob: 110ab73e2aa400c605eee4193eb1f245b74b039f [file] [log] [blame]
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +02001/**
2 * Provides structured logging to standard output according to the Google Cloud Logging
3 * specification.
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +02004 *
5 * <ul>
6 * <li><a href="#sect-summary">Summary</a>
7 * <li><a href="#sect-activation">Activation</a>
8 * <li><a href="#sect-usage">Usage</a>
9 * </ul>
10 *
11 * <h2 id="sect-summary">Summary</h2>
12 *
13 * <p>This package contains a log formatter for JBoss Logging in the form of a Quarkus plugin that
14 * implements the <a href="https://cloud.google.com/logging/docs/structured-logging">Google Cloud
15 * Logging JSON format</a> on standard output.
16 *
17 * <p>It is possible to log unstructured text, structured data, or a mixture of both depending on
18 * the situation.
19 *
20 * <h2 id="sect-activation">Activation</h2>
21 *
22 * <ul>
23 * <li><a href="#sect-activation-maven">Activation with Maven</a>
24 * <li><a href="#sect-activation-gradle">Activation with Gradle</a>
25 * </ul>
26 *
27 * <p>Add the runtime POM to your dependency list. As long as the JAR is on the classpath at both
28 * build time and runtime, the log formatter automatically registers itself on startup.
29 *
30 * <h3 id="sect-activation-maven">Activation with Maven</h3>
31 *
32 * <pre>{@code
33 * <project>
34 * ...
35 *
36 * <dependencies>
37 * ...
38 *
39 * <dependency>
40 * <groupId>eu.mulk.quarkus-googlecloud-jsonlogging</groupId>
41 * <artifactId>quarkus-googlecloud-jsonlogging</artifactId>
42 * <version>3.1.0</version>
43 * </dependency>
44 *
45 * ...
46 * </dependencies>
47 *
48 * ...
49 * </project>
50 * }</pre>
51 *
52 * <h3 id="sect-activation-gradle">Activation with Gradle</h3>
53 *
54 * <pre>{@code
55 * dependencies {
56 * ...
57 *
58 * implementation("eu.mulk.quarkus-googlecloud-jsonlogging:quarkus-googlecloud-jsonlogging:3.1.0")
59 *
60 * ...
61 * }
62 * }</pre>
63 *
64 * <h2 id="sect-usage">Usage</h2>
65 *
66 * <ul>
67 * <li><a href="#sect-usage-parameter">Using Label and StructuredParameter</a>
68 * <li><a href="#sect-usage-provider">Using LabelProvider and StructuredParameterProvider</a>
69 * <li><a href="#sect-usage-mdc">Using the Mapped Diagnostic Context</a>
70 * </ul>
71 *
72 * <p>Logging unstructured data requires no code changes. All logs are automatically converted to
73 * Google-Cloud-Logging-compatible JSON.
74 *
75 * <p>Structured data can be logged in one of 3 different ways: by passing {@link
76 * eu.mulk.quarkus.googlecloud.jsonlogging.Label}s and {@link
77 * eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameter}s as parameters to individual log
78 * entries, by supplying {@link eu.mulk.quarkus.googlecloud.jsonlogging.LabelProvider}s and {@link
79 * eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider}s, or by using the Mapped
80 * Diagnostic Context.
81 *
82 * <h3 id="sect-usage-parameter">Using Label and StructuredParameter</h3>
83 *
84 * <p>Instances of {@link eu.mulk.quarkus.googlecloud.jsonlogging.Label} and {@link
85 * eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameter} can be passed as log parameters to
86 * the {@code *f} family of logging functions on JBoss Logging's {@link org.jboss.logging.Logger}.
87 *
88 * <p>Simple key–value pairs are represented by {@link
89 * eu.mulk.quarkus.googlecloud.jsonlogging.KeyValueParameter}.
90 *
91 * <p><strong>Example:</strong>
92 *
93 * <pre>{@code
94 * logger.logf(
95 * "Request rejected: unauthorized.",
96 * Label.of("requestId", "123"),
97 * KeyValueParameter.of("resource", "/users/mulk"),
98 * KeyValueParameter.of("method", "PATCH"),
99 * KeyValueParameter.of("reason", "invalid token"));
100 * }</pre>
101 *
102 * Result:
103 *
104 * <pre>{@code
105 * {
106 * "jsonPayload": {
107 * "message": "Request rejected: unauthorized.",
108 * "resource": "/users/mulk",
109 * "method": "PATCH",
110 * "reason": "invalid token"
111 * },
112 * "labels": {
113 * "requestId": "123"
114 * }
115 * }
116 * }</pre>
117 *
118 * <h3 id="sect-usage-provider">Using LabelProvider and StructuredParameterProvider</h3>
119 *
120 * <p>Any CDI beans that implement {@link eu.mulk.quarkus.googlecloud.jsonlogging.LabelProvider}s
121 * and {@link eu.mulk.quarkus.googlecloud.jsonlogging.StructuredParameterProvider}s are discovered
122 * at build time and consulted to provide labels and parameters for each message that is logged.
123 * This can be used to provide contextual information such as tracing and request IDs stored in
124 * thread-local storage.
125 *
126 * <p><strong>Example:</strong>
127 *
128 * <pre>{@code
129 * @Singleton
130 * @Unremovable
131 * public final class TraceLogParameterProvider implements StructuredParameterProvider, LabelProvider {
132 *
133 * @Override
134 * public StructuredParameter getParameter() {
135 * var b = Json.createObjectBuilder();
136 * b.add("traceId", Span.current().getSpanContext().getTraceId());
137 * b.add("spanId", Span.current().getSpanContext().getSpanId());
138 * return () -> b;
139 * }
140 *
141 * @Override
142 * public Collection<Label> getLabels() {
143 * return List.of(Label.of("requestId", "123"));
144 * }
145 * }
146 * }</pre>
147 *
148 * Result:
149 *
150 * <pre>{@code
151 * {
152 * "jsonPayload": {
153 * "message": "Request rejected: unauthorized.",
154 * "traceId": "39f9a49a9567a8bd7087b708f8932550",
155 * "spanId": "c7431b14630b633d"
156 * },
157 * "labels": {
158 * "requestId": "123"
159 * }
160 * }
161 * }</pre>
162 *
163 * <h3 id="sect-usage-mdc">Using the Mapped Diagnostic Context</h3>
164 *
165 * <p>Any key–value pairs in JBoss Logging's thread-local {@link org.jboss.logging.MDC} are added to
166 * the resulting JSON.
167 *
168 * <p><strong>Example:</strong>
169 *
170 * <pre>{@code
171 * MDC.put("resource", "/users/mulk");
172 * MDC.put("method", "PATCH");
173 * logger.logf("Request rejected: unauthorized.");
174 * }</pre>
175 *
176 * Result:
177 *
178 * <pre>{@code
179 * {
180 * "jsonPayload": {
181 * "message": "Request rejected: unauthorized.",
182 * "resource": "/users/mulk",
183 * "method": "PATCH"
184 * }
185 * }
186 * }</pre>
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +0200187 */
188package eu.mulk.quarkus.googlecloud.jsonlogging;