From ae9faf1bde62989081670c0ede5a12b14ab4f803 Mon Sep 17 00:00:00 2001 From: Jason Aguilon Date: Thu, 14 Apr 2016 12:15:23 -0600 Subject: [PATCH 1/3] save non-string object on LogRecord So that a handler can access the original object instead of just its `toString()`. This enables a couple different use cases: - sending an arbitrary object to the logging methods on `window.console` - handler specific serialization --- lib/logging.dart | 19 +++++++++++++------ test/logging_test.dart | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/logging.dart b/lib/logging.dart index 493d859..8be87b2 100644 --- a/lib/logging.dart +++ b/lib/logging.dart @@ -154,19 +154,24 @@ class Logger { * * If [message] is a [Function], it will be lazy evaluated. Additionally, if * [message] or its evaluated value is not a [String], then 'toString()' will - * be called on it and the result will be logged. + * be called on the object and the result will be logged. The log record will + * contain a field holding the original object. * - * The log record will contain a field for the zone in which this call was - * made. + * The log record will also contain a field for the zone in which this call + * was made. * This can be advantagous if a log listener wants to handle records of * different zones differently (e.g. group log records by http-request if each * http-request handler runs in it's own zone). */ void log(Level logLevel, message, [Object error, StackTrace stackTrace, Zone zone]) { + Object object; if (isLoggable(logLevel)) { if (message is Function) message = message(); - if (message is! String) message = message.toString(); + if (message is! String) { + object = message; + message = message.toString(); + } if (stackTrace == null && logLevel >= recordStackTraceAtLevel) { try { throw "autogenerated stack trace for $logLevel $message"; @@ -178,7 +183,8 @@ class Logger { if (zone == null) zone = Zone.current; var record = - new LogRecord(logLevel, message, fullName, error, stackTrace, zone); + new LogRecord(logLevel, message, fullName, error, stackTrace, zone, + object); if (hierarchicalLoggingEnabled) { var target = this; @@ -334,6 +340,7 @@ class Level implements Comparable { class LogRecord { final Level level; final String message; + final Object object; /** Logger where this record is stored. */ final String loggerName; @@ -356,7 +363,7 @@ class LogRecord { final Zone zone; LogRecord(this.level, this.message, this.loggerName, - [this.error, this.stackTrace, this.zone]) + [this.error, this.stackTrace, this.zone, this.object]) : time = new DateTime.now(), sequenceNumber = LogRecord._nextNumber++; diff --git a/test/logging_test.dart b/test/logging_test.dart index 2f5bf60..863650b 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -529,17 +529,28 @@ void main() { test('message logging - calls toString', () { root.level = Level.INFO; var messages = []; + var objects = []; + var object = new Object(); root.onRecord.listen((record) { messages.add('${record.level}: ${record.message}'); + objects.add(record.object); }); root.info(5); root.info(false); root.info([1, 2, 3]); root.info(() => 10); + root.info(object); - expect(messages, - equals(['INFO: 5', 'INFO: false', 'INFO: [1, 2, 3]', 'INFO: 10',])); + expect(messages, equals([ + 'INFO: 5', + 'INFO: false', + 'INFO: [1, 2, 3]', + 'INFO: 10', + "INFO: Instance of 'Object'" + ])); + + expect(objects, [5, false, [1, 2, 3], 10, object]); }); }); From 5250f30a64e1d9b7da2f6d36c235e5209b2a2604 Mon Sep 17 00:00:00 2001 From: Jason Aguilon Date: Fri, 15 Apr 2016 20:08:22 -0600 Subject: [PATCH 2/3] dartdoc `LogRecord.object` --- lib/logging.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/logging.dart b/lib/logging.dart index 8be87b2..4866329 100644 --- a/lib/logging.dart +++ b/lib/logging.dart @@ -340,6 +340,8 @@ class Level implements Comparable { class LogRecord { final Level level; final String message; + + /** Non-string message passed to Logger. */ final Object object; /** Logger where this record is stored. */ From 4ed06e687f4549ef298c36dc11d4d6385f44dc1d Mon Sep 17 00:00:00 2001 From: Jason Aguilon Date: Fri, 15 Apr 2016 20:09:21 -0600 Subject: [PATCH 3/3] bump version and update changelog --- CHANGELOG.md | 10 +++++++++- pubspec.yaml | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32204e7..953c763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ +## 0.11.3 + +* Added optional `LogRecord.object` field. + +* `Logger.log` sets `LogRecord.object` if the message is not a string or a + function that returns a string. So that a handler can access the original + object instead of just its `toString()`. + ## 0.11.2 -* Added Logger.detached - a convinience factory to obtain a logger that is not +* Added Logger.detached - a convenience factory to obtain a logger that is not attached to this library's logger hierarchy. ## 0.11.1+1 diff --git a/pubspec.yaml b/pubspec.yaml index 2bbbe94..91d2cc7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 0.11.2 +version: 0.11.3 author: Dart Team description: > Provides APIs for debugging and error logging. This library introduces