OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Support for debugging and error logging. | 6 * Support for logging. |
7 * | |
8 * This library introduces abstractions similar to those used in other | |
9 * languages, such as the Closure JS Logger and java.util.logging.Logger. | |
10 * | 7 * |
11 * For information on installing and importing this library, see the | 8 * For information on installing and importing this library, see the |
12 * [logging package on pub.dartlang.org] | 9 * [logging package on pub.dartlang.org] |
13 * (http://pub.dartlang.org/packages/logging). | 10 * (http://pub.dartlang.org/packages/logging). |
| 11 * |
| 12 * ## Initializing |
| 13 * |
| 14 * By default, the logging package does not do anything useful with the |
| 15 * log messages. You must configure the logging level and add a handler |
| 16 * for the log messages. |
| 17 * |
| 18 * Here is a simple logging configuration that logs all messages |
| 19 * via `print`. |
| 20 * |
| 21 * Logger.root.level = Level.ALL; |
| 22 * Logger.root.onRecord.listen((LogRecord rec) { |
| 23 * print('${rec.level.name}: ${rec.time}: ${rec.message}'); |
| 24 * }); |
| 25 * |
| 26 * First, set the root [Level]. All messages at or above the level are |
| 27 * sent to the [onRecord] stream. |
| 28 * |
| 29 * Then, listen on the [onRecord] stream for [LogRecord] events. The |
| 30 * [LogRecord] class has various properties for the message, error, |
| 31 * logger name, and more. |
| 32 * |
| 33 * ## Logging messages |
| 34 * |
| 35 * Create a [Logger] with a unique name to easily identify the source |
| 36 * of the log messages. |
| 37 * |
| 38 * final Logger log = new Logger('MyClassName'); |
| 39 * |
| 40 * Here is an example of logging a debug message and an error: |
| 41 * |
| 42 * Future future = doSomethingAsync(); |
| 43 * future.then((result) { |
| 44 * log.fine('Got the result: $result'); |
| 45 * processResult(result); |
| 46 * }) |
| 47 * .catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace)); |
| 48 * |
| 49 * See the [Logger] class for the different logging methods. |
14 */ | 50 */ |
15 library logging; | 51 library logging; |
16 | 52 |
17 import 'dart:async'; | 53 import 'dart:async'; |
18 import 'package:unmodifiable_collection/unmodifiable_collection.dart'; | 54 import 'package:unmodifiable_collection/unmodifiable_collection.dart'; |
19 | 55 |
20 /** | 56 /** |
21 * Whether to allow fine-grain logging and configuration of loggers in a | 57 * Whether to allow fine-grain logging and configuration of loggers in a |
22 * hierarchy. When false, all logging is merged in the root logger. | 58 * hierarchy. When false, all logging is merged in the root logger. |
23 */ | 59 */ |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 /** Associated stackTrace (if any) when recording errors messages. */ | 347 /** Associated stackTrace (if any) when recording errors messages. */ |
312 final StackTrace stackTrace; | 348 final StackTrace stackTrace; |
313 | 349 |
314 LogRecord(this.level, this.message, this.loggerName, [this.error, | 350 LogRecord(this.level, this.message, this.loggerName, [this.error, |
315 this.stackTrace]) | 351 this.stackTrace]) |
316 : time = new DateTime.now(), | 352 : time = new DateTime.now(), |
317 sequenceNumber = LogRecord._nextNumber++; | 353 sequenceNumber = LogRecord._nextNumber++; |
318 | 354 |
319 String toString() => '[${level.name}] $loggerName: $message'; | 355 String toString() => '[${level.name}] $loggerName: $message'; |
320 } | 356 } |
OLD | NEW |