From 601b7b406b26ae4aae773bff5a509f68504e67c6 Mon Sep 17 00:00:00 2001
From: Tobe O <thosakwe@gmail.com>
Date: Fri, 9 Aug 2019 18:53:32 -0400
Subject: [PATCH] 1.0.0

---
 CHANGELOG.md            |  2 ++
 README.md               | 32 +++++++++++++++++++++++++-
 analysis_options.yaml   |  4 ++++
 example/main.dart       | 11 +++++++++
 lib/pretty_logging.dart | 50 +++++++++++++++++++++++++++++++++++++++++
 pubspec.yaml            | 12 ++++++++++
 6 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 CHANGELOG.md
 create mode 100644 analysis_options.yaml
 create mode 100644 example/main.dart
 create mode 100644 lib/pretty_logging.dart
 create mode 100644 pubspec.yaml

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 <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