Chromium Code Reviews| 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 logRecord) { | |
| 23 * StringBuffer sb = new StringBuffer() | |
|
kevmoo-old
2013/11/01 17:49:19
Maybe use a format string? The StingBuffer is pret
| |
| 24 * ..write(logRecord.time.toString())..write(":") | |
| 25 * ..write(logRecord.loggerName)..write(":") | |
| 26 * ..write(logRecord.level.name)..write(":") | |
| 27 * ..write(logRecord.sequenceNumber)..write(": ") | |
| 28 * ..write(logRecord.message.toString()); | |
| 29 * print(sb.toString()); | |
| 30 * }); | |
| 31 * | |
| 32 * First, set the root [Level]. All messages at or above the level are | |
| 33 * sent to the [onRecord] stream. | |
| 34 * | |
| 35 * Then, listen on the [onRecord] stream for [LogRecord] events. | |
| 36 * | |
| 37 * ## Logging messages | |
| 38 * | |
| 39 * Create a [Logger] with a unique name to easily identify the source | |
| 40 * of the log messages. | |
| 41 * | |
| 42 * final Logger log = new Logger('MyClassName'); | |
| 43 * | |
| 44 * Here is an example of logging a debug message and an error: | |
| 45 * | |
| 46 * Future future = doSomethingAsync(); | |
| 47 * future.then((result) { | |
| 48 * log.fine('Got the result: $result'); | |
| 49 * processResult(result); | |
| 50 * }) | |
| 51 * .catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace)); | |
| 52 * | |
| 53 * See the [Logger] class for the different logging methods. | |
| 14 */ | 54 */ |
| 15 library logging; | 55 library logging; |
| 16 | 56 |
| 17 import 'dart:async'; | 57 import 'dart:async'; |
| 18 import 'package:unmodifiable_collection/unmodifiable_collection.dart'; | 58 import 'package:unmodifiable_collection/unmodifiable_collection.dart'; |
| 19 | 59 |
| 20 /** | 60 /** |
| 21 * Whether to allow fine-grain logging and configuration of loggers in a | 61 * Whether to allow fine-grain logging and configuration of loggers in a |
| 22 * hierarchy. When false, all logging is merged in the root logger. | 62 * hierarchy. When false, all logging is merged in the root logger. |
| 23 */ | 63 */ |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 /** Associated stackTrace (if any) when recording errors messages. */ | 351 /** Associated stackTrace (if any) when recording errors messages. */ |
| 312 final StackTrace stackTrace; | 352 final StackTrace stackTrace; |
| 313 | 353 |
| 314 LogRecord(this.level, this.message, this.loggerName, [this.error, | 354 LogRecord(this.level, this.message, this.loggerName, [this.error, |
| 315 this.stackTrace]) | 355 this.stackTrace]) |
| 316 : time = new DateTime.now(), | 356 : time = new DateTime.now(), |
| 317 sequenceNumber = LogRecord._nextNumber++; | 357 sequenceNumber = LogRecord._nextNumber++; |
| 318 | 358 |
| 319 String toString() => '[${level.name}] $loggerName: $message'; | 359 String toString() => '[${level.name}] $loggerName: $message'; |
| 320 } | 360 } |
| OLD | NEW |