This commit is contained in:
Tobe O 2019-08-09 18:53:32 -04:00
parent f5dd02e9ef
commit 601b7b406b
6 changed files with 110 additions and 1 deletions

2
CHANGELOG.md Normal file
View file

@ -0,0 +1,2 @@
# 1.0.0
* Initial release.

View file

@ -1,2 +1,32 @@
# pretty_logging # pretty\_logging
Standalone helper for colorful logging output, using pkg:io AnsiCode. Standalone helper for colorful logging output, using pkg:io AnsiCode.
# Installation
In your `pubspec.yaml`:
```yaml
dependencies:
pretty_logging: 1.0.0
```
# Usage
Basic usage is very simple:
```dart
myLogger.onRecord.listen(prettyLog);
```
However, you can conditionally pass logic to omit printing an
error, provide colors, or to provide a custom print function:
```dart
var pretty = prettyLog(
logColorChooser: (_) => red,
printFunction: stderr.writeln,
omitError: (r) {
var err = r.error;
return err is AngelHttpException && err.statusCode != 500;
},
);
myLogger.onRecord.listen(pretty);
```

4
analysis_options.yaml Normal file
View file

@ -0,0 +1,4 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false

11
example/main.dart Normal file
View file

@ -0,0 +1,11 @@
import 'package:logging/logging.dart';
import 'package:pretty_logging/pretty_logging.dart';
void main() {
Logger.root
..level = Level.ALL
..onRecord.listen(prettyLog)
..info('Hey!')
..finest('Bye!')
..severe('Oops!', StateError('Wrong!'), StackTrace.current);
}

50
lib/pretty_logging.dart Normal file
View file

@ -0,0 +1,50 @@
import 'package:logging/logging.dart';
import 'package:io/ansi.dart';
/// Prints the contents of a [LogRecord] with pretty colors.
///
/// By passing [omitError], you can omit printing the error of a given
/// [LogRecord].
///
/// You can also pass a custom [printFunction] or [logColorChooser].
void prettyLog(LogRecord record,
{bool Function(LogRecord) omitError,
void Function(String) printFunction,
AnsiCode Function(Level) logColorChooser}) {
logColorChooser ??= chooseLogColor;
omitError ??= (_) => false;
printFunction ??= print;
var code = logColorChooser(record.level);
if (record.error == null) printFunction(code.wrap(record.toString()));
if (record.error != null) {
var err = record.error;
if (omitError(record)) return;
printFunction(code.wrap(record.toString() + '\n'));
printFunction(code.wrap(err.toString()));
if (record.stackTrace != null) {
printFunction(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.CONFIG ||
level == Level.FINE ||
level == Level.FINER ||
level == Level.FINEST) {
return lightGray;
}
return resetAll;
}

12
pubspec.yaml Normal file
View file

@ -0,0 +1,12 @@
name: pretty_logging
version: 1.0.0
description: Standalone helper for colorful logging output, using pkg:io AnsiCode.
author: Tobe Osakwe <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/pretty_logging
environment:
sdk: ">=2.0.0 <3.0.0"
dependencies:
io: ^0.3.2
logging: ^0.11.0
dev_dependencies:
pedantic: ^1.0.0