Class LogBuilder
StringBuilder-like helper that retains only the last
maxSize characters.
This utility is typically used when capturing potentially unbounded output (for example, process stdout/stderr) while keeping a deterministic upper bound on memory usage. It also supports optional persistence of appended content to a log file on disk.
The log buffer is truncated from the beginning if the maximum size is exceeded, and a flag is set to indicate truncation. The class also tracks the total number of characters ever appended and the elapsed time since instantiation.
- Since:
- 1.2.0
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringName of the directory beneath the runtime temporary directory where this builder stores its persisted log file.static final StringStandard filename extension assigned to persisted command log files.private final StringOptional identifier used as the base name of the persisted log file.private final intMaximum number of characters retained insbafter each append.private final FileOptional project directory marker indicating that appended content should also be persisted.private final StringBuilderMutable buffer containing the most recently appended, retained log content.private final longEpoch time in milliseconds at which this builder was created.private intTotal number of characters accepted byappend(String)since this instance was created, including content no longer retained insb.private booleanWhether content has been removed from the beginning of the buffer since the last call toclear(). -
Constructor Summary
ConstructorsConstructorDescriptionLogBuilder(String folder, int maxSize, String logId, File projectDir) Creates a builder that keeps at mostmaxSizecharacters. -
Method Summary
Modifier and TypeMethodDescriptionAppends the specified text to the internal log buffer and optionally persists it to disk.voidclear()Clears the retained content and resets the truncation flag.static PathgetCommandLogPath(String folder, String logId) Returns the path to the log file for the given log identifier.Returns a report of the log state, including log ID, retained tail, total length, truncation status, and elapsed process time in milliseconds.getTail()Returns the retained content.intReturns the total number of characters ever appended to this builder.intlength()Returns the number of characters currently retained.
-
Field Details
-
folder
Name of the directory beneath the runtime temporary directory where this builder stores its persisted log file.This directory is combined with
ProjectLayout.getTempDir()when a log path is requested. -
LOG_EXTENSION
Standard filename extension assigned to persisted command log files.All log files created by this class use this extension after their log identifier.
- See Also:
-
maxSize
private final int maxSizeMaximum number of characters retained insbafter each append. -
sb
Mutable buffer containing the most recently appended, retained log content. -
truncated
private boolean truncatedWhether content has been removed from the beginning of the buffer since the last call toclear(). -
logId
Optional identifier used as the base name of the persisted log file. -
projectDir
Optional project directory marker indicating that appended content should also be persisted. The directory itself is not used to construct the log path; persistence is enabled only when this value andlogIdare non-null. -
totalLength
private int totalLengthTotal number of characters accepted byappend(String)since this instance was created, including content no longer retained insb. -
startTime
private final long startTimeEpoch time in milliseconds at which this builder was created.
-
-
Constructor Details
-
LogBuilder
Creates a builder that keeps at mostmaxSizecharacters.- Parameters:
folder- directory beneath the runtime temporary directory for the persisted log filemaxSize- maximum number of characters to retain; must be positivelogId- optional log identifier for file persistenceprojectDir- optional non-null marker enabling file persistence whenlogIdis also non-null- Throws:
IllegalArgumentException- ifmaxSizeis not positive
-
-
Method Details
-
append
Appends the specified text to the internal log buffer and optionally persists it to disk.This method updates the internal buffer by adding the provided text. If the buffer exceeds the configured maximum size (
maxSize), the oldest content is truncated to maintain the limit. Thetruncatedflag is set if truncation occurs.If both
projectDirandlogIdare set, the appended text is also written to a log file on disk. The log file is created if it does not exist, or appended to if it does. Parent directories are created as needed.- Parameters:
text- the text to append to the log buffer; ifnull, no action is taken- Returns:
- this
LogBuilderinstance for method chaining - Throws:
UncheckedIOException- if an I/O error occurs while writing to the log file
-
getCommandLogPath
Returns the path to the log file for the given log identifier.The log file is located beneath the runtime temporary directory in the supplied
folder. Parent directories are created if necessary.- Parameters:
folder- the directory beneath the runtime temporary directorylogId- the log identifier used as the file name- Returns:
- the path to the log file
- Throws:
UncheckedIOException- if the log directory cannot be created
-
getTail
Returns the retained content.Earlier content is omitted when the configured maximum size was exceeded; callers can inspect
getReport()for the truncation status.- Returns:
- retained text; when truncation occurred, the returned value omits the discarded leading content
-
length
public int length()Returns the number of characters currently retained.- Returns:
- retained length
-
clear
public void clear()Clears the retained content and resets the truncation flag.This operation does not reset the total appended length, the start time, or any persisted log file.
-
getTotalLength
public int getTotalLength()Returns the total number of characters ever appended to this builder.- Returns:
- the total length of all appended content
-
getReport
Returns a report of the log state, including log ID, retained tail, total length, truncation status, and elapsed process time in milliseconds.- Returns:
- a map containing log state information
-