forked from xuchang116/smCounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_log.py
56 lines (47 loc) · 1.82 KB
/
run_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
import sys
import datetime
import logging
# global hack
sysStdOut = sys.stdout
sysStdErr = sys.stderr
#----------------------------------------------------------------------
# stream handler object that redirects to Python logging facility
#----------------------------------------------------------------------
class RedirectToLogger(object):
def __init__(self):
self.logger = logging.getLogger()
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.debug(line.rstrip())
def flush(self):
pass
#----------------------------------------------------------------------
# initialize the logging
#----------------------------------------------------------------------
def init(logFilePrefix):
# format a log file name
now = datetime.datetime.now()
timestamp = now.strftime("_%Y.%m.%d_%H.%M.%S")
logFileName = logFilePrefix + ".run-log" + timestamp + ".txt"
# set up logging to a log file
logDateFormat = "%Y-%m-%d %H:%M:%S"
logFormat = "%(asctime)s.%(msecs)03d %(message)s"
logging.basicConfig(level=logging.DEBUG,
format=logFormat,
datefmt=logDateFormat,
filename=logFileName,
filemode="w")
# create a stream redirection object for stdout, so print() goes to logger
rtl = RedirectToLogger()
sys.stdout = rtl
sys.stderr = rtl
#----------------------------------------------------------------------
# close log file and restore stdout and stderr
#----------------------------------------------------------------------
def close():
sys.stdout = sysStdOut
sys.stdout = sysStdErr
logger = logging.getLogger()
handler = logger.handlers[0]
handler.stream.close()
logger.removeHandler(handler)