request_tracker_logger 1.1.0 copy "request_tracker_logger: ^1.1.0" to clipboard
request_tracker_logger: ^1.1.0 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 using the extension
    _dio.addRequestTracker(
      redactedFields: ['password'],
      excludedEndpoints: ['/health'],
    );
  }

  Future<void> _makeSuccessfulRequest() async {
    RequestLogger.runWithContext(() async {
      RequestLogger.info('Starting manual request tracking');
      try {
        await _dio.get('https://httpbin.org/json');
      } catch (e) {
        // Errors are handled by the Interceptor
      }
    }, extraContext: {'source': 'demo_page'});
  }

  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');
  }

  void _triggerAsyncException() {
    // This will be caught by PlatformDispatcher.onError
    Future.delayed(const Duration(seconds: 1), () {
      throw StateError('This is an uncaught asynchronous error');
    });
  }

  @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: 16),
            ElevatedButton(
              onPressed: _triggerAsyncException,
              child: const Text('Trigger Async Exception'),
            ),
            const SizedBox(height: 32),
            const Text(
              'Check console logs to see UUIDs and Context tracking.',
              textAlign: TextAlign.center,
              style: TextStyle(fontStyle: FontStyle.italic),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
170
downloads

Publisher

verified publisherdivyanshdev.tech

Weekly Downloads

A comprehensive request tracking and exception logging library for Flutter applications, inspired by Spring Boot's MDC.

Homepage
Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, uuid

More

Packages that depend on request_tracker_logger