-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_util.py
48 lines (36 loc) · 1.18 KB
/
log_util.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
#!/bin/env python
# -*- encoding: utf-8 -*-
import logging
import logging.handlers
import os
import os.path
global g_log_inst
class Logger(object):
_inst = None
_level_dict = {
'CRITICAL': logging.CRITICAL,
'ERROR': logging.ERROR,
'WARNING': logging.WARNING,
'INFO': logging.INFO,
'DEBUG': logging.DEBUG,
'NOTSET': logging.NOTSET,
}
@classmethod
def start(cls, log_path, name=None, level=None):
if cls._inst is not None:
return cls._inst
fpath = '/'.join(log_path.split('/')[0: -1])
if False == os.path.exists(fpath):
os.mkdir(fpath)
fmt = '[%(levelname)s] %(asctime)s, pid=%(process)d, src=%(filename)s:%(lineno)d, %(message)s'
datefmt = '%Y-%m-%d %H:%M:%S'
cls._inst = logging.getLogger(name)
cls._inst.setLevel(Logger._level_dict['DEBUG'])
handler = logging.handlers.RotatingFileHandler(log_path, maxBytes=500 * (1 << 20), backupCount=8)
fmtter = logging.Formatter(fmt, datefmt)
handler.setFormatter(fmtter)
cls._inst.addHandler(handler)
@classmethod
def get(cls):
return cls._inst
g_log_inst = Logger