Masamune Logger Firebase
[GitHub](https://github.com/mathrunet) | [YouTube](https://www.youtube.com/c/mathrunetchannel) | [Packages](https://pub.flutter-io.cn/publishers/mathru.net/packages) | [X](https://x.com/mathru) | [LinkedIn](https://www.linkedin.com/in/mathrunet/) | [mathru.net](https://mathru.net)
Masamune Logger Firebase
Usage
Installation
Add the package to your project.
flutter pub add masamune_logger_firebase
Run flutter pub get after editing pubspec.yaml manually.
Register the Adapter
Set up FirebaseLoggerMasamuneAdapter before running the app. This adapter provides both logging (FirebaseLoggerAdapter) and integration with Firebase services.
// lib/adapter.dart
import 'package:masamune/masamune.dart';
import 'package:masamune_logger_firebase/masamune_logger_firebase.dart';
/// Logger adapters used by the application.
final loggerAdapters = <LoggerAdapter>[
const FirebaseLoggerAdapter(), // Add Firebase logger
];
/// Masamune adapters used by the application.
final masamuneAdapters = <MasamuneAdapter>[
const UniversalMasamuneAdapter(),
const FirebaseLoggerMasamuneAdapter(
options: DefaultFirebaseOptions.currentPlatform, // From firebase_options.dart
),
];
Key Features:
- Initializes Firebase automatically
- Attaches Crashlytics error handling to
FlutterErrorandPlatformDispatcher - Adds navigation observers for automatic screen tracking
- Exposes
FirebaseAnalytics,FirebaseCrashlytics, andFirebasePerformanceinstances
Access the adapter via FirebaseLoggerMasamuneAdapter.primary.
Logging Events
Use the logger API to send events to Firebase Analytics. The package provides pre-built loggable classes for common events.
Sign-In Events:
// Get the logger from your controller or page
final logger = LoggerAdapter.primary.first;
// Log user sign-in
await logger.send(const FirebaseAnalyticsSignInLoggable(
userId: "user_123",
providerId: "google.com",
));
// Log user registration
await logger.send(const FirebaseAnalyticsRegisterLoggable(
userId: "user_456",
));
Purchase Events:
await logger.send(const FirebaseAnalyticsPurchasedLoggable(
transactionId: "order-001",
currency: "USD",
price: 9.99,
products: [
FirebaseAnalyticsPurchaseProduct(
id: "sku-1",
name: "Premium Plan",
price: 9.99,
quantity: 1,
),
],
));
Tutorial Events:
// Tutorial started
await logger.send(const FirebaseAnalyticsTutorialStartLoggable());
// Tutorial completed
await logger.send(const FirebaseAnalyticsTutorialEndLoggable());
Performance Monitoring
Use performance traces to measure operation durations:
final logger = LoggerAdapter.primary.first;
// Start a trace
final trace = logger.trace("load_profile");
await trace.start();
// Execute the operation you want to measure
await loadUserProfile();
// Stop the trace
await trace.stop();
Crashlytics
Crash reports are captured automatically when the adapter is registered. The adapter hooks into FlutterError.onError and PlatformDispatcher.onError.
Test Crashlytics Integration:
// Force a test crash (only for testing!)
final logger = FirebaseLoggerAdapter.primary;
await logger.crash();
Manual Error Reporting:
try {
// Your code
} catch (e, stackTrace) {
await FirebaseCrashlytics.instance.recordError(e, stackTrace);
}
GitHub Sponsors
Sponsors are always welcome. Thank you for your support!
Libraries
- masamune_logger_firebase
- Plug-in package to easily add Firebase Analytics, Performance and Crashlytics on Masamune Framework. The functionality is available by simply adding an Adapter.