-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add custom logging provider to support structured logging for SLF4J 1.x + Logback 1.2.x #3693
base: main
Are you sure you want to change the base?
Changes from all commits
60ec4e6
88097e8
bd30ffa
b32e11b
4181927
98e4130
209768b
b0310a4
64acdfb
82ff79d
e0f8981
204fb8f
925f04c
469c348
25a6cb3
d1698a1
b4effd9
fb42331
d0712cc
e8eda21
b6819d8
f35a134
25705f1
0bc9c94
710705b
d56cf81
310e546
8bfd02a
7c8e653
99bf64b
dc73a51
c029d4f
d3a8988
9fd9d56
68c7a3c
9e69796
4b88289
4de3bfc
ed28cd4
7529de0
f295db8
f54db56
e717447
8237028
4aef523
f5efa2a
c26da4b
afb6ae6
12ea137
cfc9af3
9ea01fa
89c469a
3179277
39f94ed
1c6f064
b8db96f
6ed523e
6eb0f8e
2cc02e5
05fcd82
179ecc8
47b4221
eea89ae
af93306
5898bd1
2c7ff17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above | ||
* copyright notice, this list of conditions and the following disclaimer | ||
* in the documentation and/or other materials provided with the | ||
* distribution. | ||
* * Neither the name of Google LLC nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.google.cloud.sdk.logging; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is extending from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The I don't think we can change the interface but we can extract jackson library and update to the lastest, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an interface that does not expose jackson on the surface? if not, I think we may need to re-evaluate the feasibility of it. Since adding a new dependency, especially jackson, is a big commitment. |
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import net.logstash.logback.composite.loggingevent.MdcJsonProvider; | ||
|
||
public class SDKLoggingMdcJsonProvider extends MdcJsonProvider { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException { | ||
Map<String, String> mdcProperties = event.getMDCPropertyMap(); | ||
if (mdcProperties == null || mdcProperties.isEmpty()) { | ||
return; | ||
} | ||
|
||
boolean hasWrittenStart = false; | ||
for (Map.Entry<String, String> entry : mdcProperties.entrySet()) { | ||
String fieldName = entry.getKey(); | ||
String entryValueString = entry.getValue(); | ||
// an entry will be skipped if one of the scenario happens: | ||
// 1. key or value is null | ||
if (fieldName == null || entryValueString == null) { | ||
continue; | ||
} | ||
// 2. includeMdcKeyNames is not empty and the key is not in the list | ||
if (!getIncludeMdcKeyNames().isEmpty() && !getIncludeMdcKeyNames().contains(fieldName)) { | ||
continue; | ||
} | ||
// 3. excludeMdcKeyNames is not empty and the key is in the list | ||
if (!getExcludeMdcKeyNames().isEmpty() && getExcludeMdcKeyNames().contains(fieldName)) { | ||
continue; | ||
} | ||
|
||
fieldName = getMdcKeyFieldNames().getOrDefault(entry.getKey(), fieldName); | ||
if (!hasWrittenStart && getFieldName() != null) { | ||
generator.writeObjectFieldStart(getFieldName()); | ||
hasWrittenStart = true; | ||
} | ||
generator.writeFieldName(fieldName); | ||
|
||
try { | ||
generator.writeTree(convertToTreeNode(entryValueString)); | ||
} catch (JsonProcessingException e) { | ||
// in case of conversion exception, just use String | ||
generator.writeObject(entryValueString); | ||
} | ||
} | ||
if (hasWrittenStart) { | ||
generator.writeEndObject(); | ||
} | ||
} | ||
|
||
private JsonNode convertToTreeNode(String jsonString) throws JsonProcessingException { | ||
return objectMapper.readTree(jsonString); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to install logging module in the main CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Showcase needs to add the logging module as a dependency.
https://github.com/googleapis/sdk-platform-java/pull/3693/files#diff-3c3eb08864bc257af6721c1c900ec0cbb8568e2045b8aadc3330756a97b6ef1eR298