From 3af29c30779c00e141776f9826834f3262bfbc46 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 25 Jun 2024 22:31:04 +0200 Subject: feat: Omit the source location if it is all null. Change-Id: If4be1fe137510d803c4092dff4b17774113d3760 --- .../quarkus/googlecloud/jsonlogging/Formatter.java | 23 ++++++++++++++++------ .../quarkus/googlecloud/jsonlogging/LogEntry.java | 16 ++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'core/src/main') 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 labels; private final List parameters; private final Map mappedDiagnosticContext; @@ -43,7 +43,7 @@ final class LogEntry { Timestamp timestamp, @Nullable String trace, @Nullable String spanId, - SourceLocation sourceLocation, + @Nullable SourceLocation sourceLocation, Map labels, List parameters, Map 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)); } -- cgit v1.2.3