forked from iBreaker/bjguahao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
113 lines (91 loc) · 2.62 KB
/
log.py
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
106
107
108
109
110
111
112
#!/usr/bin/env python
# -*- coding: utf-8
import time
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class Debug_level():
debug = 1
info = 2
error = 3
global debug_level
global filesystemencoding
class Log(object):
"""
日志
"""
@staticmethod
def load_config():
"""获取log配置"""
global debug_level
global filesystemencoding
filesystemencoding = sys.getfilesystemencoding()
try:
with open('config.json') as json_file:
data = json.load(json_file)
data = data[0]
if data["DebugLevel"] == "info":
debug_level = Debug_level.info
elif data["DebugLevel"] == "debug":
debug_level = Debug_level.debug
elif data["DebugLevel"] == "error":
debug_level = Debug_level.error
Log.info("DebugLevel设置为:" + str(debug_level))
Log.debug("DebugTest" )
except Exception, e:
debug_level = Debug_level.info
Log.error(repr(e))
Log.error("获取Log模块配置失败,DebugLevel设置为默认值:info")
@staticmethod
def get_time():
"""
获取时间
"""
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
@staticmethod
def set_encoding(msg):
"""
windows console didn't support utf-8
"""
global filesystemencoding
if filesystemencoding == 'mbcs':
msg = msg.encode("GBK")
if filesystemencoding== 'utf-8':
msg = msg.encode("utf-8")
return msg
@staticmethod
def info(msg):
"""
info
"""
global debug_level
msg = Log.set_encoding(msg)
if debug_level <= Debug_level.info:
print("\033[0;37m " + Log.get_time() + " [info] " + msg + "\033[0m")
@staticmethod
def debug(msg):
"""
debug
"""
global debug_level
msg = Log.set_encoding(msg)
if debug_level <= Debug_level.debug:
print("\033[0;34m " + Log.get_time() + " [debug] " + msg + "\033[0m")
@staticmethod
def error(msg):
"""
输出错误
"""
global debug_level
msg = Log.set_encoding(msg)
if debug_level <= Debug_level.error:
print("\033[0;31m " + Log.get_time() + " [error] " + msg + "\033[0m")
@staticmethod
def exit(msg):
"""
打印信息,并退出
"""
Log.error(msg)
Log.error("exit")
exit(0)