From fec3685a8ae3ab61877fa4972a0f5b5722619755 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Tue, 14 Jan 2025 01:51:47 +0100 Subject: [PATCH] display location information to LogWindow/LogPane output --- README.md | 1 + .../utility/logging/log4j/LogEntryLog4J.java | 42 ++++++------------- .../utility/logging/slf4j/LogEntrySlf4j.java | 35 +++------------- .../com/dua3/utility/logging/LogEntry.java | 14 ++++++- .../com/dua3/utility/concurrent/AutoLock.java | 2 +- 5 files changed, 32 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 4cb3d66e..0cbc4334 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ the parameter. - add logging of exceptions to PlatformHelper.runLater() - add AutoLock class +- add location information to LogWindow/LogPane messages ### 15.1.1 diff --git a/utility-logging-log4j/src/main/java/com/dua3/utility/logging/log4j/LogEntryLog4J.java b/utility-logging-log4j/src/main/java/com/dua3/utility/logging/log4j/LogEntryLog4J.java index 9147413f..95eb1073 100644 --- a/utility-logging-log4j/src/main/java/com/dua3/utility/logging/log4j/LogEntryLog4J.java +++ b/utility-logging-log4j/src/main/java/com/dua3/utility/logging/log4j/LogEntryLog4J.java @@ -22,6 +22,8 @@ public final class LogEntryLog4J implements LogEntry { private @Nullable Supplier messageFormatter; private @Nullable String formattedMessage; private final @Nullable Throwable throwable; + private @Nullable StackTraceElement source; + private @Nullable String location; /** * Creates a new LogEntry object. @@ -35,6 +37,7 @@ public LogEntryLog4J(LogEvent event) { this.level = LogUtilLog4J.translate(event.getLevel()); var marker = event.getMarker(); this.marker = marker == null ? "" : marker.getName(); + this.source = event.getSource(); Message message = event.getMessage(); if (message instanceof ReusableMessage rm) { // for reusable messages, the message must be formatted instantly @@ -49,11 +52,6 @@ public LogEntryLog4J(LogEvent event) { this.throwable = event.getThrown(); } - /** - * Formats the message using MessageFormatter.basicArrayFormat. - * - * @return the formatted message as a string. - */ @Override public String message() { if (messageFormatter != null) { @@ -68,51 +66,35 @@ public String toString() { return format("", ""); } - /** - * Returns the logger name. - * - * @return the name of the logger. - */ @Override public String loggerName() { return loggerName; } - /** - * Returns the time when the log entry was created. - * - * @return the time when the log entry was created. - */ @Override public Instant time() { return time; } - /** - * Returns the log level of the log entry. - * - * @return the log level of the log entry. - */ @Override public LogLevel level() { return level; } - /** - * Returns the marker of the log entry. - * - * @return the marker of the log entry. - */ @Override public String marker() { return marker; } - /** - * Returns the throwable associated with this log entry. - * - * @return the throwable associated with this log entry. - */ + @Override + public @Nullable String location() { + if (source != null) { + location = source.getFileName() + ":" + source.getLineNumber() + " [" + source.getMethodName() + "]"; + source = null; + } + return location; + } + @Override public @Nullable Throwable throwable() { return throwable; diff --git a/utility-logging-slf4j/src/main/java/com/dua3/utility/logging/slf4j/LogEntrySlf4j.java b/utility-logging-slf4j/src/main/java/com/dua3/utility/logging/slf4j/LogEntrySlf4j.java index 94168bb3..40f3aa05 100644 --- a/utility-logging-slf4j/src/main/java/com/dua3/utility/logging/slf4j/LogEntrySlf4j.java +++ b/utility-logging-slf4j/src/main/java/com/dua3/utility/logging/slf4j/LogEntrySlf4j.java @@ -42,11 +42,6 @@ public LogEntrySlf4j(String loggerName, Level level, @Nullable Marker marker, Su this.throwable = throwable; } - /** - * Formats the message using MessageFormatter.basicArrayFormat. - * - * @return the formatted message as a string. - */ @Override public String message() { if (messageFormatter != null) { @@ -61,56 +56,36 @@ public String toString() { return format("", ""); } - /** - * Returns the logger name. - * - * @return the name of the logger. - */ @Override public String loggerName() { return loggerName; } - /** - * Returns the time when the log entry was created. - * - * @return the time when the log entry was created. - */ @Override public Instant time() { return time; } - /** - * Returns the log level of the log entry. - * - * @return the log level of the log entry. - */ @Override public LogLevel level() { return level; } - /** - * Returns the marker of the log entry. - * - * @return the marker of the log entry. - */ @Override public String marker() { return marker; } - /** - * Returns the throwable associated with this log entry. - * - * @return the throwable associated with this log entry. - */ @Override public @Nullable Throwable throwable() { return throwable; } + @Override + public @Nullable String location() { + return null; + } + /** * Translates a given SLF4J Level object to an equivalent LogLevel object. * diff --git a/utility-logging/src/main/java/com/dua3/utility/logging/LogEntry.java b/utility-logging/src/main/java/com/dua3/utility/logging/LogEntry.java index b9710784..b9a8dd7d 100644 --- a/utility-logging/src/main/java/com/dua3/utility/logging/LogEntry.java +++ b/utility-logging/src/main/java/com/dua3/utility/logging/LogEntry.java @@ -56,6 +56,14 @@ public interface LogEntry { @Nullable Throwable throwable(); + /** + * Returns the location information if present. + * + * @return the location information, or null if no location is present + */ + @Nullable + String location(); + /** * Formats the log entry with the given prefix and suffix. * @@ -71,7 +79,11 @@ default String format(String prefix, String suffix) { sb.append(DateTimeFormatter.ISO_INSTANT.format(time())); sb.append(' '); sb.append(loggerName()); - sb.append(' '); + sb.append('\n'); + if (location() != null) { + sb.append(location()); + sb.append('\n'); + } sb.append(message()); if (throwable() != null) { sb.append(System.lineSeparator()); diff --git a/utility/src/main/java/com/dua3/utility/concurrent/AutoLock.java b/utility/src/main/java/com/dua3/utility/concurrent/AutoLock.java index 03a8d806..2808f5d3 100644 --- a/utility/src/main/java/com/dua3/utility/concurrent/AutoLock.java +++ b/utility/src/main/java/com/dua3/utility/concurrent/AutoLock.java @@ -92,7 +92,7 @@ private AutoLock(Lock lock, Supplier name) { this.lock = lock; this.name = name; - LOG.trace("AutoLock({}): lock [{}]", name::get, () -> System.identityHashCode(this)); + LOG.trace("AutoLock({}): lock [{}] - {}", name::get, () -> System.identityHashCode(this), lock::toString); lock.lock(); }