Skip to content

Commit

Permalink
Added tags
Browse files Browse the repository at this point in the history
  • Loading branch information
pyciko committed Mar 23, 2024
1 parent 3e9f87c commit 8beaf51
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/src/log_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class LogEvent {
final dynamic message;
final Object? error;
final StackTrace? stackTrace;
final String? tag;

/// Time when this log was created.
final DateTime time;
Expand All @@ -15,5 +16,6 @@ class LogEvent {
DateTime? time,
this.error,
this.stackTrace,
this.tag,
}) : time = time ?? DateTime.now();
}
37 changes: 28 additions & 9 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Logger {
final LogFilter _filter;
final LogPrinter _printer;
final LogOutput _output;
final String? _tag;
bool _active = true;

/// Create a new instance of Logger.
Expand All @@ -48,9 +49,11 @@ class Logger {
LogPrinter? printer,
LogOutput? output,
Level? level,
String? tag,
}) : _filter = filter ?? defaultFilter(),
_printer = printer ?? defaultPrinter(),
_output = output ?? defaultOutput() {
_output = output ?? defaultOutput(),
_tag = tag {
var filterInit = _filter.init();
if (level != null) {
_filter.level = level;
Expand All @@ -75,8 +78,9 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
t(message, time: time, error: error, stackTrace: stackTrace);
t(message, time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.trace].
Expand All @@ -85,8 +89,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.trace, message, time: time, error: error, stackTrace: stackTrace);
log(Level.trace, message,
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.debug].
Expand All @@ -95,8 +101,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.debug, message, time: time, error: error, stackTrace: stackTrace);
log(Level.debug, message,
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.info].
Expand All @@ -105,8 +113,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.info, message, time: time, error: error, stackTrace: stackTrace);
log(Level.info, message,
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.warning].
Expand All @@ -115,9 +125,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.warning, message,
time: time, error: error, stackTrace: stackTrace);
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.error].
Expand All @@ -126,8 +137,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.error, message, time: time, error: error, stackTrace: stackTrace);
log(Level.error, message,
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.wtf].
Expand All @@ -138,8 +151,9 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
f(message, time: time, error: error, stackTrace: stackTrace);
f(message, time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message at level [Level.fatal].
Expand All @@ -148,8 +162,10 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
log(Level.fatal, message, time: time, error: error, stackTrace: stackTrace);
log(Level.fatal, message,
time: time, error: error, stackTrace: stackTrace, tag: tag);
}

/// Log a message with [level].
Expand All @@ -159,6 +175,7 @@ class Logger {
DateTime? time,
Object? error,
StackTrace? stackTrace,
String? tag,
}) {
if (!_active) {
throw ArgumentError('Logger has already been closed.');
Expand All @@ -177,6 +194,8 @@ class Logger {
time: time,
error: error,
stackTrace: stackTrace,
// individual log tag has higher priority than Logger tag
tag: tag ?? _tag,
);
for (var callback in _logCallbacks) {
callback(logEvent);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/printers/logfmt_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class LogfmtPrinter extends LogPrinter {
if (event.error != null) {
output.write(' error="${event.error}"');
}
if (event.tag != null) {
output.write(' tag="${event.tag}"');
}

return [output.toString()];
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/printers/pretty_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class PrettyPrinter extends LogPrinter {
timeStr,
errorStr,
stackTraceStr,
event.tag,
);
}

Expand Down Expand Up @@ -391,6 +392,7 @@ class PrettyPrinter extends LogPrinter {
String? time,
String? error,
String? stacktrace,
String? tag,
) {
List<String> buffer = [];
var verticalLineAtLevel = (_includeBox[level]!) ? ('$verticalLine ') : '';
Expand All @@ -416,6 +418,11 @@ class PrettyPrinter extends LogPrinter {
if (_includeBox[level]!) buffer.add(color(_middleBorder));
}

if (tag != null) {
buffer.add(color('$verticalLineAtLevel$tag'));
if (_includeBox[level]!) buffer.add(color(_middleBorder));
}

var emoji = _getEmoji(level);
for (var line in message.split('\n')) {
buffer.add(color('$verticalLineAtLevel$emoji$line'));
Expand Down
5 changes: 3 additions & 2 deletions lib/src/printers/simple_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class SimplePrinter extends LogPrinter {
@override
List<String> log(LogEvent event) {
var messageStr = _stringifyMessage(event.message);
var tagStr = event.tag != null ? ' [${event.tag}]' : '';
var errorStr = event.error != null ? ' ERROR: ${event.error}' : '';
var timeStr = printTime ? 'TIME: ${event.time.toIso8601String()}' : '';
return ['${_labelFor(event.level)} $timeStr $messageStr$errorStr'];
var timeStr = printTime ? ' TIME: ${event.time.toIso8601String()}' : '';
return ['${_labelFor(event.level)}$tagStr $timeStr $messageStr$errorStr'];
}

String _labelFor(Level level) {
Expand Down
13 changes: 13 additions & 0 deletions test/printers/pretty_printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ void main() {
expect(actualLogString, contains(expectedMessage));
});

test('should print a tag', () {
final expectedMessage = 'some message with a tag';
final printer = PrettyPrinter();

final event = LogEvent(Level.debug, expectedMessage,
error: 'some error', stackTrace: StackTrace.current, tag: 'SomeTag');

final actualLog = printer.log(event);
final actualLogString = readMessage(actualLog);
expect(actualLogString, contains('SomeTag'));
expect(actualLogString, contains(expectedMessage));
});

test('should print custom emoji or fallback', () {
final expectedMessage = 'some message with an emoji';
final emojiPrettyPrinter = PrettyPrinter(
Expand Down
9 changes: 8 additions & 1 deletion test/printers/simple_printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void main() {
'some message',
error: 'some error',
stackTrace: StackTrace.current,
tag: 'SomeTag',
);

var plainPrinter = SimplePrinter(colors: false, printTime: false);
Expand All @@ -17,7 +18,7 @@ void main() {
var outputs = plainPrinter.log(event);

expect(outputs, hasLength(1));
expect(outputs[0], '[T] some message ERROR: some error');
expect(outputs[0], '[T] [SomeTag] some message ERROR: some error');
});

group('color', () {
Expand All @@ -42,6 +43,12 @@ void main() {
expect(printer.log(event)[0], contains('TIME'));
});

test('print tag', () {
var printer = SimplePrinter();

expect(printer.log(event)[0], contains('[SomeTag]'));
});

test('does not print time', () {
var printer = SimplePrinter(printTime: false);

Expand Down

0 comments on commit 8beaf51

Please sign in to comment.