diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..7906ae6c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 1.0.0 +* Initial release. diff --git a/README.md b/README.md index c717bfa6..45e7a6a4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,32 @@ -# pretty_logging +# pretty\_logging 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); +``` diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..a4f33350 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:pedantic/analysis_options.yaml +analyzer: + strong-mode: + implicit-casts: false diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..a7521393 --- /dev/null +++ b/example/main.dart @@ -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); +} diff --git a/lib/pretty_logging.dart b/lib/pretty_logging.dart new file mode 100644 index 00000000..30917c6b --- /dev/null +++ b/lib/pretty_logging.dart @@ -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; +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..f1e360ac --- /dev/null +++ b/pubspec.yaml @@ -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 +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