Skip to content

Commit

Permalink
display location information to LogWindow/LogPane output
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Jan 14, 2025
1 parent 86d060e commit fec3685
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 62 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public final class LogEntryLog4J implements LogEntry {
private @Nullable Supplier<String> messageFormatter;
private @Nullable String formattedMessage;
private final @Nullable Throwable throwable;
private @Nullable StackTraceElement source;
private @Nullable String location;

/**
* Creates a new LogEntry object.
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private AutoLock(Lock lock, Supplier<String> 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();
}

Expand Down

0 comments on commit fec3685

Please sign in to comment.