This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
log.py
44 lines (38 loc) · 1.57 KB
/
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
#!/usr/bin/python python3
# coding=utf-8
'''
Author: whalefall
Date: 2021-07-16 22:17:09
LastEditTime: 2021-07-17 22:40:53
Description: 日志文件
'''
import logging
from logging import handlers
class Logger(object):
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'crit': logging.CRITICAL
} # 日志关系映射
def __init__(self, filename, level='info', backCount=10, fmt='%(asctime)s - %(pathname)s [line:%(lineno)d] - %(levelname)s: %(message)s'):
self.logger = logging.getLogger(filename)
format_str = logging.Formatter(fmt) # 设置日志格式
self.logger.setLevel(self.level_relations.get(level)) # 设置日志级别
sh = logging.StreamHandler() # 往屏幕上输出
sh.setFormatter(format_str) # 设置屏幕上显示的格式
self.logger.addHandler(sh) # 把对象加到logger里
fh = handlers.RotatingFileHandler(
filename=filename, maxBytes=10485760, backupCount=backCount) # 按照文件大小分割日志文件
fh.setLevel(self.level_relations.get(level))
fh.setFormatter(format_str) # 设置文件里写入的格式
self.logger.addHandler(fh)
log = Logger('run.log', level='debug')
if __name__ == '__main__':
log = Logger('run.log', level='debug')
log.logger.debug('详细信息,调试使用')
log.logger.info('正常信息')
log.logger.warning('警告信息')
log.logger.error('错误信息')
log.logger.critical('问题很严重')