log_utils.dart
3.07 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'dart:convert' as convert;
import 'package:flutter/foundation.dart';
import 'package:common_utils/common_utils.dart';
/// 输出Log工具类
class LogUtils {
static const String tag = 'MES';
//const bool isDebug = true;//是否是调试模式
/// debug开关,上线需要关闭
/// App运行在Release环境时,inProduction为true;当App运行在Debug和Profile环境时,inProduction为false
static const bool inProduction = kReleaseMode;
static void print_(String msg) {
if (!inProduction) {
print(msg);
}
}
static void init() {
LogUtil.init(isDebug: !inProduction);
}
static void d(String msg, {String tag = tag}) {
if (!inProduction) {
LogUtil.v(msg, tag: tag);
}
}
static void e(String msg, {String tag = tag}) {
if (!inProduction) {
LogUtil.e(msg, tag: tag);
}
}
static void json(String msg, {String tag = tag}) {
if (!inProduction) {
final dynamic data = convert.json.decode(msg);
if (data is Map) {
_printMap(data);
} else if (data is List) {
_printList(data);
} else
LogUtil.v(msg, tag: tag);
}
}
// https://github.com/Milad-Akarie/pretty_dio_logger
static void _printMap(Map data,
{String tag = tag,
int tabs = 1,
bool isListItem = false,
bool isLast = false}) {
final bool isRoot = tabs == 1;
final String initialIndent = _indent(tabs);
tabs++;
if (isRoot || isListItem) LogUtil.v('$initialIndent{', tag: tag);
data.keys.toList().asMap().forEach((index, key) {
final bool isLast = index == data.length - 1;
var value = data[key];
if (value is String) value = '\"$value\"';
if (value is Map) {
if (value.length == 0)
LogUtil.v('${_indent(tabs)} $key: $value${!isLast ? ',' : ''}',
tag: tag);
else {
LogUtil.v('${_indent(tabs)} $key: {', tag: tag);
_printMap(value, tabs: tabs);
}
} else if (value is List) {
if (value.length == 0) {
LogUtil.v('${_indent(tabs)} $key: ${value.toString()}', tag: tag);
} else {
LogUtil.v('${_indent(tabs)} $key: [', tag: tag);
_printList(value, tabs: tabs);
LogUtil.v('${_indent(tabs)} ]${isLast ? '' : ','}', tag: tag);
}
} else {
final msg = value.toString().replaceAll('\n', '');
LogUtil.v('${_indent(tabs)} $key: $msg${!isLast ? ',' : ''}', tag: tag);
}
});
LogUtil.v('$initialIndent}${isListItem && !isLast ? ',' : ''}', tag: tag);
}
static void _printList(List list, {String tag = tag, int tabs = 1}) {
list.asMap().forEach((i, e) {
final bool isLast = i == list.length - 1;
if (e is Map) {
if (e.length == 0) {
LogUtil.v('${_indent(tabs)} $e${!isLast ? ',' : ''}', tag: tag);
} else {
_printMap(e, tabs: tabs + 1, isListItem: true, isLast: isLast);
}
} else {
LogUtil.v('${_indent(tabs + 2)} $e${isLast ? '' : ','}', tag: tag);
}
});
}
static String _indent([int tabCount = 1]) => ' ' * tabCount;
}