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 * Provides APIs for debugging and error logging. This library introduces | 6 * Provides APIs for debugging and error logging. This library introduces |
7 * abstractions similar to those used in other languages, such as the Closure JS | 7 * abstractions similar to those used in other languages, such as the Closure JS |
8 * Logger and java.util.logging.Logger. | 8 * Logger and java.util.logging.Logger. |
9 */ | 9 */ |
10 #library('logging'); | 10 #library('logging'); |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 /** | 25 /** |
26 * Use a [Logger] to log debug messages. [Logger]s are named using a | 26 * Use a [Logger] to log debug messages. [Logger]s are named using a |
27 * hierarchical dot-separated name convention. | 27 * hierarchical dot-separated name convention. |
28 */ | 28 */ |
29 class Logger { | 29 class Logger { |
30 /** Simple name of this logger. */ | 30 /** Simple name of this logger. */ |
31 final String name; | 31 final String name; |
32 | 32 |
33 /** The full name of this logger, which includes the parent's full name. */ | 33 /** The full name of this logger, which includes the parent's full name. */ |
34 String get fullName() => | 34 String get fullName => |
35 (parent == null || parent.name == '') ? name : '${parent.fullName}.$name'; | 35 (parent == null || parent.name == '') ? name : '${parent.fullName}.$name'; |
36 | 36 |
37 /** Parent of this logger in the hierarchy of loggers. */ | 37 /** Parent of this logger in the hierarchy of loggers. */ |
38 final Logger parent; | 38 final Logger parent; |
39 | 39 |
40 /** Logging [Level] used for entries generated on this logger. */ | 40 /** Logging [Level] used for entries generated on this logger. */ |
41 Level _level; | 41 Level _level; |
42 | 42 |
43 /** Children in the hierarchy of loggers, indexed by their simple names. */ | 43 /** Children in the hierarchy of loggers, indexed by their simple names. */ |
44 Map<String, Logger> children; | 44 Map<String, Logger> children; |
(...skipping 30 matching lines...) Expand all Loading... |
75 | 75 |
76 Logger._internal(this.name, this.parent) | 76 Logger._internal(this.name, this.parent) |
77 : children = new Map<String, Logger>() { | 77 : children = new Map<String, Logger>() { |
78 if (parent != null) parent.children[name] = this; | 78 if (parent != null) parent.children[name] = this; |
79 } | 79 } |
80 | 80 |
81 /** | 81 /** |
82 * Effective level considering the levels established in this logger's parents | 82 * Effective level considering the levels established in this logger's parents |
83 * (when [hierarchicalLoggingEnabled] is true). | 83 * (when [hierarchicalLoggingEnabled] is true). |
84 */ | 84 */ |
85 Level get level() { | 85 Level get level { |
86 if (hierarchicalLoggingEnabled) { | 86 if (hierarchicalLoggingEnabled) { |
87 if (_level != null) return _level; | 87 if (_level != null) return _level; |
88 if (parent != null) return parent.level; | 88 if (parent != null) return parent.level; |
89 } | 89 } |
90 return _rootLevel; | 90 return _rootLevel; |
91 } | 91 } |
92 | 92 |
93 /** Override the level for this particular [Logger] and its children. */ | 93 /** Override the level for this particular [Logger] and its children. */ |
94 Level set level(value) { | 94 Level set level(value) { |
95 if (hierarchicalLoggingEnabled && parent != null) { | 95 if (hierarchicalLoggingEnabled && parent != null) { |
96 _level = value; | 96 _level = value; |
97 } else { | 97 } else { |
98 if (parent != null) { | 98 if (parent != null) { |
99 throw new UnsupportedOperationException( | 99 throw new UnsupportedOperationException( |
100 'Please set "hierarchicalLoggingEnabled" to true if you want to ' | 100 'Please set "hierarchicalLoggingEnabled" to true if you want to ' |
101 'change the level on a non-root logger.'); | 101 'change the level on a non-root logger.'); |
102 } | 102 } |
103 _rootLevel = value; | 103 _rootLevel = value; |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 /** | 107 /** |
108 * Returns an event manager for this [Logger]. You can listen for log messages | 108 * Returns an event manager for this [Logger]. You can listen for log messages |
109 * by adding a [LoggerHandler] to an event from the event manager, for | 109 * by adding a [LoggerHandler] to an event from the event manager, for |
110 * instance: | 110 * instance: |
111 * logger.on.record.add((record) { ... }); | 111 * logger.on.record.add((record) { ... }); |
112 */ | 112 */ |
113 LoggerEvents get on() => new LoggerEvents(this); | 113 LoggerEvents get on => new LoggerEvents(this); |
114 | 114 |
115 /** Adds a handler to listen whenever a log record is added to this logger. */ | 115 /** Adds a handler to listen whenever a log record is added to this logger. */ |
116 void _addHandler(LoggerHandler handler) { | 116 void _addHandler(LoggerHandler handler) { |
117 if (hierarchicalLoggingEnabled || parent == null) { | 117 if (hierarchicalLoggingEnabled || parent == null) { |
118 if (_handlers == null) { | 118 if (_handlers == null) { |
119 _handlers = new List<LoggerHandler>(); | 119 _handlers = new List<LoggerHandler>(); |
120 } | 120 } |
121 _handlers.add(handler); | 121 _handlers.add(handler); |
122 } else { | 122 } else { |
123 root._addHandler(handler); | 123 root._addHandler(handler); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 /** Log message at level [Level.SHOUT]. */ | 194 /** Log message at level [Level.SHOUT]. */ |
195 void shout(String message) => log(Level.SHOUT, message); | 195 void shout(String message) => log(Level.SHOUT, message); |
196 | 196 |
197 void _publish(LogRecord record) { | 197 void _publish(LogRecord record) { |
198 if (_handlers != null) { | 198 if (_handlers != null) { |
199 _handlers.forEach((h) => h(record)); | 199 _handlers.forEach((h) => h(record)); |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 /** Top-level root [Logger]. */ | 203 /** Top-level root [Logger]. */ |
204 static get root() => new Logger(''); | 204 static get root => new Logger(''); |
205 | 205 |
206 /** All [Logger]s in the system. */ | 206 /** All [Logger]s in the system. */ |
207 static Map<String, Logger> _loggers; | 207 static Map<String, Logger> _loggers; |
208 } | 208 } |
209 | 209 |
210 | 210 |
211 /** Handler callback to process log entries as they are added to a [Logger]. */ | 211 /** Handler callback to process log entries as they are added to a [Logger]. */ |
212 typedef void LoggerHandler(LogRecord); | 212 typedef void LoggerHandler(LogRecord); |
213 | 213 |
214 | 214 |
215 /** Event manager for a [Logger] (holds events that a [Logger] can fire). */ | 215 /** Event manager for a [Logger] (holds events that a [Logger] can fire). */ |
216 class LoggerEvents { | 216 class LoggerEvents { |
217 final Logger _logger; | 217 final Logger _logger; |
218 | 218 |
219 LoggerEvents(this._logger); | 219 LoggerEvents(this._logger); |
220 | 220 |
221 /** Event fired when a log record is added to a [Logger]. */ | 221 /** Event fired when a log record is added to a [Logger]. */ |
222 LoggerHandlerList get record() => new LoggerHandlerList(_logger); | 222 LoggerHandlerList get record => new LoggerHandlerList(_logger); |
223 } | 223 } |
224 | 224 |
225 | 225 |
226 /** List of handlers that will be called on a logger event. */ | 226 /** List of handlers that will be called on a logger event. */ |
227 class LoggerHandlerList { | 227 class LoggerHandlerList { |
228 Logger _logger; | 228 Logger _logger; |
229 | 229 |
230 LoggerHandlerList(this._logger); | 230 LoggerHandlerList(this._logger); |
231 | 231 |
232 void add(LoggerHandler handler) => _logger._addHandler(handler); | 232 void add(LoggerHandler handler) => _logger._addHandler(handler); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 | 325 |
326 /** Associated exception message (if any) when recording errors messages. */ | 326 /** Associated exception message (if any) when recording errors messages. */ |
327 String exceptionText; | 327 String exceptionText; |
328 | 328 |
329 LogRecord( | 329 LogRecord( |
330 this.level, this.message, this.loggerName, | 330 this.level, this.message, this.loggerName, |
331 [time, this.exception, this.exceptionText]) : | 331 [time, this.exception, this.exceptionText]) : |
332 this.time = (time == null) ? new Date.now() : time, | 332 this.time = (time == null) ? new Date.now() : time, |
333 this.sequenceNumber = LogRecord._nextNumber++; | 333 this.sequenceNumber = LogRecord._nextNumber++; |
334 } | 334 } |
OLD | NEW |