FileSink class
A LogSink that writes log records to files with automatic rotation.
Supports rotation by file size, configurable maximum file count, and both human-readable text and NDJSON output formats.
import 'dart:io';
import 'package:log_pilot/log_pilot.dart';
final logDir = Directory('/path/to/logs');
LogPilot.init(
config: LogPilotConfig(
sinks: [
FileSink(
directory: logDir,
maxFileSize: 2 * 1024 * 1024, // 2 MB
maxFileCount: 5,
format: FileLogFormat.text,
),
],
),
child: const MyApp(),
);
Log files are named LogPilot.log (current) and LogPilot.1.log through
LogPilot.{maxFileCount - 1}.log (rotated archives). When the current
file exceeds maxFileSize, files are shifted and the oldest is
deleted.
Writes are buffered and flushed periodically (every 500ms by default) or when the buffer reaches 100 records to avoid blocking the UI thread on every log call.
- Implemented types
Constructors
- FileSink({required Directory directory, int maxFileSize = 2 * 1024 * 1024, int maxFileCount = 5, FileLogFormat format = FileLogFormat.text, String baseFileName = 'LogPilot', @visibleForTesting Duration flushInterval = const Duration(milliseconds: 500)})
-
Creates a file sink that writes to
directory.
Properties
- baseFileName → String
-
Base name for log files (without extension). Files are named
{baseFileName}.log,{baseFileName}.1.log, etc.final - directory → Directory
-
The directory where log files are stored.
final
- format → FileLogFormat
-
Output format for log records.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
logFiles
→ List<
File> -
Returns the paths of all existing log files (active + rotated),
ordered from newest to oldest.
no setter
- maxFileCount → int
-
Total number of log files to keep (active + rotated archives).
final
- maxFileSize → int
-
Maximum size of the active log file in bytes before rotation.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispose(
) → Future< void> -
Flush and release resources (cancels the periodic flush timer).
override
-
flush(
) → Future< void> - Flush all buffered records to disk immediately.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onLog(
LogPilotRecord record) → void -
Called for every log record that passes the level and tag filters.
override
-
readAll(
) → Future< String> - Read and return the contents of all log files concatenated, ordered from oldest to newest.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited