file_logs 1.0.4
file_logs: ^1.0.4 copied to clipboard
A flexible file logger based on the "logging" package, supporting file rotation, date-based log splitting, and custom handlers.
A very flexible file logger which utilizes "logging" package.
Features #
This package provides a simple and concurrent-safe way to log messages to a file. It builds on the existing "logging" package, adding file-handling features such as:
- Splitting logs into multiple files when they exceed a specified size
- Creating a new file for each day
- Automatically deleting old log files after a specified retention period
In addition to file logging, the package supports:
- Writing logs to the console
- Defining custom record handlers for extensibility
Getting started #
This package is built on top of the logging package.
The available log levels follow the same conventions commonly used in software development.
Usage #
Creating logger:
void main() {
final logger = FileLogger(
logFilePath: 'path/to/log/file.log',
maxFileSize: 1024 * 1024, // 1 MB
logToConsole: true,
logLevel: Level.ALL
);
}
Simple log output:
void main() {
logger.info('This is an info message');
}
Logging an error:
void main() {
try {
throw Exception('This is a test exception');
} catch (e, stackTrace) {
logger.severe('This is a severe message', e, stackTrace);
}
}
Clearing old log files:
void main() async {
await logger.deleteOldLogs(7); // Deletes log files older than 7 days
await logger.clearLogs();
}
Custom format for log messages:
void main() {
logger.logFormatter = (LogRecord record, int sequenceNumber) =>
'${record.time} [${record.level.name}] ($sequenceNumber): ${record.message}\n';
}
Custom format for file names:
void main() {
logger.fileNameGenerator = (String loggerName, DateTime date) =>
'${loggerName.isNotEmpty ? '${loggerName}_' : ''}${DateFormat('yyyy-MM-dd').format(date)}';
}
Custom record handler:
void main() {
logger.recordHandler = (LogRecord record, void Function(LogRecord record) defaultProcessor) {
defaultProcessor(record);
print("Perform additional tasks");
};
}
Additional information #
You can find more information about this package on GitHub: https://github.com/elvedin-hamzagic/file-logs