deep_link_logger 1.0.0+1
deep_link_logger: ^1.0.0+1 copied to clipboard
A Chucker-like deep link logger for Flutter with local persistence
Deep Link Logger #
A Chucker-like deep link logger for Flutter with local persistence. This package helps you debug and monitor deep links in your Flutter application by logging all incoming deep links with detailed information and storing them locally.
Features #
- 🔗 Automatic Deep Link Logging: Logs all deep links with detailed information
- 💾 Local Persistence: Stores logs using SharedPreferences
- 🎨 Beautiful UI: Browse, search, and filter logs with an intuitive interface
- 📊 Statistics: View analytics about your deep links
- 🔍 Search & Filter: Find specific logs quickly
- 📤 Export/Import: Export logs as JSON for sharing or backup
- 🎯 Error Tracking: Track successful and failed deep link handling
- ⚡ Lightweight: Minimal impact on app performance
Installation #
Add this to your pubspec.yaml:
dependencies:
deep_link_logger:
path: ../deep_link_logger # Use path for local package
Or if published:
dependencies:
deep_link_logger: ^1.0.0
Quick Start #
1. Initialize the Logger #
In your main.dart:
import 'package:deep_link_logger/deep_link_logger.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize the logger
await DeepLinkLogger().init(
enableConsolePrint: true, // Print to console
enablePersistence: true, // Save to storage
maxLogs: 100, // Maximum logs to keep
);
runApp(MyApp());
}
2. Log Deep Links #
Update your DeepLinkHelper class:
import 'package:deep_link_logger/deep_link_logger.dart';
class DeepLinkHelper {
final _logger = DeepLinkLogger();
Future<void> init(Function(Uri) onLinkReceived) async {
try {
final uri = await _appLinks.getInitialLink();
if (uri != null) {
// Log the deep link
await _logger.log(
uri,
metadata: {'source': 'initial_link'},
);
onLinkReceived(uri);
return;
}
_appLinks.uriLinkStream.listen(
(Uri? uri) {
if (uri != null) {
// Log the deep link
_logger.log(
uri,
metadata: {'source': 'stream'},
);
onLinkReceived(uri);
}
},
);
} catch (e) {
logPrint("Error initializing deep link: $e");
}
}
Future<void> handleDeepLink(Uri uri) async {
try {
// Your existing logic
if (uri.pathSegments.isEmpty) {
await _logger.log(uri, result: 'Navigated to home');
backToHome();
return;
}
switch (uri.pathSegments.first) {
case 'vacData':
_showVacancyDetail(uri);
await _logger.log(uri, result: 'Opened vacancy detail');
break;
case 'comData':
_showCompanyDetail(uri);
await _logger.log(uri, result: 'Opened company detail');
break;
// ... other cases
default:
await _logger.log(uri, result: 'Unknown path, navigated to home');
backToHome();
break;
}
} catch (e) {
// Log errors
await _logger.log(
uri,
error: e.toString(),
stackTrace: StackTrace.current,
);
SnackbarHelper.showSnackBarSlide(
type: SnackbarType.error,
text: LangConstant.errorOpenLink.translate(),
);
}
}
}
3. View Logs UI #
Add a route to view logs:
import 'package:deep_link_logger/deep_link_logger.dart';
// In your navigation
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DeepLinkLoggerPage(),
),
);
Or add a debug button:
// In development builds
if (kDebugMode) {
FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DeepLinkLoggerPage(),
),
);
},
child: const Icon(Icons.bug_report),
);
}
API Reference #
DeepLinkLogger #
Methods
// Initialize the logger
await DeepLinkLogger().init({
bool enableConsolePrint = true,
bool enablePersistence = true,
int maxLogs = 100,
});
// Log a deep link
await DeepLinkLogger().log(
uri,
result: 'Success message', // Optional
error: 'Error message', // Optional
metadata: {'key': 'value'}, // Optional
stackTrace: StackTrace.current, // Optional
);
// Get logs from memory
List<DeepLinkLog> logs = DeepLinkLogger().getLogs();
// Get logs from storage
List<DeepLinkLog> logs = await DeepLinkLogger().getLogsFromStorage();
// Clear all logs
await DeepLinkLogger().clear();
// Search logs
List<DeepLinkLog> results = await DeepLinkLogger().searchLogs('query');
// Delete old logs (older than X days)
await DeepLinkLogger().deleteOldLogs(7);
// Export logs as JSON
String json = await DeepLinkLogger().exportLogs();
// Import logs from JSON
await DeepLinkLogger().importLogs(jsonString);
// Get statistics
Map<String, dynamic> stats = DeepLinkLogger().getStatistics();
// Enable/disable console printing
DeepLinkLogger().setConsolePrint(false);
DeepLinkLog Model #
class DeepLinkLog {
final String id;
final Uri uri;
final DateTime timestamp;
final String? result;
final String? error;
final Map<String, dynamic>? metadata;
final String? stackTrace;
// Helper getters
String get formattedTime;
String get formattedDateTime;
bool get hasError;
String get pathSegment;
String get statusIcon;
}
Console Output Example #
When a deep link is received, you'll see formatted output in console:
┌─────────────────────────────────────────────────
│ 🔗 DEEP LINK RECEIVED
├─────────────────────────────────────────────────
│ Time: 14:30:45.123
│ Full URL: myapp://vacData?vacancyid=123&isApplied=true
│ Scheme: myapp
│ Host:
│ Path: /vacData
│ Path Segments: vacData
├─────────────────────────────────────────────────
│ Query Parameters:
│ • vacancyid: 123
│ • isApplied: true
├─────────────────────────────────────────────────
│ Metadata:
│ • source: stream
├─────────────────────────────────────────────────
│ ✅ Result: Opened vacancy detail
└─────────────────────────────────────────────────
UI Features #
Main Logger Page #
- View all logged deep links
- Search and filter logs (All, Success, Errors)
- Pull to refresh
- View statistics
- Export logs
- Clear all logs
- Delete old logs
Log Detail View #
- Full URI information
- Path segments
- Query parameters
- Metadata
- Result/Error status
- Stack trace (for errors)
- Copy functionality
Statistics #
- Total logs
- Success count
- Error count
- First and last log timestamps
- Top path segments
Best Practices #
- Initialize Early: Call
init()in your app's main function - Log Strategically: Log at entry points and after processing
- Include Context: Use metadata to add context
- Handle Errors: Always log errors with stack traces
- Clean Regularly: Delete old logs periodically
- Debug Only: Consider disabling in production or using compile-time flags
Example Integration #
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize logger only in debug mode
if (kDebugMode) {
await DeepLinkLogger().init(
enableConsolePrint: true,
enablePersistence: true,
maxLogs: 200,
);
}
runApp(MyApp());
}
// In your deep link handler
Future<void> handleDeepLink(Uri uri) async {
final logger = DeepLinkLogger();
try {
// Log the incoming link
await logger.log(uri, metadata: {'handler': 'main'});
// Process the link
final result = await processDeepLink(uri);
// Log success
await logger.log(uri, result: 'Processed successfully: $result');
} catch (e, stackTrace) {
// Log error
await logger.log(
uri,
error: e.toString(),
stackTrace: stackTrace,
);
rethrow;
}
}
License #
MIT License
Contributing #
Contributions are welcome! Please open an issue or submit a pull request.