Skip to content

Commit 750f6a6

Browse files
authored
Remove logger name when not verbose (#305)
This assumes the logger names do not contain spaces. Based off of dart-lang/build#2194 this assumption appears correct. Closes #301
1 parent ecde807 commit 750f6a6

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

webdev/lib/src/logging.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'package:logging/logging.dart';
1010

1111
var _verbose = false;
1212

13+
var _loggerName = RegExp(r'^\w+: ');
14+
1315
/// Sets the verbosity of the current [logHandler].
1416
void setVerbosity(bool verbose) => _verbose = verbose;
1517

@@ -39,6 +41,7 @@ void _colorLog(Level level, String message, {bool verbose}) {
3941
var multiline = message.contains('\n') && !message.endsWith('\n');
4042
var eraseLine = _verbose ? '' : '\x1b[2K\r';
4143
var colorLevel = color.wrap('[$level]');
44+
if (!verbose) message = trimLoggerName(message);
4245

4346
stdout.write('$eraseLine$colorLevel $message');
4447

@@ -53,6 +56,14 @@ String trimLevel(Level level, String message) => message.startsWith('[$level]')
5356
? message.replaceFirst('[$level]', '').trimLeft()
5457
: message;
5558

59+
/// Removes the logger name from the [message] if one is present.
60+
String trimLoggerName(String message) {
61+
var match = _loggerName.firstMatch(message);
62+
// Remove the logger name.
63+
if (match != null) message = message.substring(match.end);
64+
return message;
65+
}
66+
5667
/// Detects if the [ServerLog] contains a [Level] and returns the
5768
/// resulting value.
5869
///

webdev/test/logging_test.dart

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:build_daemon/data/server_log.dart';
6+
import 'package:logging/logging.dart';
7+
import 'package:test/test.dart';
8+
import 'package:webdev/src/logging.dart';
9+
10+
void main() {
11+
group('trimLoggerName', () {
12+
test('properly removes logger names', () {
13+
expect(trimLoggerName('SomeLogName: foo bar'), equals('foo bar'));
14+
});
15+
16+
test('leaves messages untouched if no logger name is present', () {
17+
expect(trimLoggerName('foo bar looksLikeALoggerName: '),
18+
equals('foo bar looksLikeALoggerName: '));
19+
});
20+
21+
test('works with empty messages', () {
22+
expect(trimLoggerName(''), equals(''));
23+
});
24+
25+
test('assumes logger names is one word', () {
26+
expect(trimLoggerName('not a logger: foo bar'),
27+
equals('not a logger: foo bar'));
28+
expect(trimLoggerName('foo baz: foo bar'), equals('foo baz: foo bar'));
29+
});
30+
31+
test('assumes loggers are directly at the front of a message', () {
32+
expect(
33+
trimLoggerName(' notLogger: foo bar'), equals(' notLogger: foo bar'));
34+
});
35+
});
36+
37+
group('levelForLog', () {
38+
test('correctly returns the level from the log', () {
39+
expect(levelForLog(ServerLog((b) => b.log = '[INFO] foo bar')),
40+
equals(Level.INFO));
41+
expect(levelForLog(ServerLog((b) => b.log = '[SEVERE] foo bar')),
42+
equals(Level.SEVERE));
43+
});
44+
45+
test('returns null if no level is present', () {
46+
expect(levelForLog(ServerLog((b) => b.log = 'foo bar')), isNull);
47+
});
48+
49+
test('only looks for levels at the start of a log', () {
50+
expect(levelForLog(ServerLog((b) => b.log = 'foo bar [INFO]')), isNull);
51+
});
52+
});
53+
54+
group('trimLevel', () {
55+
test('correctly trims the level from the message', () {
56+
expect(trimLevel(Level.INFO, '[INFO]foo bar'), equals('foo bar'));
57+
expect(trimLevel(Level.INFO, '[INFO]no space'), equals('no space'));
58+
});
59+
60+
test('leaves the message untouched if the level is not present', () {
61+
expect(trimLevel(Level.INFO, '[SEVERE] foo bar'),
62+
equals('[SEVERE] foo bar'));
63+
});
64+
});
65+
}

0 commit comments

Comments
 (0)