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 */ | 6 */ |
7 library logging; | 7 library logging; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'dart:collection'; | 10 import 'dart:collection'; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 StreamController<LogRecord> _controller; | 54 StreamController<LogRecord> _controller; |
55 | 55 |
56 /** | 56 /** |
57 * Singleton constructor. Calling `new Logger(name)` will return the same | 57 * Singleton constructor. Calling `new Logger(name)` will return the same |
58 * actual instance whenever it is called with the same string name. | 58 * actual instance whenever it is called with the same string name. |
59 */ | 59 */ |
60 factory Logger(String name) { | 60 factory Logger(String name) { |
61 return _loggers.putIfAbsent(name, () => new Logger._named(name)); | 61 return _loggers.putIfAbsent(name, () => new Logger._named(name)); |
62 } | 62 } |
63 | 63 |
| 64 /// Creates a new detached [Logger]. |
| 65 /// |
| 66 /// Returns a new [Logger] instance (unlike `new Logger`, which returns a |
| 67 /// [Logger] singleton), which doesn't have any parent or children, |
| 68 /// and it's not a part of the global hierarchial loggers structure. |
| 69 /// |
| 70 /// It can be useful when you just need a local short-living logger, |
| 71 /// which you'd like to be garbage-collected later. |
| 72 factory Logger.detached(String name) { |
| 73 return new Logger._internal(name, null, new Map<String, Logger>()); |
| 74 } |
| 75 |
64 factory Logger._named(String name) { | 76 factory Logger._named(String name) { |
65 if (name.startsWith('.')) { | 77 if (name.startsWith('.')) { |
66 throw new ArgumentError("name shouldn't start with a '.'"); | 78 throw new ArgumentError("name shouldn't start with a '.'"); |
67 } | 79 } |
68 // Split hierarchical names (separated with '.'). | 80 // Split hierarchical names (separated with '.'). |
69 int dot = name.lastIndexOf('.'); | 81 int dot = name.lastIndexOf('.'); |
70 Logger parent = null; | 82 Logger parent = null; |
71 String thisName; | 83 String thisName; |
72 if (dot == -1) { | 84 if (dot == -1) { |
73 if (name != '') parent = new Logger(''); | 85 if (name != '') parent = new Logger(''); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 /** Zone of the calling code which resulted in this LogRecord. */ | 355 /** Zone of the calling code which resulted in this LogRecord. */ |
344 final Zone zone; | 356 final Zone zone; |
345 | 357 |
346 LogRecord(this.level, this.message, this.loggerName, | 358 LogRecord(this.level, this.message, this.loggerName, |
347 [this.error, this.stackTrace, this.zone]) | 359 [this.error, this.stackTrace, this.zone]) |
348 : time = new DateTime.now(), | 360 : time = new DateTime.now(), |
349 sequenceNumber = LogRecord._nextNumber++; | 361 sequenceNumber = LogRecord._nextNumber++; |
350 | 362 |
351 String toString() => '[${level.name}] $loggerName: $message'; | 363 String toString() => '[${level.name}] $loggerName: $message'; |
352 } | 364 } |
OLD | NEW |