-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
executable file
·233 lines (183 loc) · 7.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# encoding: utf-8
#===============================================================================
#
# FILE:
#
# USAGE:
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: YOUR NAME (),
# COMPANY:
# VERSION: 1.0
# CREATED:
# REVISION: ---
#===============================================================================
'''Implements a simple log library.
This module is a simple encapsulation of logging module to provide a more
convenient interface to write log. The log will both print to stdout and
write to log file. It provides a more flexible way to set the log actions,
and also very simple. See examples showed below:
Example 1: Use default settings
import log
log.debug('hello, world')
log.info('hello, world')
log.error('hello, world')
log.critical('hello, world')
Result:
Print all log messages to file, and only print log whose level is greater
than ERROR to stdout. The log file is located in '/tmp/xxx.log' if the module
name is xxx.py. The default log file handler is size-rotated, if the log
file's size is greater than 20M, then it will be rotated.
Example 2: Use set_logger to change settings
# Change limit size in bytes of default rotating action
log.set_logger(limit = 10240) # 10M
# Use time-rotated file handler, each day has a different log file, see
# logging.handlers.TimedRotatingFileHandler for more help about 'when'
log.set_logger(when = 'D', limit = 1)
# Use normal file handler (not rotated)
log.set_logger(backup_count = 0)
# File log level set to INFO, and stdout log level set to DEBUG
log.set_logger(level = 'DEBUG:INFO')
# Both log level set to INFO
log.set_logger(level = 'INFO')
# Change default log file name and log mode
log.set_logger(filename = 'yyy.log', mode = 'w')
# Change default log formatter
log.set_logger(fmt = '[%(levelname)s] %(message)s'
'''
__author__ = "tuantuan.lv <[email protected]>"
__status__ = "Development"
__all__ = ['set_logger', 'debug', 'info', 'warning', 'error',
'critical', 'exception']
import os
import sys
import logging
import logging.handlers
# Color escape string
COLOR_RED='\033[1;31m'
COLOR_GREEN='\033[1;32m'
COLOR_YELLOW='\033[1;33m'
COLOR_BLUE='\033[1;34m'
COLOR_PURPLE='\033[1;35m'
COLOR_CYAN='\033[1;36m'
COLOR_GRAY='\033[1;37m'
COLOR_WHITE='\033[1;38m'
COLOR_RESET='\033[1;0m'
# Define log color
LOG_COLORS = {
'DEBUG': '%s',
'INFO': COLOR_GREEN + '%s' + COLOR_RESET,
'WARNING': COLOR_YELLOW + '%s' + COLOR_RESET,
'ERROR': COLOR_RED + '%s' + COLOR_RESET,
'CRITICAL': COLOR_RED + '%s' + COLOR_RESET,
'EXCEPTION': COLOR_RED + '%s' + COLOR_RESET,
}
# Global logger
g_logger = None
class ColoredFormatter(logging.Formatter):
'''A colorful formatter.'''
def __init__(self, fmt = None, datefmt = None):
logging.Formatter.__init__(self, fmt, datefmt)
def format(self, record):
level_name = record.levelname
msg = logging.Formatter.format(self, record)
return LOG_COLORS.get(level_name, '%s') % msg
def add_handler(cls, level, fmt, colorful, **kwargs):
'''Add a configured handler to the global logger.'''
global g_logger
if isinstance(level, str):
level = getattr(logging, level.upper(), logging.DEBUG)
handler = cls(**kwargs)
handler.setLevel(level)
if colorful:
formatter = ColoredFormatter(fmt)
else:
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
g_logger.addHandler(handler)
return handler
def add_streamhandler(level, fmt):
'''Add a stream handler to the global logger.'''
return add_handler(logging.StreamHandler, level, fmt, True)
def add_filehandler(level, fmt, filename , mode, backup_count, limit, when):
'''Add a file handler to the global logger.'''
kwargs = {}
# If the filename is not set, use the default filename
if filename is None:
filename = getattr(sys.modules['__main__'], '__file__', 'log.py')
filename = os.path.basename(filename.replace('.py', '.log'))
filename = os.path.join('/tmp', filename)
kwargs['filename'] = filename
# Choose the filehandler based on the passed arguments
if backup_count == 0: # Use FileHandler
cls = logging.FileHandler
kwargs['mode' ] = mode
elif when is None: # Use RotatingFileHandler
cls = logging.handlers.RotatingFileHandler
kwargs['maxBytes'] = limit
kwargs['backupCount'] = backup_count
kwargs['mode' ] = mode
else: # Use TimedRotatingFileHandler
cls = logging.handlers.TimedRotatingFileHandler
kwargs['when'] = when
kwargs['interval'] = limit
kwargs['backupCount'] = backup_count
return add_handler(cls, level, fmt, False, **kwargs)
def init_logger():
'''Reload the global logger.'''
global g_logger
if g_logger is None:
g_logger = logging.getLogger()
else:
logging.shutdown()
g_logger.handlers = []
g_logger.setLevel(logging.DEBUG)
#这是默认的 def set_logger(filename = None, mode = 'a', level='ERROR:DEBUG',
def set_logger(filename = None, mode = 'a', level='ERROR',
fmt = '%(asctime)s [%(levelname)s] %(filename)s[line:%(lineno)d] %(message)s',
backup_count = 5, limit = 20480, when = None, console = False):
'''Configure the global logger.'''
level = level.split(':')
if len(level) == 1: # Both set to the same level
s_level = f_level = level[0]
else:
s_level = level[0] # StreamHandler log level
f_level = level[1] # FileHandler log level
init_logger()
#许向添加,增加判断,只有console = True才会向STDOUT输出
if console:
add_streamhandler(s_level, fmt)
add_filehandler(f_level, fmt, filename, mode, backup_count, limit, when)
# Import the common log functions for convenient
import_log_funcs()
def import_log_funcs():
'''Import the common log functions from the global logger to the module.'''
global g_logger
curr_mod = sys.modules[__name__]
log_funcs = ['debug', 'info', 'warning', 'error', 'critical',
'exception']
for func_name in log_funcs:
func = getattr(g_logger, func_name)
setattr(curr_mod, func_name, func)
def usages():
print("这是脚本库中用于日志输出的库")
print("使用方法:")
print(" import MyLog as log")
print(" LOG_LEVEL = 'DEBUG' #默认是ERROR")
print(" #LOG_FILE = '/tmp/somelog.log' #指定日志名,默认在/tmp/脚本名.log")
print(" LOG_CONSOLE = True #是否打开控制台输出,默认为不打开")
print(" log.set_logger(level = LOG_LEVEL, console = LOG_CONSOLE) #打开日志( 如果要指定日志,请添加额外参数 filename = LOG_FILE")
print(" log.debug('这是Debug')")
print(" log.info('这是Info')")
print(" log.error('这是Error')")
print(" log.critical('这是Critical')")
# Set a default logger
set_logger()
if __name__ == '__main__':
usages()