Skip to content

Commit

Permalink
updated logging package to accept functions or other non-string objec…
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 committed Jul 25, 2014
1 parent d67fbc6 commit 815de34
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
28 changes: 18 additions & 10 deletions lib/logging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<LogRecord> _getStream() {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: logging
version: 0.9.1+2
version: 0.9.2
author: Dart Team <[email protected]>
description: >
Provides APIs for debugging and error logging. This library introduces
Expand Down
38 changes: 38 additions & 0 deletions test/logging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',]));
});
});
}

0 comments on commit 815de34

Please sign in to comment.