feat: Omit the source location if it is all null.
Change-Id: If4be1fe137510d803c4092dff4b17774113d3760
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 @@
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 @@
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 @@
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 @@
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 @@
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));
}