summaryrefslogtreecommitdiff
path: root/core/src/main/java/eu
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/eu')
-rw-r--r--core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Formatter.java23
-rw-r--r--core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/LogEntry.java16
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));
}