Skip to content

Commit 94c9f9a

Browse files
committed
Added tests and multiple filters and outputs
1 parent 749d040 commit 94c9f9a

17 files changed

+440
-96
lines changed

CHANGELOG.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
## 0.7.0
2+
- Added `ProductionFilter`, `FileOutput`, `MemoryOutput`, `SimplePrinter`
3+
- Breaking: Changed `LogFilter`, `LogPrinter` and `LogOutput`
4+
15
## 0.6.0
2-
- Add option to output timestamp
3-
- Add option to disable color
4-
- Add `LogOutput`
6+
- Added option to output timestamp
7+
- Added option to disable color
8+
- Added `LogOutput`
59
- Behaviour change of `LogPrinter`
610
- Remove dependency
711

812
## 0.5.0
9-
- Add emojis
13+
- Added emojis
1014
- `LogFilter` is a class now
1115

1216
## 0.4.0

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<img src="https://raw.githubusercontent.com/leisim/logger/master/art/logo.svg?sanitize=true" width="200"/>
2-
3-
<hr>
1+
# Logger
42

53
[![Travis](https://img.shields.io/travis/com/leisim/logger/master.svg)](https://travis-ci.com/leisim/logger) [![Version](https://img.shields.io/pub/v/logger.svg)](https://pub.dev/packages/logger) ![Runtime](https://img.shields.io/badge/dart-%3E%3D2.1-brightgreen.svg) ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)
64

@@ -85,14 +83,14 @@ var logger = Logger(
8583

8684
## LogFilter
8785

88-
The `LogFilter` decides which logs should be shown and which don't.
86+
The `LogFilter` decides which log events should be shown and which don't.<br>
8987
The default implementation (`DebugFilter`) shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted.
9088

9189
You can create your own `LogFilter` like this:
9290
```dart
9391
class MyFilter extends LogFilter {
9492
@override
95-
bool shouldLog(Level level, dynamic message, [dynamic error, StackTrace stackTrace]) {
93+
bool shouldLog(LogEvent event) {
9694
return true;
9795
}
9896
}
@@ -102,13 +100,15 @@ This will show all logs even in release mode. (**NOT** a good idea)
102100

103101
## LogPrinter
104102

105-
You can implement your own `LogPrinter`. This gives you maximum flexibility. A very basic printer could look like this:
103+
The `LogPrinter` creates and formats the output, which is then sent to the `LogOutput`.<br>
104+
You can implement your own `LogPrinter`. This gives you maximum flexibility.
106105

106+
A very basic printer could look like this:
107107
```dart
108108
class MyPrinter extends LogPrinter {
109109
@override
110-
void log(Level level, dynamic message, dynamic error, StackTrace stackTrace) {
111-
println(message);
110+
void log(LogEvent event) {
111+
println(event.message);
112112
}
113113
}
114114
```
@@ -120,13 +120,14 @@ If you created a cool `LogPrinter` which might be helpful to others, feel free t
120120

121121
## LogOutput
122122

123-
`LogOutput` sends the log lines to the desired destination. The default implementation (`ConsoleOutput`) send every line to the system console.
123+
`LogOutput` sends the log lines to the desired destination.<br>
124+
The default implementation (`ConsoleOutput`) send every line to the system console.
124125

125126
```dart
126127
class ConsoleOutput extends LogOutput {
127128
@override
128-
void output(Level level, List<String> lines) {
129-
for (var line in lines) {
129+
void output(OutputEvent event) {
130+
for (var line in event.lines) {
130131
print(line);
131132
}
132133
}

art/logo.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ void demo() {
2222
logger.e("Error! Something bad happened", "Test Error");
2323

2424
loggerNoStack.v({"key": 5, "value": "something"});
25+
26+
print("Test\nTest2");
2527
}

lib/logger.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
/// Small, easy to use and extensible logger which prints beautiful logs.
22
library logger;
33

4+
import 'dart:collection';
45
import 'dart:convert';
6+
import 'dart:io';
7+
import 'package:meta/meta.dart';
8+
59
import 'src/ansi_color.dart';
610

7-
part 'src/default/console_output.dart';
8-
part 'src/default/debug_filter.dart';
9-
part 'src/default/pretty_printer.dart';
11+
part 'src/filters/debug_filter.dart';
12+
part 'src/filters/production_filter.dart';
13+
part 'src/outputs/console_output.dart';
14+
part 'src/outputs/file_output.dart';
15+
part 'src/outputs/memory_output.dart';
16+
part 'src/printers/pretty_printer.dart';
17+
part 'src/printers/simple_printer.dart';
18+
19+
part 'src/log_filter.dart';
20+
part 'src/log_output.dart';
21+
part 'src/log_printer.dart';
1022
part 'src/logger.dart';

lib/src/default/debug_filter.dart renamed to lib/src/filters/debug_filter.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ part of logger;
22

33
/// Default implementation of [LogFilter].
44
///
5-
/// Shows all logs with `level >= Logger.level` while in debug mode. In release
5+
/// Prints all logs with `level >= Logger.level` while in debug mode. In release
66
/// mode all logs are omitted.
77
class DebugFilter extends LogFilter {
88
@override
9-
bool shouldLog(Level level, message, [error, StackTrace stackTrace]) {
9+
bool shouldLog(LogEvent event) {
1010
var shouldLog = false;
1111
assert(() {
12-
if (level.index >= Logger.level.index) {
12+
if (event.level.index >= Logger.level.index) {
1313
shouldLog = true;
1414
}
1515
return true;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
part of logger;
2+
3+
/// Prints all logs with `level >= Logger.level` even in production.
4+
class ProductionFilter extends LogFilter {
5+
@override
6+
bool shouldLog(LogEvent event) {
7+
return event.level.index >= Logger.level.index;
8+
}
9+
}

lib/src/log_filter.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
part of logger;
2+
3+
/// An abstract filter of log messages.
4+
///
5+
/// You can implement your own `LogFilter` or use [DebugFilter].
6+
/// Every implementation should consider [Logger.level].
7+
abstract class LogFilter {
8+
void init() {}
9+
10+
/// Is called every time a new log message is sent and decides if
11+
/// it will be printed or canceled.
12+
///
13+
/// Returns `true` if the message should be logged.
14+
bool shouldLog(LogEvent event);
15+
16+
void destroy() {}
17+
}

lib/src/log_output.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
part of logger;
2+
3+
/// Log output receives a [OutputEvent] from [LogPrinter] and sends it to the
4+
/// desired destination.
5+
///
6+
/// This can be an output stream, a file or a network target. [LogOutput] may
7+
/// cache multiple log messages.
8+
abstract class LogOutput {
9+
void init() {}
10+
11+
void output(OutputEvent event);
12+
13+
void destroy() {}
14+
}

lib/src/log_printer.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
part of logger;
2+
3+
/// An abstract handler of log events.
4+
///
5+
/// A log printer creates and formats the output, which is then sent to
6+
/// [LogOutput]. Every implementation has to use the [LogPrinter.println]
7+
/// method to send the output.
8+
///
9+
/// You can implement a `LogPrinter` from scratch or extend [PrettyPrinter].
10+
abstract class LogPrinter {
11+
List<String> _buffer;
12+
13+
void init() {}
14+
15+
/// Is called every time a new [LogEvent] is sent and handles printing or
16+
/// storing the message.
17+
void log(LogEvent event);
18+
19+
void destroy() {}
20+
21+
/// Sends a line to the [LogOutput].
22+
void println(String line) {
23+
_buffer.add(line);
24+
}
25+
}

0 commit comments

Comments
 (0)