batch_logger 0.0.1
batch_logger: ^0.0.1 copied to clipboard
A zero-dependency, enterprise-grade asynchronous batch logging package.
0.0.1 #
- Initial Release: Enterprise-grade asynchronous batch logging system for Flutter.
Core Features #
- Zero Dependencies: Pure Dart/Flutter implementation with no external package bloat.
- Non-Blocking UI (Isolate-based): Offloads heavy JSON serialization and File I/O to a background Isolate to ensure smooth 60/120 FPS performance.
- Asynchronous Batching: Efficiently flushes logs based on a configurable
batchSizeorflushInterval(time-based). - Crash-Safe Persistence: Logs are written to local storage in a separate thread before syncing, ensuring data recovery even if the app crashes.
- Encryption at Rest: Provides a
LogEncryptorinterface to allow securing log files on the device (GDPR/HIPAA compliant). - Flexible Remote Sync: Includes a
LogSyncerinterface to easily pipe logs to any backend (REST API, Firebase, ElasticSearch, etc.). - Structured Logging: Robust
LogEntrymodel supporting actions, screen tracking, user IDs, custom metadata, and standardLogLevels.
Developer Experience #
- Implementation of a clean Singleton pattern for global access.
- Fully documented interfaces for custom encryption and synchronization logic.
- Comprehensive test suite covering Isolate communication, persistence, and batching logic.
USE #
import 'package:flutter/material.dart'; import 'package:path_provider/path_provider.dart'; // Required in the app layer or any other path provider u may choose import 'package:batch_logger/batch_logger.dart';
void main() async { // 1. Required if you call async code (like path_provider) before runApp WidgetsFlutterBinding.ensureInitialized();
// 2. Get the correct, OS-sanctioned path for the device (iOS/Android/Desktop) final directory = await getApplicationDocumentsDirectory(); final logDirectory = '${directory.path}/app_logs';
// 3. Initialize the logger with all the Enterprise features await BatchLogger.instance.initialize( storagePath: logDirectory, batchSize: 5, // Sync every 5 logs flushInterval: const Duration(seconds: 15), // Or every 15 seconds encryptor: MyLogEncryptor(), // SECURE: Encrypts files on disk syncer: MySimpleSyncer(), // SYNC: Uploads batches to your API );
runApp(const MyApp()); }
// --- Supporting Classes ---
/// A simple implementation of encryption (use AES-256 for production) class MyLogEncryptor implements LogEncryptor { @override String encrypt(String plainText) { // Replace with real encryption logic using 'package:encrypt' return "ENC_$plainText"; }
@override String decrypt(String encryptedText) { return encryptedText.replaceFirst("ENC_", ""); } }
/// A simple implementation of a sync logic class MySimpleSyncer implements LogSyncer { @override Future
class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: ElevatedButton( onPressed: () => BatchLogger.instance.log( action: 'app_start', screen: 'home', level: LogLevel.info, ), child: const Text('Send Log'), ), ), ), ); } }