-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloggetter.py
57 lines (45 loc) · 1.59 KB
/
loggetter.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
import logging
import sys
from tkinter import scrolledtext
from tkinter import WORD
def scrolltest(win,scrolW,scrolH):
scr = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=WORD,relief="sunken",font=('宋体 10'))
return scr
# 用于重定向输出到滚动窗口
class IODirector(object):
def __init__(self, text_area):
self.text_area = text_area
class StdoutDirector(IODirector):
def write(self, msg):
self.text_area.insert('end', msg)
self.text_area.see('end')
self.text_area.update()
def flush(self):
pass
def loggetter(scr):
logger = logging.getLogger() # 不加名称设置root logger
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s %(message)s',
datefmt='[%H:%M:%S]')
# 使用FileHandler输出到文件
fh = logging.FileHandler('user/qiangke_log_file.log','a+',encoding='utf-8')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
cur_fh = logging.FileHandler('user/cur_log_file.log','w',encoding='utf-8')
cur_fh.setLevel(logging.INFO)
cur_fh.setFormatter(formatter)
# 使用StreamHandler输出到屏幕
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
# 添加Handler
logger.addHandler(ch)
logger.addHandler(fh)
logger.addHandler(cur_fh)
sys.stdout = StdoutDirector(scr)
app = logging.StreamHandler(stream=sys.stdout) # added
app.setLevel(logging.INFO)
app.setFormatter(formatter)
logger.addHandler(app)
return logger