forked from sml2h3/python_collect_domain
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
45 lines (30 loc) · 1.11 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
# -*-coding:utf-8 -*-
import logging
class Logger(object):
def __init__(self, name):
self.logger = logging.getLogger(name)
self.logger.setLevel(logging.DEBUG)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('/tmp/test.log')
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
# 定义handler的输出格式formatter
formatter = logging.Formatter('%(asctime)s-[ %(name)s ]-%(levelname)s: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def error(self, msg):
self.logger.error(msg)
def info(self, msg):
self.logger.info(msg)
def warning(self, msg):
self.logger.warning(msg)
def debug(self, msg):
self.logger.debug(msg)
if __name__ == "__main__":
logger = Logger("test")
logger.debug('logger5 debug message')
logger.info('logger5 info message')
logger.warning('logger5 warning message')
logger.error('logger5 error message')