Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: lib/logging.dart

Issue 1132533003: Add option to automatically record stack trace in logging (fixes #2) (Closed) Base URL: https://github.com/dart-lang/logging@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
11 11
12 /** 12 /**
13 * Whether to allow fine-grain logging and configuration of loggers in a 13 * Whether to allow fine-grain logging and configuration of loggers in a
14 * hierarchy. When false, all logging is merged in the root logger. 14 * hierarchy. When false, all logging is merged in the root logger.
15 */ 15 */
16 bool hierarchicalLoggingEnabled = false; 16 bool hierarchicalLoggingEnabled = false;
17 17
18 /** 18 /**
19 * Automatically record stack traces for any message of this level or above.
20 * Because this is expensive, this is off by default.
21 */
22 Level recordStackTraceAtLevel = Level.OFF;
23
24 /**
19 * Level for the root-logger. This will be the level of all loggers if 25 * Level for the root-logger. This will be the level of all loggers if
20 * [hierarchicalLoggingEnabled] is false. 26 * [hierarchicalLoggingEnabled] is false.
21 */ 27 */
22 Level _rootLevel = Level.INFO; 28 Level _rootLevel = Level.INFO;
23 29
24 /** 30 /**
25 * Use a [Logger] to log debug messages. [Logger]s are named using a 31 * Use a [Logger] to log debug messages. [Logger]s are named using a
26 * hierarchical dot-separated name convention. 32 * hierarchical dot-separated name convention.
27 */ 33 */
28 class Logger { 34 class Logger {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 * 146 *
141 * The log record will contain a field for the zone in which this call was 147 * The log record will contain a field for the zone in which this call was
142 * made. 148 * made.
143 * This can be advantagous if a log listener wants to handle records of 149 * This can be advantagous if a log listener wants to handle records of
144 * different zones differently (e.g. group log records by http-request if each 150 * different zones differently (e.g. group log records by http-request if each
145 * http-request handler runs in it's own zone). 151 * http-request handler runs in it's own zone).
146 */ 152 */
147 void log(Level logLevel, message, 153 void log(Level logLevel, message,
148 [Object error, StackTrace stackTrace, Zone zone]) { 154 [Object error, StackTrace stackTrace, Zone zone]) {
149 if (isLoggable(logLevel)) { 155 if (isLoggable(logLevel)) {
150 // If message is a Function, evaluate it.
151 if (message is Function) message = message(); 156 if (message is Function) message = message();
152 // If message is still not a String, call toString().
153 if (message is! String) message = message.toString(); 157 if (message is! String) message = message.toString();
154 // Only record the current zone if it was not given. 158 if (stackTrace == null && logLevel >= recordStackTraceAtLevel) {
159 try {
160 throw "log stack trace";
Jennifer Messerly 2015/05/07 22:32:19 include message here? e.g. throw "log ${logLe
Siggi Cherem (dart-lang) 2015/05/08 00:31:03 Good point. Done.
161 } catch (e, t) {
162 stackTrace = t;
163 }
164 }
155 if (zone == null) zone = Zone.current; 165 if (zone == null) zone = Zone.current;
156 166
157 var record = 167 var record =
158 new LogRecord(logLevel, message, fullName, error, stackTrace, zone); 168 new LogRecord(logLevel, message, fullName, error, stackTrace, zone);
159 169
160 if (hierarchicalLoggingEnabled) { 170 if (hierarchicalLoggingEnabled) {
161 var target = this; 171 var target = this;
162 while (target != null) { 172 while (target != null) {
163 target._publish(record); 173 target._publish(record);
164 target = target.parent; 174 target = target.parent;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 /** Zone of the calling code which resulted in this LogRecord. */ 342 /** Zone of the calling code which resulted in this LogRecord. */
333 final Zone zone; 343 final Zone zone;
334 344
335 LogRecord(this.level, this.message, this.loggerName, 345 LogRecord(this.level, this.message, this.loggerName,
336 [this.error, this.stackTrace, this.zone]) 346 [this.error, this.stackTrace, this.zone])
337 : time = new DateTime.now(), 347 : time = new DateTime.now(),
338 sequenceNumber = LogRecord._nextNumber++; 348 sequenceNumber = LogRecord._nextNumber++;
339 349
340 String toString() => '[${level.name}] $loggerName: $message'; 350 String toString() => '[${level.name}] $loggerName: $message';
341 } 351 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698