Skip to content

Commit c14db94

Browse files
committed
Improvements
1 parent 32f6174 commit c14db94

File tree

11 files changed

+280
-90
lines changed

11 files changed

+280
-90
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.6.0
2+
- Add option to output timestamp
3+
- Add option to disable color
4+
- Add `LogOutput`
5+
- Behaviour change of `LogPrinter`
6+
- Remove dependency
7+
18
## 0.5.0
29
- Add emojis
310
- `LogFilter` is a class now

README.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ When creating a logger, you can pass some options:
6363
var logger = Logger(
6464
printer: PrettyPrinter(), // Use the PrettyPrinter to format and print log
6565
filter: null, // Use the default LogFilter (-> only log in debug mode)
66+
output: null, // Use the default LogOutput (-> send everything to console)
6667
);
6768
```
6869

@@ -74,41 +75,66 @@ var logger = Logger(
7475
methodCount: 2, // number of method calls to be displayed
7576
errorMethodCount: 8, // number of method calls if stacktrace is provided
7677
lineLength: 120, // width of the output
78+
colors: true, // Colorful log messages
7779
printEmojis: true, // Print an emoji for each log message
80+
printTime: false // Should each log print contain a timestamp
7881
),
7982
)
8083
```
8184

85+
86+
## LogFilter
87+
88+
The `LogFilter` decides which logs should be shown and which don't.
89+
The default implementation (`DebugFilter`) shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted.
90+
91+
You can create your own `LogFilter` like this:
92+
```dart
93+
class MyFilter extends LogFilter {
94+
@override
95+
bool shouldLog(Level level, dynamic message, [dynamic error, StackTrace stackTrace]) {
96+
return true;
97+
}
98+
}
99+
```
100+
This will show all logs even in release mode. (**NOT** a good idea)
101+
102+
82103
## LogPrinter
83104

84105
You can implement your own `LogPrinter`. This gives you maximum flexibility. A very basic printer could look like this:
85106

86-
```
107+
```dart
87108
class MyPrinter extends LogPrinter {
88109
@override
89110
void log(Level level, dynamic message, dynamic error, StackTrace stackTrace) {
90-
print(message);
111+
println(message);
91112
}
92113
}
93114
```
94115

116+
**Important:** Every implementation has to send its output using the `println()` method.
117+
95118
If you created a cool `LogPrinter` which might be helpful to others, feel free to open a pull request. :)
96119

97-
## LogFilter
98120

99-
The `LogFilter` decides which logs should be shown and which don't.
100-
The default implementation (`DebugFilter`) shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted.
121+
## LogOutput
122+
123+
`LogOutput` sends the log lines to the desired destination. The default implementation (`ConsoleOutput`) send every line to the system console.
101124

102-
You can create your own `LogFilter` like this:
103125
```dart
104-
class MyFilter extends LogFilter {
126+
class ConsoleOutput extends LogOutput {
105127
@override
106-
bool shouldLog(Level level, dynamic message, [dynamic error, StackTrace stackTrace]) {
107-
return true;
128+
void output(Level level, List<String> lines) {
129+
for (var line in lines) {
130+
print(line);
131+
}
108132
}
109133
}
110134
```
111-
This will show all logs even in release mode. (**NOT** a good idea)
135+
136+
Possible future `LogOutput`s could send to a file, firebase or to Logcat. Feel free to open pull requests.
137+
112138

113139
## MIT License
114140
```

art/screenshot.png

4.68 KB
Loading

example/lib/main.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import 'package:logger/logger.dart';
22

3-
var logger = Logger();
3+
var logger = Logger(
4+
printer: PrettyPrinter(),
5+
);
46

5-
var loggerNoStack = Logger(printer: PrettyPrinter(methodCount: 0));
7+
var loggerNoStack = Logger(
8+
printer: PrettyPrinter(methodCount: 0),
9+
);
610

7-
void main() {
11+
void main() async {
812
demo();
913
}
1014

lib/logger.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
library logger;
33

44
import 'dart:convert';
5-
import 'package:ansicolor/ansicolor.dart';
5+
import 'src/ansi_color.dart';
66

7-
part 'src/debug_filter.dart';
8-
part 'src/pretty_printer.dart';
7+
part 'src/default/console_output.dart';
8+
part 'src/default/debug_filter.dart';
9+
part 'src/default/pretty_printer.dart';
910
part 'src/logger.dart';

lib/src/ansi_color.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class AnsiColor {
2+
/// ANSI Control Sequence Introducer, signals the terminal for new settings.
3+
static const ansiEsc = '\x1B[';
4+
5+
/// Reset all colors and options for current SGRs to terminal defaults.
6+
static const ansiDefault = "${ansiEsc}0m";
7+
8+
final int fg;
9+
final int bg;
10+
final bool color;
11+
12+
AnsiColor.none()
13+
: fg = null,
14+
bg = null,
15+
color = false;
16+
17+
AnsiColor.fg(this.fg)
18+
: bg = null,
19+
color = true;
20+
21+
AnsiColor.bg(this.bg)
22+
: fg = null,
23+
color = true;
24+
25+
String toString() {
26+
if (fg != null) {
27+
return "${ansiEsc}38;5;${fg}m";
28+
} else if (bg != null) {
29+
return "${ansiEsc}48;5;${bg}m";
30+
} else {
31+
return "";
32+
}
33+
}
34+
35+
String call(String msg) {
36+
if (color) {
37+
return "${this}$msg$ansiDefault";
38+
} else {
39+
return msg;
40+
}
41+
}
42+
43+
AnsiColor toFg() => AnsiColor.fg(bg);
44+
45+
AnsiColor toBg() => AnsiColor.bg(fg);
46+
47+
/// Defaults the terminal's foreground color without altering the background.
48+
String get resetForeground => color ? "${ansiEsc}39m" : "";
49+
50+
/// Defaults the terminal's background color without altering the foreground.
51+
String get resetBackground => color ? "${ansiEsc}49m" : "";
52+
53+
static int grey(double level) => 232 + (level.clamp(0.0, 1.0) * 23).round();
54+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
part of logger;
2+
3+
/// Default implementation of [LogOutput].
4+
///
5+
/// It sends everything to the system console.
6+
class ConsoleOutput extends LogOutput {
7+
@override
8+
void output(Level level, List<String> lines) {
9+
for (var line in lines) {
10+
print(line);
11+
}
12+
}
13+
}
File renamed without changes.

0 commit comments

Comments
 (0)