Last active
December 7, 2023 14:09
-
-
Save lukepighetti/57b5979f293e7a368d918a494c4c3a42 to your computer and use it in GitHub Desktop.
Another logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyService { | |
final _log = Logger('MyService'); | |
void init() { | |
try { | |
// operation | |
_log("initialized"); | |
} catch(e, stackTrace){ | |
_log.e("init() error", e, stackTrace); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_crashlytics/firebase_crashlytics.dart'; | |
import 'package:flutter/foundation.dart'; | |
void setupLogger() { | |
Logger.registerChannel(DebugPrintLoggerChannel()); | |
Logger.registerChannel(CrashlyticsLoggerChannel()); | |
} | |
/// A tagged logging utility with debug / warn / error severity and custom | |
/// channels | |
class Logger { | |
Logger(this.tag); | |
final String tag; | |
static final _channels = <LoggerChannel>{}; | |
/// Adds a channel to the logging utility | |
static void registerChannel(LoggerChannel channel) => _channels.add(channel); | |
void d(String message) { | |
for (final channel in _channels) { | |
channel.d(tag, message); | |
} | |
} | |
void call(String message) => d(message); | |
void e(String message, dynamic error, [StackTrace? stackTrace]) { | |
for (final channel in _channels) { | |
channel.e(tag, message, error, stackTrace); | |
} | |
} | |
void w(String message) { | |
for (final channel in _channels) { | |
channel.w(tag, message); | |
} | |
} | |
} | |
abstract class LoggerChannel { | |
/// Handle a debug log event | |
void d(String tag, String message) {} | |
/// Handle a warning log event | |
void w(String tag, String message) {} | |
/// Handle an error log event | |
void e(String tag, String message, dynamic error, StackTrace? stackTrace) {} | |
} | |
/// Outputs all log to console via [debugPrint] | |
class DebugPrintLoggerChannel implements LoggerChannel { | |
@override | |
void d(String tag, String message) { | |
debugPrint('D [$tag]: \t $message'); | |
} | |
@override | |
void e(String tag, String message, dynamic error, StackTrace? stackTrace) { | |
debugPrint('E [$tag]: \t $message, $error'); | |
} | |
@override | |
void w(String tag, String message) { | |
debugPrint('W [$tag]: \t $message'); | |
} | |
} | |
class CrashlyticsLoggerChannel implements LoggerChannel { | |
FirebaseCrashlytics get _c => FirebaseCrashlytics.instance; | |
@override | |
void d(String tag, String message) { | |
_c.log('D [$tag]: \t $message'); | |
} | |
@override | |
void e(String tag, String message, error, StackTrace? stackTrace) { | |
_c.log('E [$tag]: \t $message, $error'); | |
} | |
@override | |
void w(String tag, String message) { | |
_c.log('W [$tag]: \t $message'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment