From 815de3404dacbd120a2bf2eb407d62107c5f24b4 Mon Sep 17 00:00:00 2001 From: "jakemac@google.com" Date: Fri, 25 Jul 2014 18:35:00 +0000 Subject: [PATCH] updated logging package to accept functions or other non-string objects when logging R=sigmund@google.com Review URL: https://codereview.chromium.org//420553007 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/logging@38584 260f80e4-7a28-3924-810f-c04153c831b5 --- README.md | 7 +++++++ lib/logging.dart | 28 ++++++++++++++++++---------- pubspec.yaml | 2 +- test/logging_test.dart | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9d6c978..f4781a7 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,11 @@ var future = doSomethingAsync().then((result) { }).catchError((e, stackTrace) => log.severe('Oh noes!', e, stackTrace)); ``` +When logging more complex messages, you can pass a closure instead +that will be evaluated only if the message is actually logged: + +```dart + log.fine(() => [1, 2, 3, 4, 5].map((e) => e * 4).join("-")); +``` + See the [Logger] class for the different logging methods. diff --git a/lib/logging.dart b/lib/logging.dart index d1fb452..5f4bc2b 100644 --- a/lib/logging.dart +++ b/lib/logging.dart @@ -134,10 +134,18 @@ class Logger { * Use this method to create log entries for user-defined levels. To record a * message at a predefined level (e.g. [Level.INFO], [Level.WARNING], etc) you * can use their specialized methods instead (e.g. [info], [warning], etc). + * + * 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. */ - void log(Level logLevel, String message, [Object error, - StackTrace stackTrace]) { + void log(Level logLevel, message, [Object error, StackTrace stackTrace]) { if (isLoggable(logLevel)) { + // If message is a Function, evaluate it. + if (message is Function) message = message(); + // If message is still not a String, call toString(). + if (message is! String) message = message.toString(); + var record = new LogRecord(logLevel, message, fullName, error, stackTrace); @@ -154,35 +162,35 @@ class Logger { } /** Log message at level [Level.FINEST]. */ - void finest(String message, [Object error, StackTrace stackTrace]) => + void finest(message, [Object error, StackTrace stackTrace]) => log(Level.FINEST, message, error, stackTrace); /** Log message at level [Level.FINER]. */ - void finer(String message, [Object error, StackTrace stackTrace]) => + void finer(message, [Object error, StackTrace stackTrace]) => log(Level.FINER, message, error, stackTrace); /** Log message at level [Level.FINE]. */ - void fine(String message, [Object error, StackTrace stackTrace]) => + void fine(message, [Object error, StackTrace stackTrace]) => log(Level.FINE, message, error, stackTrace); /** Log message at level [Level.CONFIG]. */ - void config(String message, [Object error, StackTrace stackTrace]) => + void config(message, [Object error, StackTrace stackTrace]) => log(Level.CONFIG, message, error, stackTrace); /** Log message at level [Level.INFO]. */ - void info(String message, [Object error, StackTrace stackTrace]) => + void info(message, [Object error, StackTrace stackTrace]) => log(Level.INFO, message, error, stackTrace); /** Log message at level [Level.WARNING]. */ - void warning(String message, [Object error, StackTrace stackTrace]) => + void warning(message, [Object error, StackTrace stackTrace]) => log(Level.WARNING, message, error, stackTrace); /** Log message at level [Level.SEVERE]. */ - void severe(String message, [Object error, StackTrace stackTrace]) => + void severe(message, [Object error, StackTrace stackTrace]) => log(Level.SEVERE, message, error, stackTrace); /** Log message at level [Level.SHOUT]. */ - void shout(String message, [Object error, StackTrace stackTrace]) => + void shout(message, [Object error, StackTrace stackTrace]) => log(Level.SHOUT, message, error, stackTrace); Stream _getStream() { diff --git a/pubspec.yaml b/pubspec.yaml index 678ceef..dd7ba7c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 0.9.1+2 +version: 0.9.2 author: Dart Team description: > Provides APIs for debugging and error logging. This library introduces diff --git a/test/logging_test.dart b/test/logging_test.dart index 82ef3e1..223498f 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -418,5 +418,43 @@ main() { 'WARNING: 9', 'SHOUT: 10'])); }); + + test('message logging - lazy functions', () { + root.level = Level.INFO; + var messages = []; + root.onRecord.listen((record) { + messages.add('${record.level}: ${record.message}'); + }); + + var callCount = 0; + var myClosure = () => "${++callCount}"; + + root.info(myClosure); + root.finer(myClosure); // Should not get evaluated. + root.warning(myClosure); + + expect(messages, equals([ + 'INFO: 1', + 'WARNING: 2',])); + }); + + test('message logging - calls toString', () { + root.level = Level.INFO; + var messages = []; + root.onRecord.listen((record) { + messages.add('${record.level}: ${record.message}'); + }); + + root.info(5); + root.info(false); + root.info([1, 2, 3]); + root.info(() => 10); + + expect(messages, equals([ + 'INFO: 5', + 'INFO: false', + 'INFO: [1, 2, 3]', + 'INFO: 10',])); + }); }); }