diff --git a/bin/dev.dart b/bin/dev.dart index fbf194a..e1972b1 100644 --- a/bin/dev.dart +++ b/bin/dev.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'package:angel/src/pretty_logging.dart'; import 'package:angel/angel.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_hot/angel_hot.dart'; @@ -9,13 +10,8 @@ main() async { var hot = new HotReloader(() async { var app = new Angel()..lazyParseBodies = true; await app.configure(configureServer); - app.logger = new Logger('angel')..onRecord.listen((rec) { - print(rec); - if (rec.error != null) { - print(rec.error); - print(rec.stackTrace); - } - }); + app.logger = new Logger('angel') + ..onRecord.listen(prettyLog); return app; }, [ new Directory('config'), diff --git a/lib/src/pretty_logging.dart b/lib/src/pretty_logging.dart new file mode 100644 index 0000000..a0ba5f8 --- /dev/null +++ b/lib/src/pretty_logging.dart @@ -0,0 +1,32 @@ +import 'package:console/console.dart'; +import 'package:logging/logging.dart'; + +/// Prints the contents of a [LogRecord] with pretty colors. +void prettyLog(LogRecord record) async { + var pen = new TextPen(); + chooseLogColor(pen.reset(), record.level); + pen(record.toString()); + + if (record.error != null) + pen.record.error.toString(); + if (record.stackTrace != null) + pen.record.stackTrace.toString(); + + pen(); +} + +/// Chooses a color based on the logger [level]. +void chooseLogColor(TextPen pen, Level level) { + if (level == Level.SHOUT) + pen.darkRed(); + else if (level == Level.SEVERE) + pen.red(); + else if (level == Level.WARNING) + pen.yellow(); + else if (level == Level.INFO) + pen.magenta(); + else if (level == Level.FINER) + pen.blue(); + else if (level == Level.FINEST) + pen.darkBlue(); +}