-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
331 additions
and
228 deletions.
There are no files selected for viewing
172 changes: 34 additions & 138 deletions
172
utility-logging/src/main/java/com/dua3/utility/logging/LogEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,61 @@ | ||
package com.dua3.utility.logging; | ||
|
||
import com.dua3.cabe.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Represents a log entry with information about the log message, time, level, logger, and optional marker and throwable. | ||
*/ | ||
public final class LogEntry { | ||
private static final char[] NEW_LINE = String.format("%n").toCharArray(); | ||
private final String loggerName; | ||
private final Instant time; | ||
private final LogLevel level; | ||
private final String marker; | ||
private Consumer<StringBuilder> messageFormatter; | ||
private String formattedMessage; | ||
private final Throwable throwable; | ||
public interface LogEntry { | ||
String message(); | ||
|
||
/** | ||
* Creates a new LogEntry object. | ||
* | ||
* @param loggerName The name of the logger. | ||
* @param time The time the log entry was created, represented as an Instant object. | ||
* @param level The log level of the entry. | ||
* @param marker The marker associated with the log entry. Can be null. | ||
* @param messageFormatter A Supplier that provides the formatted log message. | ||
* @param throwable The throwable associated with the log entry. Can be null. | ||
*/ | ||
public LogEntry(String loggerName, Instant time, LogLevel level, @Nullable String marker, Consumer<StringBuilder> messageFormatter, | ||
@Nullable Throwable throwable) { | ||
this.loggerName = loggerName; | ||
this.time = time; | ||
this.level = level; | ||
this.marker = marker; | ||
this.messageFormatter = messageFormatter; | ||
this.throwable = throwable; | ||
} | ||
String loggerName(); | ||
|
||
/** | ||
* Formats the message using MessageFormatter.basicArrayFormat. | ||
* | ||
* @return the formatted message as a string. | ||
*/ | ||
public String formatMessage() { | ||
if (messageFormatter != null) { | ||
StringBuilder sb = new StringBuilder(100); | ||
messageFormatter.accept(sb); | ||
formattedMessage = sb.toString(); | ||
messageFormatter = null; | ||
} | ||
return formattedMessage; | ||
} | ||
Instant time(); | ||
|
||
/** | ||
* Formats the throwable object by printing its stack trace. | ||
* If the throwable object is null, it returns an empty string. | ||
* | ||
* @return the formatted stack trace as a string. | ||
*/ | ||
private void appendThrowable(StringBuilder sb) { | ||
if (throwable == null) { | ||
sb.append("null"); | ||
} else { | ||
try (StringWriter sw = new StringWriter(200); PrintWriter pw = new PrintWriter(sw)) { | ||
throwable.printStackTrace(pw); | ||
sb.append(sw); | ||
} catch (IOException e) { | ||
sb.append(throwable); | ||
} | ||
} | ||
} | ||
LogLevel level(); | ||
|
||
@Override | ||
public String toString() { | ||
return format("", ""); | ||
} | ||
String marker(); | ||
|
||
Throwable throwable(); | ||
|
||
default String format(String prefix, String suffix) { | ||
class Constants { | ||
private static final char[] NEW_LINE = String.format("%n").toCharArray(); | ||
}; | ||
|
||
String format(String prefix, String suffix) { | ||
StringBuilder sb = new StringBuilder(100); | ||
sb.append(prefix); | ||
sb.append('[').append(level).append(']'); | ||
sb.append('[').append(level()).append(']'); | ||
sb.append(' '); | ||
sb.append(DateTimeFormatter.ISO_INSTANT.format(time)); | ||
sb.append(DateTimeFormatter.ISO_INSTANT.format(time())); | ||
sb.append(' '); | ||
messageFormatter.accept(sb); | ||
sb.append(suffix); | ||
sb.append(loggerName()); | ||
sb.append(' '); | ||
sb.append(message()); | ||
if (throwable() != null) { | ||
sb.append(NEW_LINE); | ||
sb.append(Constants.NEW_LINE); | ||
appendThrowable(sb); | ||
} | ||
sb.append(loggerName); | ||
sb.append(suffix); | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Returns the logger name. | ||
* | ||
* @return the name of the logger. | ||
*/ | ||
public String loggerName() { | ||
return loggerName; | ||
} | ||
|
||
/** | ||
* Returns the time when the log entry was created. | ||
* | ||
* @return the time when the log entry was created. | ||
*/ | ||
public Instant time() { | ||
return time; | ||
} | ||
|
||
/** | ||
* Returns the log level of the log entry. | ||
* | ||
* @return the log level of the log entry. | ||
*/ | ||
public LogLevel level() { | ||
return level; | ||
} | ||
|
||
/** | ||
* Returns the marker of the log entry. | ||
* | ||
* @return the marker of the log entry. | ||
*/ | ||
public String marker() { | ||
return marker; | ||
} | ||
|
||
/** | ||
* Returns the throwable associated with this log entry. | ||
* | ||
* @return the throwable associated with this log entry. | ||
* Appends the throwable object to the supplied StringBuilder instance by printing its stack trace. | ||
* @param sb the StringBuilder to append to | ||
*/ | ||
public Throwable throwable() { | ||
return throwable; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object obj) { | ||
if (obj == this) return true; | ||
if (obj == null || obj.getClass() != this.getClass()) return false; | ||
var that = (LogEntry) obj; | ||
return Objects.equals(this.loggerName, that.loggerName) && | ||
Objects.equals(this.time, that.time) && | ||
Objects.equals(this.level, that.level) && | ||
Objects.equals(this.marker, that.marker) && | ||
Objects.equals(this.messageFormatter, that.messageFormatter) && | ||
Objects.equals(this.throwable, that.throwable); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(loggerName, time, level, marker, messageFormatter, throwable); | ||
private void appendThrowable(StringBuilder sb) { | ||
Throwable t = throwable(); | ||
if (t == null) { | ||
sb.append("null"); | ||
} else { | ||
try (StringWriter sw = new StringWriter(200); PrintWriter pw = new PrintWriter(sw)) { | ||
t.printStackTrace(pw); | ||
sb.append(sw); | ||
} catch (IOException e) { | ||
sb.append(t); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.