flutter_log_handler 1.0.2
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"),
),
],
),
),
);
}
}