FileLoggerProvider class final
A provider that writes logs to files on disk.
This provider wraps FileObserver to integrate file logging
with the provider pattern used by FlexLogger.
Use one of the factory constructors for common use cases:
- FileLoggerProvider.singleFile - Single file with optional cleanup
- FileLoggerProvider.rotating - Multiple files with size-based rotation
- FileLoggerProvider.timeBased - Daily log files
Or use the default constructor with a custom FileStrategy.
Example with rotating files (recommended for production):
final provider = FileLoggerProvider.rotating(
filePath: '/logs/app.log',
maxFileSize: 5 * 1024 * 1024, // 5 MB
maxBackupCount: 10,
filter: MinLevelFilter(FlexLogLevel.info),
);
FlexLogger.instance.configure(providers: [provider]);
await FlexLogger.instance.initialize();
Example with daily files:
final provider = FileLoggerProvider.timeBased(
directoryPath: '/logs',
filePrefix: 'myapp',
maxAge: Duration(days: 30),
);
Example with single file (no limits):
final provider = FileLoggerProvider.singleFile(
filePath: '/logs/debug.log',
);
Constructors
- FileLoggerProvider({required FileStrategy strategy, LogFormatter? formatter, LogFilter filter = const AcceptAllFilter(), String? providerId})
- Creates a file logger provider with a custom strategy.
- FileLoggerProvider.rotating({String? filePath, int maxFileSize = 10 * 1024 * 1024, int maxBackupCount = 5, LogFormatter? formatter, LogFilter filter = const AcceptAllFilter(), String? providerId})
-
Creates a provider that uses file rotation.
factory
- FileLoggerProvider.singleFile({String? filePath, int? maxFileSize, Duration? maxAge, int cleanupCheckInterval = 100, LogFormatter? formatter, LogFilter filter = const AcceptAllFilter(), String? providerId, DateFormat? dateFormat})
-
Creates a provider that writes to a single log file.
factory
- FileLoggerProvider.timeBased({String? directoryPath, String filePrefix = 'app', Duration? maxAge, int? maxFiles, LogFormatter? formatter, LogFilter filter = const AcceptAllFilter(), String? providerId})
-
Creates a provider that creates daily log files.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- providerId → String
-
Unique id for this provider (used for deduplication and lookup).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createObserver(
) → FlexObserver? - Throws StateError if called before initialize.
-
dispose(
) → Future< void> - Disposes any resources used by the provider.
-
initialize(
) → Future< void> - Initializes the provider.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited