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
Fields -
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
-
LOG_EXTENSION
Standard file extension for log files.All log files created by this component or related utilities should use this extension.
- See Also:
-
-
Constructor Details
-
LogBuilder
Creates a builder that keeps at mostmaxSizecharacters.- Parameters:
folder- TODOmaxSize- maximum number of characters to retain; must be positivelogId- optional log identifier for file persistenceprojectDir- optional project directory for log file location- 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:
IllegalArgumentException- 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 in the system temporary directory under
gw-command-logs. Parent directories are created if necessary.- Parameters:
folder-logId- the log identifier- Returns:
- the path to the log file
- Throws:
RuntimeException- if the log directory cannot be created
-
getTail
Returns the retained content.If truncation occurred, a prefix is added to indicate that earlier content was discarded.
- Returns:
- retained text (possibly with a truncation prefix)
-
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. -
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
-