request_tracker_logger 1.0.1
request_tracker_logger: ^1.0.1 copied to clipboard
A comprehensive request tracking and exception logging library for Flutter applications, inspired by Spring Boot's MDC.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:request_tracker_logger/request_tracker_logger.dart';
import 'package:dio/dio.dart';
void main() {
// 1. Initialize Global Exception Handling
GlobalExceptionHandler.initialize();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Logger Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: const LoggerDemoPage(),
);
}
}
class LoggerDemoPage extends StatefulWidget {
const LoggerDemoPage({super.key});
@override
State<LoggerDemoPage> createState() => _LoggerDemoPageState();
}
class _LoggerDemoPageState extends State<LoggerDemoPage> {
final _dio = Dio();
@override
void initState() {
super.initState();
// 2. Add Tracking Interceptor to Dio
_dio.interceptors.add(RequestTrackingInterceptor());
}
Future<void> _makeSuccessfulRequest() async {
try {
// Switched to httpbin.org to avoid Cloudflare blocking
await _dio.get('https://httpbin.org/json');
} catch (e) {
// Errors are handled by the Interceptor
}
}
Future<void> _makeFailedRequest() async {
try {
// This will return a 404
await _dio.get('https://httpbin.org/status/404');
} catch (e) {
// Errors are handled by the Interceptor
}
}
void _triggerManualException() {
// This will be caught by the GlobalExceptionHandler
throw Exception('Manual Exception for testing context tracking');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Logger Plugin Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _makeSuccessfulRequest,
child: const Text('Make Successful Request'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _makeFailedRequest,
child: const Text('Make Failed Request (404)'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _triggerManualException,
child: const Text('Trigger Global Exception'),
),
const SizedBox(height: 32),
const Text(
'Check console logs to see UUIDs and Context tracking.',
textAlign: TextAlign.center,
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
),
);
}
}