-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
83 lines (66 loc) · 2.89 KB
/
logger.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
#!/usr/bin/python
#-*-coding:utf-8-*-
"""
Copyright
2017 Jeff <[email protected]>
License
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
S the License for the specific language governing permissions and
limitations under the License.
Brief
logging helper
"""
import sys
import time
import logging
import logging.handlers
DEFAULT_LOGFMT = '[%(asctime)s][%(filename)s - %(lineno)s][%(levelname)s] $> %(message)s'
DEFAULT_DATAFMT = '%(message)s'
# common logger creater
# @param logfile - name represent log file
# @param module - name represent log object
# @param loglevel - logging.[CRITICAL, ERROR, WARNING, INFO, DEBUG]
# @param stream - sys.stdout or sys.stderr
# @param b_append_lvlname - append level name to log
def _log2file(logfile, module=None, loglevel = logging.INFO, logfmt = DEFAULT_LOGFMT):
#module = time.strftime(module + "_%Y-%m-%d", time.localtime(time.time()))
logger = logging.getLogger(module)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt=logfmt, datefmt='%Y-%m-%d %H:%M:%S')
# logger | debug file
loghandler = logging.handlers.RotatingFileHandler(logfile, maxBytes = 1024*1024*1024, backupCount = 5) # 实例化handle
loghandler.setFormatter(formatter)
loghandler.setLevel(loglevel)
logger.addHandler(loghandler)
logger.info("#### start ####")
return logger
# log to stdout or stderr
def _log2stream(stream, module=None, loglevel = logging.INFO, logfmt = DEFAULT_LOGFMT):
logger = logging.getLogger(module)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt=logfmt, datefmt='%Y-%m-%d %H:%M:%S')
if stream == sys.stdout or stream == sys.stderr:
loghandler = logging.StreamHandler(stream)
loghandler.setFormatter(formatter)
loghandler.setLevel(loglevel)
logger.addHandler(loghandler)
#
return logger
def log2stdout(module=None, loglevel = logging.INFO, logfmt = DEFAULT_DATAFMT):
return _log2stream(sys.stdout, module, loglevel, logfmt)
def log2stderr(module=None, loglevel = logging.ERROR, logfmt = DEFAULT_DATAFMT):
return _log2stream(sys.stderr, module, loglevel, logfmt)
# set root log file
def log2file(logfile, loglevel = logging.INFO, logfmt = DEFAULT_LOGFMT):
return _log2file(logfile, None, loglevel, logfmt)
# @param module - auto append ".log"
def log2data(module, logfile = None, loglevel = logging.INFO, logfmt = DEFAULT_DATAFMT):
if logfile is None:
logfile = module + ".data"
return _log2file(logfile, module, loglevel, logfmt)