Class FileLogger
- All Implemented Interfaces:
ILogListener,Closeable,AutoCloseable,EventListener
ILogListener that appends log messages to a file with automatic
size-based rotation.
File format
Messages are written as UTF-8 text, one entry per line (or one entry per block of lines for multi-line messages such as stack traces). Each entry is prefixed with an ISO-8601 date-time stamp and a fixed-width level tag:
2025-04-07T09:14:02 [INFO ] Application started. 2025-04-07T09:14:02 [CONFIG ] Loaded 195 countries from GeoJSON resource. 2025-04-07T09:14:05 [WARNING] Configuration file not found; using defaults.
Unlike ConsoleLogger, the file logger uses a full date-time stamp
(not just wall-clock time) so that entries from different days can be
distinguished in a long-running application.
Rotation
When the log file exceeds getMaxFileSizeBytes() the current file
is renamed to <name>.1 (overwriting any previous backup) and a
fresh file is opened. This keeps exactly two files on disk at all times —
the current log and one previous — which is sufficient for most desktop
applications. More elaborate rotation strategies can be achieved by
subclassing and overriding rotate().
The default maximum file size is 5 MB (5242880L bytes).
An alternative limit can be supplied to the
FileLogger(Path, long) constructor.
Flushing
The writer is flushed after every message. This is slightly less efficient than batched flushing but ensures that no messages are lost if the JVM terminates abnormally.
Registration
Log.getInstance().addLogListener(new FileLogger(Path.of("app.log")));
Shutdown
FileLogger implements Closeable. In applications that want
an orderly close of the underlying file, call close() during
shutdown:
FileLogger fileLogger = new FileLogger(Path.of("app.log"));
Log.getInstance().addLogListener(fileLogger);
// ... application runs ...
Log.getInstance().removeLogListener(fileLogger);
fileLogger.close();
If close() is not called the file is closed when the JVM exits via
a shutdown hook registered in the constructor, so no messages are silently
dropped.
Thread safety
All write operations are synchronized on the FileLogger
instance, so it is safe to receive messages from multiple threads
simultaneously.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault maximum log-file size before rotation: 5 MB. -
Constructor Summary
ConstructorsConstructorDescriptionFileLogger(Path logPath) FileLogger(Path logPath, long maxFileSizeBytes) Construct aFileLoggerthat writes tologPathand rotates when the file exceedsmaxFileSizeBytes. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Flush and close the underlying file writer.voidAppends aLog.Level.CONFIGmessage to the log file.voidAppends aLog.Level.ERRORmessage to the log file.voidAppends aLog.Level.EXCEPTIONmessage (stack trace) to the log file.Returns the path of the active log file.longReturns the maximum file size in bytes before rotation is triggered.voidAppends anLog.Level.INFOmessage to the log file.protected voidrotate()Rotate the log file.voidAppends aLog.Level.WARNINGmessage to the log file.
-
Field Details
-
DEFAULT_MAX_BYTES
public static final long DEFAULT_MAX_BYTESDefault maximum log-file size before rotation: 5 MB.- See Also:
-
-
Constructor Details
-
FileLogger
- Parameters:
logPath- path of the log file to write; the file and any missing parent directories are created automatically- Throws:
IllegalArgumentException- iflogPathisnull
-
FileLogger
Construct aFileLoggerthat writes tologPathand rotates when the file exceedsmaxFileSizeBytes.- Parameters:
logPath- path of the log file to writemaxFileSizeBytes- maximum file size in bytes before rotation; must be positive- Throws:
IllegalArgumentException- iflogPathisnullormaxFileSizeBytesis not positive
-
-
Method Details
-
info
Appends anLog.Level.INFOmessage to the log file.- Specified by:
infoin interfaceILogListener- Parameters:
message- the message; nevernull
-
config
Appends aLog.Level.CONFIGmessage to the log file.- Specified by:
configin interfaceILogListener- Parameters:
message- the message; nevernull
-
warning
Appends aLog.Level.WARNINGmessage to the log file.- Specified by:
warningin interfaceILogListener- Parameters:
message- the message; nevernull
-
error
Appends aLog.Level.ERRORmessage to the log file.- Specified by:
errorin interfaceILogListener- Parameters:
message- the message; nevernull
-
exception
Appends aLog.Level.EXCEPTIONmessage (stack trace) to the log file.- Specified by:
exceptionin interfaceILogListener- Parameters:
message- the formatted stack trace; nevernull
-
close
public void close()Flush and close the underlying file writer.After this call the logger silently discards any further messages. The logger should also be unregistered from
Logso it stops receiving messages:Log.getInstance().removeLogListener(fileLogger); fileLogger.close();
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
getLogPath
Returns the path of the active log file.- Returns:
- the log file path; never
null
-
getMaxFileSizeBytes
public long getMaxFileSizeBytes()Returns the maximum file size in bytes before rotation is triggered.- Returns:
- the rotation threshold in bytes
-
rotate
protected void rotate()Rotate the log file.The current writer is closed, the active file is moved to the backup path (overwriting any previous backup), and a fresh writer is opened on the original path. Subclasses may override this method to implement more elaborate rotation strategies, such as date-stamped archives or more than one backup generation.
-