|
| 1 | +[](https://github.com/dart-lang/core/actions/workflows/logging.yaml) |
| 2 | +[](https://pub.dev/packages/logging) |
| 3 | +[](https://pub.dev/packages/logging/publisher) |
| 4 | + |
| 5 | +## Initializing |
| 6 | + |
| 7 | +By default, the logging package does not do anything useful with the log |
| 8 | +messages. You must configure the logging level and add a handler for the log |
| 9 | +messages. |
| 10 | + |
| 11 | +Here is a simple logging configuration that logs all messages via `print`. |
| 12 | + |
| 13 | +```dart |
| 14 | +Logger.root.level = Level.ALL; // defaults to Level.INFO |
| 15 | +Logger.root.onRecord.listen((record) { |
| 16 | + print('${record.level.name}: ${record.time}: ${record.message}'); |
| 17 | +}); |
| 18 | +``` |
| 19 | + |
| 20 | +First, set the root `Level`. All messages at or above the current level are sent to the |
| 21 | +`onRecord` stream. Available levels are: |
| 22 | + |
| 23 | ++ `Level.OFF` |
| 24 | ++ `Level.SHOUT` |
| 25 | ++ `Level.SEVERE` |
| 26 | ++ `Level.WARNING` |
| 27 | ++ `Level.INFO` |
| 28 | ++ `Level.CONFIG` |
| 29 | ++ `Level.FINE` |
| 30 | ++ `Level.FINER` |
| 31 | ++ `Level.FINEST` |
| 32 | + |
| 33 | +Then, listen on the `onRecord` stream for `LogRecord` events. The `LogRecord` |
| 34 | +class has various properties for the message, error, logger name, and more. |
| 35 | + |
| 36 | +To listen for changed level notifications use: |
| 37 | + |
| 38 | +```dart |
| 39 | +Logger.root.onLevelChanged.listen((level) { |
| 40 | + print('The new log level is $level'); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +## Logging messages |
| 45 | + |
| 46 | +Create a `Logger` with a unique name to easily identify the source of the log |
| 47 | +messages. |
| 48 | + |
| 49 | +```dart |
| 50 | +final log = Logger('MyClassName'); |
| 51 | +``` |
| 52 | + |
| 53 | +Here is an example of logging a debug message and an error: |
| 54 | + |
| 55 | +```dart |
| 56 | +var future = doSomethingAsync().then((result) { |
| 57 | + log.fine('Got the result: $result'); |
| 58 | + processResult(result); |
| 59 | +}).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace)); |
| 60 | +``` |
| 61 | + |
| 62 | +When logging more complex messages, you can pass a closure instead that will be |
| 63 | +evaluated only if the message is actually logged: |
| 64 | + |
| 65 | +```dart |
| 66 | +log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-")); |
| 67 | +``` |
| 68 | + |
| 69 | +Available logging methods are: |
| 70 | + |
| 71 | ++ `log.shout(logged_content);` |
| 72 | ++ `log.severe(logged_content);` |
| 73 | ++ `log.warning(logged_content);` |
| 74 | ++ `log.info(logged_content);` |
| 75 | ++ `log.config(logged_content);` |
| 76 | ++ `log.fine(logged_content);` |
| 77 | ++ `log.finer(logged_content);` |
| 78 | ++ `log.finest(logged_content);` |
| 79 | + |
| 80 | +## Configuration |
| 81 | + |
| 82 | +Loggers can be individually configured and listened to. When an individual logger has no |
| 83 | +specific configuration, it uses the configuration and any listeners found at `Logger.root`. |
| 84 | + |
| 85 | +To begin, set the global boolean `hierarchicalLoggingEnabled` to `true`. |
| 86 | + |
| 87 | +Then, create unique loggers and configure their `level` attributes and assign any listeners to |
| 88 | +their `onRecord` streams. |
| 89 | + |
| 90 | + |
| 91 | +```dart |
| 92 | + hierarchicalLoggingEnabled = true; |
| 93 | + Logger.root.level = Level.WARNING; |
| 94 | + Logger.root.onRecord.listen((record) { |
| 95 | + print('[ROOT][WARNING+] ${record.message}'); |
| 96 | + }); |
| 97 | +
|
| 98 | + final log1 = Logger('FINE+'); |
| 99 | + log1.level = Level.FINE; |
| 100 | + log1.onRecord.listen((record) { |
| 101 | + print('[LOG1][FINE+] ${record.message}'); |
| 102 | + }); |
| 103 | +
|
| 104 | + // log2 inherits LEVEL value of WARNING from `Logger.root` |
| 105 | + final log2 = Logger('WARNING+'); |
| 106 | + log2.onRecord.listen((record) { |
| 107 | + print('[LOG2][WARNING+] ${record.message}'); |
| 108 | + }); |
| 109 | +
|
| 110 | +
|
| 111 | + // Will NOT print because FINER is too low level for `Logger.root`. |
| 112 | + log1.finer('LOG_01 FINER (X)'); |
| 113 | +
|
| 114 | + // Will print twice ([LOG1] & [ROOT]) |
| 115 | + log1.fine('LOG_01 FINE (√√)'); |
| 116 | +
|
| 117 | + // Will print ONCE because `log1` only uses root listener. |
| 118 | + log1.warning('LOG_01 WARNING (√)'); |
| 119 | +
|
| 120 | + // Will never print because FINE is too low level. |
| 121 | + log2.fine('LOG_02 FINE (X)'); |
| 122 | +
|
| 123 | + // Will print twice ([LOG2] & [ROOT]) because warning is sufficient for all |
| 124 | + // loggers' levels. |
| 125 | + log2.warning('LOG_02 WARNING (√√)'); |
| 126 | +
|
| 127 | + // Will never print because `info` is filtered by `Logger.root.level` of |
| 128 | + // `Level.WARNING`. |
| 129 | + log2.info('INFO (X)'); |
| 130 | +``` |
| 131 | + |
| 132 | +Results in: |
| 133 | + |
| 134 | +``` |
| 135 | +[LOG1][FINE+] LOG_01 FINE (√√) |
| 136 | +[ROOT][WARNING+] LOG_01 FINE (√√) |
| 137 | +[LOG1][FINE+] LOG_01 WARNING (√) |
| 138 | +[ROOT][WARNING+] LOG_01 WARNING (√) |
| 139 | +[LOG2][WARNING+] LOG_02 WARNING (√√) |
| 140 | +[ROOT][WARNING+] LOG_02 WARNING (√√) |
| 141 | +``` |
0 commit comments