talker_persistent 2.0.0+1
talker_persistent: ^2.0.0+1 copied to clipboard
An extension for the talker package that adds persistence to logs, allowing you to save logs both to files and Hive database.
Talker Persistent #
An extension for the talker package that adds persistence to logs, allowing you to save logs both to files and Hive database, with flexible configuration options for buffer management and real-time logging.
Features #
- π Save logs to text files
- πΎ Persist logs using Hive database
- π Configurable buffer size (real-time or buffered)
- π¨ Immediate flush for error and critical logs
- π¨ Beautiful log formatting
- π Supports all Talker log types
- π± Works with both Flutter and pure Dart
- ποΈ Flexible logging options (local storage or no storage)
- β‘ Real-time logging support (buffer size = 0)
Homologated Versions #
While the package is designed to be flexible with dependencies, here are the specific versions that have been thoroughly tested and are known to work well together:
Package | Version | Description |
---|---|---|
talker | ^4.8.2 | Core logging functionality |
path | ^1.8.0 | Path manipulation utilities |
hive_ce | ^2.11.3 | Local database storage |
collection | ^1.19.1 | Collection utilities |
These versions are provided for reference only. The package is designed to work with any compatible version of these dependencies to avoid conflicts with your project's requirements.
Usage #
Basic Initialization #
import 'package:talker_persistent/talker_persistent.dart';
import 'package:talker/talker.dart';
void main() async {
// Initialize TalkerPersistent
await TalkerPersistent.instance.initialize(
path: 'path/to/hive/directory',
logNames: {'my_app_logs'},
);
// Create persistent history with default configuration
final history = await TalkerPersistentHistory.create(
logName: 'my_app_logs',
savePath: 'logs',
);
// Use with Talker
final talker = Talker(history: history);
talker.info('Application started');
talker.error('An error occurred');
}
Advanced Configuration #
The package now supports flexible configuration through TalkerPersistentConfig
:
// Configuration with custom settings
final config = TalkerPersistentConfig(
bufferSize: 0, // Real-time logging (no buffer)
flushOnError: true, // Immediate flush for errors
maxCapacity: 1000, // Maximum logs to keep
enableFileLogging: true, // Enable file logging
enableHiveLogging: true, // Enable Hive database logging
);
final history = await TalkerPersistentHistory.create(
logName: 'production_logs',
savePath: 'logs/production',
config: config,
);
Configuration Options #
Option | Type | Default | Description |
---|---|---|---|
bufferSize |
int |
100 |
Buffer size for logs. If 0, logs are written immediately (real-time). If > 0, logs are buffered and written when buffer is full. |
flushOnError |
bool |
true |
Whether to flush immediately for error and critical logs |
maxCapacity |
int |
1000 |
Maximum number of logs to keep in history |
enableFileLogging |
bool |
true |
Whether to enable file logging |
enableHiveLogging |
bool |
true |
Whether to enable Hive database logging |
Real-time Logging #
For critical applications where immediate log writing is required:
final config = TalkerPersistentConfig(
bufferSize: 0, // No buffer - real-time logging
flushOnError: true, // Always flush errors immediately
maxCapacity: 5000, // High capacity for production
);
final history = await TalkerPersistentHistory.create(
logName: 'critical_logs',
savePath: 'logs/critical',
config: config,
);
Buffered Logging #
For performance optimization with buffered writes:
final config = TalkerPersistentConfig(
bufferSize: 100, // Buffer 100 logs before writing
flushOnError: true, // Still flush errors immediately
maxCapacity: 1000,
);
final history = await TalkerPersistentHistory.create(
logName: 'performance_logs',
savePath: 'logs/performance',
config: config,
);
File-only Logging #
If you only want file logging without Hive database:
final config = TalkerPersistentConfig(
bufferSize: 50,
flushOnError: true,
maxCapacity: 500,
enableFileLogging: true,
enableHiveLogging: false, // Disable Hive
);
final history = await TalkerPersistentHistory.create(
logName: 'file_only_logs',
savePath: 'logs/file_only',
config: config,
);
Error Handling #
The package automatically handles error and critical logs with immediate flush when flushOnError
is enabled:
final talker = Talker(history: history);
// These will be flushed immediately if flushOnError is true
talker.error('Database connection failed');
talker.critical('Application crash detected');
// Normal logs follow the buffer configuration
talker.info('User logged in');
talker.debug('Processing request');
Examples #
See the example/
directory for complete working examples:
talker_persistent_example.dart
- Demonstrates different configuration scenariosexample_flutter/
- Flutter-specific example
Migration from Previous Versions #
If you're upgrading from a previous version, the main changes are:
- New Configuration Class: Use
TalkerPersistentConfig
instead of individual parameters - Buffer Control: New
bufferSize
parameter for real-time vs buffered logging - Error Flush: New
flushOnError
parameter for immediate error logging - Selective Logging: New
enableFileLogging
andenableHiveLogging
parameters
Old Way: #
final history = await TalkerPersistentHistory.create(
logName: 'logs',
savePath: 'logs',
maxCapacity: 1000,
);
New Way: #
final config = TalkerPersistentConfig(
bufferSize: 100,
flushOnError: true,
maxCapacity: 1000,
enableFileLogging: true,
enableHiveLogging: true,
);
final history = await TalkerPersistentHistory.create(
logName: 'logs',
savePath: 'logs',
config: config,
);
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 Talker Persistent
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
For the complete license text, please see the [LICENSE](LICENSE) file.