-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_logging.py
executable file
·53 lines (41 loc) · 1.51 KB
/
test_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
import logging
import datetime
def setlog():
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s %(name)-4s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
handlers=[logging.FileHandler('my.log', 'w', 'utf-8'), ],
)
print(logging.currentframe)
logging.debug("debug")
logging.info("info")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
def setlog2():
# 基礎設定
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
handlers=[logging.FileHandler('my.log', 'w', 'utf-8'), ])
# 定義 handler 輸出 sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# 設定輸出格式
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# handler 設定輸出格式
console.setFormatter(formatter)
# 加入 hander 到 root logger
logging.getLogger('').addHandler(console)
# root 輸出
logging.info('道可道非常道')
# 定義另兩個 logger
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')
logger1.debug('天高地遠')
logger1.info('天龍地虎')
logger2.warning('天發殺機')
logger2.error('地動天搖')
if __name__=="__main__":
# setlog()
setlog2()