Skip to content

Commit 3b178fc

Browse files
committed
[mixin_logger] improve log date format performance
1 parent b2029ca commit 3b178fc

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

packages/mixin_logger/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.3
2+
3+
* Improve log dateTime format performance.
4+
15
## 0.0.2
26

37
* make initLogger return Future<void> instead of void. [#136](https://github.com/MixinNetwork/flutter-plugins/pull/136)

packages/mixin_logger/lib/mixin_logger.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ library mixin_logger;
22

33
import 'package:ansicolor/ansicolor.dart';
44
import 'package:flutter/foundation.dart';
5-
import 'package:intl/intl.dart';
65

6+
import 'src/format.dart';
77
import 'src/write_log_to_file_web.dart'
88
if (dart.library.io) 'src/write_log_to_file_io.dart' as platform;
99

@@ -107,12 +107,17 @@ void wtf(String message) {
107107

108108
void _print(String message, _LogLevel level) {
109109
final logToFile = kLogMode || level.index > _LogLevel.debug.index;
110+
111+
if (!logToFile && !kLogMode) {
112+
return;
113+
}
114+
115+
final output = '${formatDateTime(DateTime.now())} ${level.prefix} $message';
110116
if (logToFile && !kIsWeb) {
111-
final now = DateTime.now();
112-
final date = DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(now);
113-
platform.writeLog('$date ${level.prefix} $message');
117+
platform.writeLog(output);
118+
}
119+
if (kLogMode) {
120+
// ignore: avoid_print
121+
print(level.colorize(output));
114122
}
115-
if (!kLogMode) return;
116-
// ignore: avoid_print
117-
print(level.colorize('${level.prefix} $message'));
118123
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
String formatDateTime(DateTime dateTime) {
2+
return '${dateTime.year.toString().padLeft(4, '0')}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} ${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}:${dateTime.second.toString().padLeft(2, '0')}.${dateTime.millisecond.toString().padLeft(3, '0')}';
3+
}

packages/mixin_logger/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: mixin_logger
22
description: Simple logger tool for flutter.
3-
version: 0.0.2
3+
version: 0.0.3
44
homepage: https://github.com/MixinNetwork/flutter-plugins
55

66
environment:
@@ -12,12 +12,12 @@ dependencies:
1212
sdk: flutter
1313
ansicolor: ^2.0.1
1414
path: ^1.8.0
15-
intl: ^0.17.0
1615

1716
dev_dependencies:
1817
flutter_test:
1918
sdk: flutter
2019
flutter_lints: ^2.0.0
20+
intl: ^0.17.0
2121

2222
flutter:
2323

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:mixin_logger/mixin_logger.dart';
4+
import 'package:mixin_logger/src/format.dart';
5+
6+
void main() {
7+
test('format', () {
8+
final time = DateTime.now();
9+
expect(
10+
formatDateTime(time),
11+
equals(DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(time)),
12+
);
13+
expect(
14+
formatDateTime(DateTime(1, 1, 1, 0, 0)),
15+
equals('0001-01-01 00:00:00.000'),
16+
);
17+
expect(
18+
formatDateTime(DateTime(2021, 1, 1, 0, 0)),
19+
equals('2021-01-01 00:00:00.000'),
20+
);
21+
});
22+
23+
test('benchmark format', () {
24+
final DateTime time = DateTime.now();
25+
final stopwatch = Stopwatch()..start();
26+
i('formatDateTime: start');
27+
for (var i = 0; i < 100000; i++) {
28+
formatDateTime(time);
29+
}
30+
i('formatDateTime: ${stopwatch.elapsedMilliseconds}ms');
31+
});
32+
33+
test('benchmark format2', () {
34+
final DateTime time = DateTime.now();
35+
final stopwatch = Stopwatch()..start();
36+
i('formatDateTime2: start');
37+
for (var i = 0; i < 100000; i++) {
38+
DateFormat('yyyy-MM-dd HH:mm:ss.SSS').format(time);
39+
}
40+
i('formatDateTime2: ${stopwatch.elapsedMilliseconds}ms');
41+
});
42+
}

packages/mixin_logger/test/mixin_logger_test.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ void main() {
117117
expect(fileContent, equals('test\n'));
118118
});
119119

120-
121120
test('test write on other isolate', () async {
122121
await LogFileManager.init(dir, 10, 1024 * 1024 * 10);
123122
final manger = LogFileManager.instance!;
@@ -137,7 +136,6 @@ void main() {
137136
final fileContent = File(p.join(dir, 'log_0.log')).readAsStringSync();
138137
expect(fileContent, contains('after initLogger\n'));
139138
});
140-
141139
}
142140

143141
void _writeLog(String message) {

0 commit comments

Comments
 (0)