-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.dart
53 lines (43 loc) · 1.6 KB
/
log.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';
class Log {
static const bool _withTime = true;
static final _normalLogger = Logger(
filter: CustomLogFilter(),
printer: SimplePrinter(),
);
static final _exceptionLogger = Logger(
filter: CustomLogFilter(),
printer: PrettyPrinter(methodCount: 8),
);
Log._();
/// Log a message at level [Level.debug].
static void d(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_normalLogger.d(_formatMessage(message), error: error, stackTrace: stackTrace);
}
/// Log a message at level [Level.info].
static void i(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_normalLogger.i(_formatMessage(message), error: error, stackTrace: stackTrace);
}
/// Log a message at level [Level.warning].
static void w(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_exceptionLogger.w(_formatMessage(message), error: error, stackTrace: stackTrace);
}
/// Log a message at level [Level.error].
static void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_exceptionLogger.e(_formatMessage(message), error: error, stackTrace: stackTrace);
}
static String _formatMessage(dynamic message) {
final time = _withTime ? DateFormat('[HH:mm:ss]').format(DateTime.now()) : '';
return '$time $message';
}
}
class CustomLogFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) {
final flutterTest = Platform.environment.containsKey('FLUTTER_TEST');
return kDebugMode && !flutterTest;
}
}