OLD | NEW |
(Empty) | |
| 1 ## Initializing |
| 2 |
| 3 By default, the logging package does not do anything useful with the |
| 4 log messages. You must configure the logging level and add a handler |
| 5 for the log messages. |
| 6 |
| 7 Here is a simple logging configuration that logs all messages |
| 8 via `print`. |
| 9 |
| 10 ```dart |
| 11 Logger.root.level = Level.ALL; |
| 12 Logger.root.onRecord.listen((LogRecord rec) { |
| 13 print('${rec.level.name}: ${rec.time}: ${rec.message}'); |
| 14 }); |
| 15 ``` |
| 16 |
| 17 First, set the root [Level]. All messages at or above the level are |
| 18 sent to the [onRecord] stream. |
| 19 |
| 20 Then, listen on the [onRecord] stream for [LogRecord] events. The |
| 21 [LogRecord] class has various properties for the message, error, |
| 22 logger name, and more. |
| 23 |
| 24 ## Logging messages |
| 25 |
| 26 Create a [Logger] with a unique name to easily identify the source |
| 27 of the log messages. |
| 28 |
| 29 ```dart |
| 30 final Logger log = new Logger('MyClassName'); |
| 31 ``` |
| 32 |
| 33 Here is an example of logging a debug message and an error: |
| 34 |
| 35 ```dart |
| 36 var future = doSomethingAsync().then((result) { |
| 37 log.fine('Got the result: $result'); |
| 38 processResult(result); |
| 39 }).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace)); |
| 40 ``` |
| 41 |
| 42 When logging more complex messages, you can pass a closure instead |
| 43 that will be evaluated only if the message is actually logged: |
| 44 |
| 45 ```dart |
| 46 log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-")); |
| 47 ``` |
| 48 |
| 49 See the [Logger] class for the different logging methods. |
OLD | NEW |