flutter_log_handler 1.0.2 copy "flutter_log_handler: ^1.0.2" to clipboard
flutter_log_handler: ^1.0.2 copied to clipboard

Enterprise-grade Flutter logging with console/file logs, crash capture, API interception, and built-in log viewer UI.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import 'package:flutter_log_handler/flutter_log_handler.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// Set environment tags
  LogContext.setEnvironment(
    flavor: "development",
    appVersion: "0.0.6",
    platform: "android",
  );

  /// Initialize Logger (Hive handled internally)
  await LogServiceHive.init(LogConfig(
    retentionDays: 5,
    enableConsoleLog: true,
    sensitiveKeys: [
      "password",
      "token",
      "accessToken",
      "refreshToken",
      "apiKey"
    ],
    uploadLevels: [LogLevelHive.error], // Upload ONLY ERROR logs
    keepUploadHistory: true, // Keep Upload History
    // Upload Configuration
    enableAutoUpload: true,
    uploadUrl: "https://yourserver.com/upload-logs",
    uploadMethod: LogUploadMethod.post,
    uploadHeaders: {
      "Authorization": "Bearer YOUR_ACCESS_TOKEN",
      "X-App-Source": "mobile",
    },
    // Static body parameters
    uploadFields: {
      "appVersion": "1.0.0",
      "platform": "android",
    },
    // Dynamic body parameters (runtime)
    extraFieldsBuilder: () async {
      return {
        "sessionId": "",
        "userId": "",
        "timestamp": DateTime.now().toIso8601String(),
      };
    },
    fileFieldName: "log_file", // if backend expects different key
    uploadInterval: Duration(minutes: 10),
    compressBeforeUpload: true,
  ));

  /// Enable crash capturing
  CrashWrapper.initialize(logService: LogServiceHive.to);

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorObservers: [RouteTracker()],
      debugShowCheckedModeBanner: false,
      home: const HomeScreen(),
    );
  }
}

/// ==============================
/// HOME SCREEN
/// ==============================
class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final dio = Dio();

    /// Attach API interceptor
    dio.interceptors.add(ApiInterceptor(LogServiceHive.to));

    return Scaffold(
      appBar: AppBar(
        title: const Text("Logger Example"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                LogServiceHive.to.logEvent(
                  message: "Manual info log",
                  level: LogLevelHive.info,
                  route: "/home",
                );
              },
              child: const Text("Add Info Log"),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () {
                LogServiceHive.to.logEvent(
                  message: "Manual warning log",
                  level: LogLevelHive.warning,
                  route: "/home",
                );
              },
              child: const Text("Add Warning Log"),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () {
                throw Exception("Test Crash from Example App");
              },
              child: const Text("Trigger Crash"),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () async {
                try {
                  await dio.get("https://jsonplaceholder.typicode.com/posts/1");
                } catch (_) {}
              },
              child: const Text("Make API Call"),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (_) => const LogViewerScreen(
                      title: "App Logs",
                      enableCollapsibleSearch: true,
                      showLevelChips: true,
                      enablePullToRefresh: true,
                      enableManualUpload: true,
                      showLogUploadHistoryButton: true,
                      exportFormat: LogExportFormat.pdf,
                    ),
                  ),
                );
              },
              child: const Text("Open Log Viewer"),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
508
downloads

Publisher

unverified uploader

Weekly Downloads

Enterprise-grade Flutter logging with console/file logs, crash capture, API interception, and built-in log viewer UI.

Repository (GitHub)
View/report issues

Topics

#logging #debugging #monitoring #crash-reporting

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, get, hive, hive_flutter, intl, path_provider, pdf, share_plus

More

Packages that depend on flutter_log_handler