This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 378
/
lixian_logging.py
91 lines (77 loc) · 2.42 KB
/
lixian_logging.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
__all__ = ['init_logger', 'get_logger']
import logging
INFO = logging.INFO
DEBUG = logging.DEBUG
TRACE = 1
def file_logger(path, level):
import os.path
path = os.path.expanduser(path)
logger = logging.getLogger('lixian')
logger.setLevel(min(level, DEBUG)) # if file log is enabled, always log debug message
handler = logging.FileHandler(filename=path, )
handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
logger.addHandler(handler)
return logger
class ConsoleLogger:
def __init__(self, level=INFO):
self.level = level
def stdout(self, message):
print message
def info(self, message):
if self.level <= INFO:
print message
def debug(self, message):
if self.level <= DEBUG:
print message
def trace(self, message):
pass
class FileLogger:
def __init__(self, path, level=INFO, file_level=None, console_level=None):
console_level = console_level or level
file_level = file_level or level
self.console = ConsoleLogger(console_level)
self.logger = file_logger(path, file_level)
def stdout(self, message):
self.console.stdout(message)
def info(self, message):
self.console.info(message)
self.logger.info(message)
def debug(self, message):
self.console.debug(message)
self.logger.debug(message)
def trace(self, message):
self.logger.log(level=TRACE, msg=message)
default_logger = None
def init_logger(use_colors=True, level=INFO, path=None):
global default_logger
if not default_logger:
if isinstance(level, int):
assert level in (INFO, DEBUG, TRACE)
console_level = level
file_level = level
elif isinstance(level, basestring):
level = level.lower()
if level in ('info', 'debug', 'trace'):
level = {'info': INFO, 'debug': DEBUG, 'trace': TRACE}[level]
console_level = level
file_level = level
else:
console_level = INFO
file_level = DEBUG
for level in level.split(','):
device, level = level.split(':')
if device == 'console':
console_level = {'info': INFO, 'debug': DEBUG, 'trace': TRACE}[level]
elif device == 'file':
file_level = {'info': INFO, 'debug': DEBUG, 'trace': TRACE}[level]
else:
raise NotImplementedError('Invalid logging level: ' + device)
else:
raise NotImplementedError(type(level))
if path:
default_logger = FileLogger(path, console_level=console_level, file_level=file_level)
else:
default_logger = ConsoleLogger(console_level)
def get_logger():
init_logger()
return default_logger