| 1 | import 'package:flutter/foundation.dart'; |
| 2 | import 'package:logger/logger.dart'; |
| 3 | |
| 4 | class Log { |
| 5 | late Logger _logger; |
| 6 | static const lineLength = 80; |
| 7 | static LogOutput? _additionalOutput; |
| 8 | |
| 9 | Log._internal() { |
| 10 | _initLogger(); |
| 11 | } |
| 12 | |
| 13 | void _initLogger() { |
| 14 | if (kDebugMode) { |
| 15 | _logger = Logger( |
| 16 | output: _additionalOutput != null |
| 17 | ? MultiOutput([ConsoleOutput(), _additionalOutput!]) |
| 18 | : null, |
| 19 | printer: PrettyPrinter( |
| 20 | methodCount: 2, |
| 21 | errorMethodCount: 8, |
| 22 | lineLength: lineLength, |
| 23 | colors: true, |
| 24 | printEmojis: true, |
| 25 | dateTimeFormat: DateTimeFormat.onlyTime, |
| 26 | ), |
| 27 | ); |
| 28 | } else { |
| 29 | _logger = Logger( |
| 30 | printer: PrettyPrinter( |
| 31 | methodCount: 0, |
| 32 | errorMethodCount: 0, |
| 33 | lineLength: 0, |
| 34 | colors: false, |
| 35 | printEmojis: false, |
| 36 | dateTimeFormat: DateTimeFormat.onlyTime, |
| 37 | ), |
| 38 | level: Level.off, |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void attachOutput(LogOutput output) { |
| 44 | _additionalOutput = output; |
| 45 | _singleton._initLogger(); |
| 46 | } |
| 47 | |
| 48 | static final Log _singleton = Log._internal(); |
| 49 | |
| 50 | static void fatal({required Object error, required StackTrace stackTrace}) => |
| 51 | _singleton._logger.f('Fatal', error: error, stackTrace: stackTrace); |
| 52 | |
| 53 | static void debug(String message) => _singleton._logger.d(message); |
| 54 | |
| 55 | static void info(String message) => _singleton._logger.i(message); |
| 56 | |
| 57 | static void error(String message) => _singleton._logger.e(message); |
| 58 | |
| 59 | static void warning(String message) => _singleton._logger.w(message); |
| 60 | } |