|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:dcc_toolkit/logger/charges/bolt_charge.dart'; |
| 4 | +import 'package:dcc_toolkit/logger/zap_event.dart'; |
| 5 | +import 'package:logging/logging.dart'; |
| 6 | + |
| 7 | +export 'package:dcc_toolkit/logger/charges/bolt_charge.dart'; |
| 8 | +export 'package:dcc_toolkit/logger/extensions/stacktrace.dart'; |
| 9 | +export 'package:dcc_toolkit/logger/extensions/zap_extension.dart'; |
| 10 | +export 'package:dcc_toolkit/logger/ui/bolt_logger_view.dart'; |
| 11 | +export 'package:dcc_toolkit/logger/zap_event.dart'; |
| 12 | + |
| 13 | +/// {@template bolt_logger} |
| 14 | +/// Super charge your logging and zap logs like Zeus with the BoltLogger. |
| 15 | +/// {@endtemplate} |
| 16 | +class BoltLogger { |
| 17 | + /// {@macro bolt_logger} |
| 18 | + factory BoltLogger() => _instance; |
| 19 | + |
| 20 | + BoltLogger._(); |
| 21 | + |
| 22 | + static final _instance = BoltLogger._(); |
| 23 | + |
| 24 | + // Is cancelled in discharge |
| 25 | + //ignore: cancel_subscriptions |
| 26 | + StreamSubscription<LogRecord>? _subscription; |
| 27 | + |
| 28 | + final Map<String, BoltCharge> _outputs = {}; |
| 29 | + |
| 30 | + /// Add [BoltCharge] charges to the logger. This will start the logger if it is not already running. |
| 31 | + /// Without any charges, the logger will not output any logs. |
| 32 | + /// |
| 33 | + /// Available charges: |
| 34 | + /// - [DebugConsoleCharge] |
| 35 | + /// - [MemoryCharge] |
| 36 | + /// - [FileCharge] |
| 37 | + /// |
| 38 | + /// For example to add a [DebugConsoleCharge] charge, use the following code snippet: |
| 39 | + /// ```dart |
| 40 | + /// BoltLogger.charge([DebugConsoleCharge()]); |
| 41 | + /// ``` |
| 42 | + static void charge(List<BoltCharge> charges) { |
| 43 | + for (final charge in charges) { |
| 44 | + _instance._outputs.putIfAbsent(charge.name, () => charge); |
| 45 | + } |
| 46 | + _instance._subscribeIfNeeded(); |
| 47 | + } |
| 48 | + |
| 49 | + /// Look up registered charge by name. |
| 50 | + static BoltCharge? getCharge(String name) { |
| 51 | + return _instance._outputs[name]; |
| 52 | + } |
| 53 | + |
| 54 | + void _subscribeIfNeeded() { |
| 55 | + Logger.root.level = Level.ALL; |
| 56 | + _subscription ??= Logger.root.onRecord.listen((record) { |
| 57 | + for (final output in _outputs.values) { |
| 58 | + output.logOutput(ZapEvent.fromRecord(record)); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + /// Discharge the logger and all charges. |
| 64 | + static void discharge() { |
| 65 | + _instance._subscription?.cancel(); |
| 66 | + _instance._subscription = null; |
| 67 | + for (final output in _instance._outputs.values) { |
| 68 | + output.discharge(); |
| 69 | + } |
| 70 | + _instance._outputs.clear(); |
| 71 | + } |
| 72 | + |
| 73 | + /// {@template zap} |
| 74 | + /// Zap a log message with optional [tag] and [level]. |
| 75 | + /// |
| 76 | + /// For example to zap a message: |
| 77 | + /// ```dart |
| 78 | + /// BoltLogger.zap('Electricity is in the air!'); |
| 79 | + /// ``` |
| 80 | + /// |
| 81 | + /// The [message] can also be an [Exception]/[Error], [StackTrace] or a [List] to zap all 3 types ([Object]?, [Exception]/[Error], [StackTrace]). These will be passed to the logger. |
| 82 | + /// |
| 83 | + /// ```dart |
| 84 | + /// BoltLogger.zap(['Electricity is in the air!', myException, stackTrace]); |
| 85 | + /// ``` |
| 86 | + /// |
| 87 | + /// {@endtemplate} |
| 88 | + static void zap( |
| 89 | + Object? message, { |
| 90 | + String? tag, |
| 91 | + Level level = Level.INFO, |
| 92 | + }) { |
| 93 | + Object? msg; |
| 94 | + Object? error; |
| 95 | + StackTrace? stacktrace; |
| 96 | + |
| 97 | + void zapMap(Object? value) { |
| 98 | + if (value is Exception || value is Error && error == null) { |
| 99 | + error = value; |
| 100 | + } else if (value is StackTrace && stacktrace == null) { |
| 101 | + stacktrace = value; |
| 102 | + } else if (msg == null) { |
| 103 | + msg = value; |
| 104 | + } else { |
| 105 | + final errorMessage = |
| 106 | + 'When zapping a list it can only contain one of each Exception/Error, StackTrace or Object?: $message'; |
| 107 | + assert(false, errorMessage); |
| 108 | + error = AssertionError([errorMessage]); |
| 109 | + msg = ''; |
| 110 | + stacktrace = StackTrace.empty; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (message is List) { |
| 115 | + for (final Object? value in message) { |
| 116 | + zapMap(value); |
| 117 | + } |
| 118 | + } else { |
| 119 | + zapMap(message); |
| 120 | + } |
| 121 | + |
| 122 | + Logger(tag ?? 'BoltLogger').log(level, msg, error, stacktrace); |
| 123 | + } |
| 124 | + |
| 125 | + /// {@template shock} |
| 126 | + /// Shock is a zap intensified! It zaps a log message default [level] of [Level.SEVERE]. |
| 127 | + /// |
| 128 | + /// {@macro zap} |
| 129 | + /// |
| 130 | + /// {@endtemplate} |
| 131 | + static void shock( |
| 132 | + Object? message, { |
| 133 | + String? tag, |
| 134 | + Level level = Level.SEVERE, |
| 135 | + }) { |
| 136 | + zap(message, tag: tag, level: level); |
| 137 | + } |
| 138 | +} |
0 commit comments