From 8f491dd63c28d1df1cb50e993f350f8bda69ec2e Mon Sep 17 00:00:00 2001 From: Tobe Osakwe Date: Mon, 31 Dec 2018 12:22:41 -0500 Subject: [PATCH] Create pretty_logging.dart --- lib/src/pretty_logging.dart | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/src/pretty_logging.dart diff --git a/lib/src/pretty_logging.dart b/lib/src/pretty_logging.dart new file mode 100644 index 0000000..3f8b452 --- /dev/null +++ b/lib/src/pretty_logging.dart @@ -0,0 +1,35 @@ +import 'package:angel_http_exception/angel_http_exception.dart'; +import 'package:logging/logging.dart'; +import 'package:io/ansi.dart'; + +/// Prints the contents of a [LogRecord] with pretty colors. +void prettyLog(LogRecord record) { + var code = chooseLogColor(record.level); + + if (record.error == null) print(code.wrap(record.toString())); + + if (record.error != null) { + var err = record.error; + if (err is AngelHttpException && err.statusCode != 500) return; + print(code.wrap(record.toString() + '\n')); + print(code.wrap(err.toString())); + + if (record.stackTrace != null) { + print(code.wrap(record.stackTrace.toString())); + } + } +} + +/// Chooses a color based on the logger [level]. +AnsiCode chooseLogColor(Level level) { + if (level == Level.SHOUT) + return backgroundRed; + else if (level == Level.SEVERE) + return red; + else if (level == Level.WARNING) + return yellow; + else if (level == Level.INFO) + return cyan; + else if (level == Level.FINER || level == Level.FINEST) return lightGray; + return resetAll; +}