flutter_log_handler 0.0.2
flutter_log_handler: ^0.0.2 copied to clipboard
Enterprise-grade logging solution for Flutter applications. Provides console logging, file persistence, crash capturing, Dio API interception, retention control, and a built-in professional log viewer UI.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_log_handler/flutter_log_handler.dart';
import 'package:dio/dio.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
LogService.init(
const LogConfig(
maxLogs: 1000,
retentionDays: 7,
enableConsoleLog: true,
enableFileLog: true,
),
);
await LogService.to.getLogs();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: HomeScreen());
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
void _logInfo() {
LogService.to.logEvent(message: "Info log triggered", level: LogLevel.info);
}
void _logWarning() {
LogService.to.logEvent(
message: "Warning log triggered",
level: LogLevel.warning,
);
}
void _logError() {
try {
throw Exception("Example exception");
} catch (e, stack) {
LogService.to.logEvent(
message: e.toString(),
level: LogLevel.error,
stackTrace: stack.toString(),
);
}
}
void _callApi() async {
final dio = Dio();
dio.interceptors.add(ApiInterceptor(LogService.to));
try {
await dio.get("https://jsonplaceholder.typicode.com/posts/1");
} catch (_) {}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("flutter_log_handler Example")),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
ElevatedButton(onPressed: _logInfo, child: const Text("Log Info")),
ElevatedButton(
onPressed: _logWarning,
child: const Text("Log Warning"),
),
ElevatedButton(
onPressed: _logError,
child: const Text("Log Error"),
),
ElevatedButton(
onPressed: _callApi,
child: const Text("Call API (Interceptor)"),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => const MyLogScreen()),
);
},
child: const Text("Open Log Viewer"),
),
],
),
),
);
}
}