Skip to content

Commit 135015b

Browse files
committed
Add emojis
1 parent 7621e19 commit 135015b

File tree

8 files changed

+63
-38
lines changed

8 files changed

+63
-38
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
## 0.5.0
2+
- Add emojis
3+
- `LogFilter` is a class now
4+
15
## 0.4.0
26
- First version of the new logger

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ var logger = Logger(
7171
printer: PrettyPrinter(
7272
methodCount: 2, // number of method calls to be displayed
7373
errorMethodCount: 8, // number of method calls if stacktrace is provided
74-
lineLength : 120 // width of the output
74+
lineLength: 120, // width of the output
75+
printEmojis: true, // Print an emoji for each log message
7576
),
7677
)
7778
```
@@ -93,13 +94,16 @@ If you created a cool `LogPrinter` which might be helpful to others, feel free t
9394

9495
## LogFilter
9596

96-
The `LogFilter` filters which logs should be shown and which don't.
97-
The default implementation shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted.
97+
The `LogFilter` decides which logs should be shown and which don't.
98+
The default implementation (`DebugFilter`) shows all logs with `level >= Logger.level` while in debug mode. In release mode all logs are omitted.
9899

99100
You can create your own `LogFilter` like this:
100101
```dart
101-
var filter = (Level level, dynamic message, dynamic error, StackTrace stackTrace) {
102+
class MyFilter extends LogFilter {
103+
@override
104+
bool shouldLog(Level level, dynamic message, [dynamic error, StackTrace stackTrace]) {
102105
return true;
106+
}
103107
}
104108
```
105109
This will show all logs even in release mode. (**NOT** a good idea)

art/screenshot.png

0 Bytes
Loading

lib/logger.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ library logger;
44
import 'dart:convert';
55
import 'package:ansicolor/ansicolor.dart';
66

7+
part 'src/debug_filter.dart';
78
part 'src/pretty_printer.dart';
89
part 'src/logger.dart';

lib/src/debug_filter.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
part of logger;
2+
3+
/// Default implementation of [LogFilter].
4+
///
5+
/// Shows all logs with `level >= Logger.level` while in debug mode. In release
6+
/// mode all logs are omitted.
7+
class DebugFilter extends LogFilter {
8+
@override
9+
bool shouldLog(Level level, message, [error, StackTrace stackTrace]) {
10+
var shouldLog = false;
11+
assert(() {
12+
if (level.index >= Logger.level.index) {
13+
shouldLog = true;
14+
}
15+
return true;
16+
}());
17+
return shouldLog;
18+
}
19+
}

lib/src/logger.dart

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ enum Level {
1212
nothing,
1313
}
1414

15-
/// `LogFilter` is called every time a new log message is sent and decides if
16-
/// it will be printed or canceled.
17-
///
18-
/// The default implementation is [Logger._defaultFilter].
19-
/// Every implementation should consider [Logger.level].
20-
typedef LogFilter = bool Function(
21-
Level level,
22-
dynamic message, [
23-
dynamic error,
24-
StackTrace stackTrace,
25-
]);
26-
2715
/// An abstract handler of log messages.
2816
///
2917
/// You can implement a `LogPrinter` from scratch or extend [PrettyPrinter].
@@ -32,6 +20,19 @@ abstract class LogPrinter {
3220
void log(Level level, dynamic message, dynamic error, StackTrace stackTrace);
3321
}
3422

23+
/// An abstract handler of log messages.
24+
///
25+
/// You can implement your own `LogFilter` or use [DebugFilter].
26+
/// Every implementation should consider [Logger.level].
27+
abstract class LogFilter {
28+
/// Is called every time a new log message is sent and decides if
29+
/// it will be printed or canceled.
30+
///
31+
/// Returns `true` if the message should be logged.
32+
bool shouldLog(Level level, dynamic message,
33+
[dynamic error, StackTrace stackTrace]);
34+
}
35+
3536
/// Use instances of logger to send log messages to the [LogPrinter].
3637
class Logger {
3738
/// The current logging level of the app.
@@ -48,7 +49,7 @@ class Logger {
4849
/// [PrettyPrinter] and [_defaultFilter] will be used.
4950
Logger({LogPrinter printer, LogFilter filter})
5051
: _printer = printer ?? PrettyPrinter(),
51-
_filter = filter ?? _defaultFilter;
52+
_filter = filter ?? DebugFilter();
5253

5354
/// Log message at level [Level.verbose].
5455
void v(dynamic message, [dynamic error, StackTrace stackTrace]) {
@@ -83,25 +84,8 @@ class Logger {
8384
/// Log message with [level].
8485
void log(Level level, dynamic message,
8586
[dynamic error, StackTrace stackTrace]) {
86-
if (_filter(level, message, error, stackTrace)) {
87+
if (_filter.shouldLog(level, message, error, stackTrace)) {
8788
_printer.log(level, message, error, stackTrace);
8889
}
8990
}
90-
91-
/// Default implementation of [LogFilter]. All log
92-
static bool _defaultFilter(
93-
Level level,
94-
dynamic message, [
95-
dynamic error,
96-
StackTrace stackTrace,
97-
]) {
98-
var shouldLog = false;
99-
assert(() {
100-
if (level.index >= Logger.level.index) {
101-
shouldLog = true;
102-
}
103-
return true;
104-
}());
105-
return shouldLog;
106-
}
10791
}

lib/src/pretty_printer.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ class PrettyPrinter extends LogPrinter {
2929
Level.wtf: AnsiPen()..xterm(199),
3030
};
3131

32+
static final emojis = {
33+
Level.verbose: '',
34+
Level.debug: '🐛 ',
35+
Level.info: '💡 ',
36+
Level.warning: '⚠️ ',
37+
Level.error: '⛔ ',
38+
Level.wtf: '👾 ',
39+
};
40+
3241
static final stackTraceRegex = RegExp(r'#[0-9]+[\s]+(.+) \(([^\s]+)\)');
3342
static const String indent = ' ';
3443

3544
final int methodCount;
3645
final int errorMethodCount;
3746
final int lineLength;
47+
final bool printEmojis;
3848

3949
String topBorder = '';
4050
String middleBorder = '';
@@ -44,6 +54,7 @@ class PrettyPrinter extends LogPrinter {
4454
this.methodCount = 2,
4555
this.errorMethodCount = 8,
4656
this.lineLength = 120,
57+
this.printEmojis = true,
4758
}) {
4859
var doubleDividerLine = StringBuffer();
4960
var singleDividerLine = StringBuffer();
@@ -92,7 +103,7 @@ class PrettyPrinter extends LogPrinter {
92103
if (match.group(2).startsWith('package:logger')) {
93104
continue;
94105
}
95-
var newLine = ("#${count + 1} ${match.group(1)} (${match.group(2)})");
106+
var newLine = ("#$count ${match.group(1)} (${match.group(2)})");
96107
formatted.add(newLine.replaceAll('<anonymous closure>', '()'));
97108
if (++count == methodCount) {
98109
break;
@@ -148,8 +159,10 @@ class PrettyPrinter extends LogPrinter {
148159
output.add(pen(middleBorder));
149160
}
150161

162+
var emoji = printEmojis ? emojis[level] : '';
151163
for (var line in message.split('\n')) {
152-
output.add(pen('$verticalLine $line'));
164+
output.add(pen('$verticalLine $emoji$line'));
165+
emoji = '';
153166
}
154167
output.add(pen(bottomBorder));
155168

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: logger
22
description: Small, easy to use and extensible logger which prints beautiful logs.
3-
version: 0.4.0
3+
version: 0.5.0
44
author: Simon Leier <[email protected]>
55
homepage: https://github.com/leisim/logger
66

0 commit comments

Comments
 (0)