Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save non-string object on LogRecord #28

Merged
merged 3 commits into from Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion 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
Expand Down
21 changes: 15 additions & 6 deletions lib/logging.dart
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -335,6 +341,9 @@ class LogRecord {
final Level level;
final String message;

/** Non-string message passed to Logger. */
final Object object;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind adding a short comment here to document this new field as well?


/** Logger where this record is stored. */
final String loggerName;

Expand All @@ -356,7 +365,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++;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: logging
version: 0.11.2
version: 0.11.3
author: Dart Team <misc@dartlang.org>
description: >
Provides APIs for debugging and error logging. This library introduces
Expand Down
15 changes: 13 additions & 2 deletions test/logging_test.dart
Expand Up @@ -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]);
});
});

Expand Down