diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2024-06-25 22:31:04 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2024-06-25 22:31:04 +0200 |
commit | 3af29c30779c00e141776f9826834f3262bfbc46 (patch) | |
tree | 8b3977195d9c879eb1d22e3894ec1770ba0aac01 /core/src | |
parent | 7cfbcdb950189dba751ca5a88b1ae2db57dfaa1f (diff) |
feat: Omit the source location if it is all null.
Change-Id: If4be1fe137510d803c4092dff4b17774113d3760
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java | 23 | ||||
-rw-r--r-- | core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java | 16 |
2 files changed, 26 insertions, 13 deletions
diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java index 72b0b50..f3773a8 100644 --- a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java @@ -134,12 +134,7 @@ public class Formatter extends ExtFormatter { var mdc = logRecord.getMdcCopy(); var ndc = logRecord.getNdc(); - var sourceLocation = - new LogEntry.SourceLocation( - logRecord.getSourceFileName(), - String.valueOf(logRecord.getSourceLineNumber()), - String.format( - "%s.%s", logRecord.getSourceClassName(), logRecord.getSourceMethodName())); + var sourceLocation = sourceLocationOf(logRecord); var entry = new LogEntry( @@ -158,6 +153,22 @@ public class Formatter extends ExtFormatter { return entry.json(json).build().toString() + "\n"; } + private static LogEntry.SourceLocation sourceLocationOf(ExtLogRecord logRecord) { + var sourceFileName = logRecord.getSourceFileName(); + var sourceLineNumber = logRecord.getSourceLineNumber(); + var sourceClassName = logRecord.getSourceClassName(); + var sourceMethodName = logRecord.getSourceMethodName(); + return (sourceFileName == null + && sourceLineNumber <= 0 + && sourceClassName == null + && sourceMethodName == null) + ? null + : new LogEntry.SourceLocation( + sourceFileName, + String.valueOf(sourceLineNumber), + String.format("%s.%s", sourceClassName, sourceMethodName)); + } + /** * Formats the log message corresponding to {@code logRecord} including a stack trace of the * {@link ExtLogRecord#getThrown()} exception if any. diff --git a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java index 8517bde..8bcea2b 100644 --- a/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java +++ b/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java @@ -30,7 +30,7 @@ final class LogEntry { private final Timestamp timestamp; @Nullable private final String trace; @Nullable private final String spanId; - private final SourceLocation sourceLocation; + @Nullable private final SourceLocation sourceLocation; private final Map<String, String> labels; private final List<StructuredParameter> parameters; private final Map<String, String> mappedDiagnosticContext; @@ -43,7 +43,7 @@ final class LogEntry { Timestamp timestamp, @Nullable String trace, @Nullable String spanId, - SourceLocation sourceLocation, + @Nullable SourceLocation sourceLocation, Map<String, String> labels, List<StructuredParameter> parameters, Map<String, String> mappedDiagnosticContext, @@ -135,11 +135,13 @@ final class LogEntry { b.add("@type", type); } - return b.add("message", message) - .add("severity", severity) - .add("timestamp", timestamp.json(json)) - .add("logging.googleapis.com/sourceLocation", sourceLocation.json(json)) - .addAll(jsonOfStringMap(json, mappedDiagnosticContext)) + b.add("message", message).add("severity", severity).add("timestamp", timestamp.json(json)); + + if (sourceLocation != null) { + b.add("logging.googleapis.com/sourceLocation", sourceLocation.json(json)); + } + + return b.addAll(jsonOfStringMap(json, mappedDiagnosticContext)) .addAll(jsonOfParameterMap(json, parameters)); } |