platform/test/pretty_log.dart

37 lines
1,000 B
Dart
Raw Normal View History

2019-05-02 22:37:32 +00:00
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;
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) {
2019-06-06 14:37:27 +00:00
if (level == Level.SHOUT) {
2019-05-02 22:37:32 +00:00
return backgroundRed;
2019-06-06 14:37:27 +00:00
} else if (level == Level.SEVERE) {
2019-05-02 22:37:32 +00:00
return red;
2019-06-06 14:37:27 +00:00
} else if (level == Level.WARNING) {
2019-05-02 22:37:32 +00:00
return yellow;
2019-06-06 14:37:27 +00:00
} else if (level == Level.INFO) {
2019-05-02 22:37:32 +00:00
return cyan;
2019-06-06 14:37:27 +00:00
} else if (level == Level.CONFIG ||
2019-05-02 22:37:32 +00:00
level == Level.FINE ||
level == Level.FINER ||
level == Level.FINEST) return lightGray;
return resetAll;
}