笔记1 min read
Python日志 - logging
Written by
记录日志:
python
import logging # 引入logging模块
import os.path
import time
# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(logging.INFO) # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/Logs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG) # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)记录日志:
python
# 日志
logger.debug('this is a logger debug message')
logger.info('this is a logger info message')
logger.warning('this is a logger warning message')
logger.error('this is a logger error message')
logger.critical('this is a logger critical message')

Related Articles
笔记
Python排序 - sort
原文链接:[Sorting HOW TO - Python 3.10.4 documentation](https://docs.python.org/zh-cn/3.6/howto/sorting.html) Python 列表内置的 list.sort()方法可以直接修改列表。另外 sorted()内置函数,可以传...
笔记
Python字符数字之间的转换函数
| 函数 | 说明 | | ------------------------ | ----------------------------------------------------- | | int(x [,base ]) | 将 x 转换为一个整数 | | long(x [,base ]) | 将 x 转换为一...
笔记
Python常用数据结构
列表 list 列表方法 - list.append(obj) - list.count(obj) - list.extend(seq) - list.index(obj) - list.insert(index, obj) - list.pop([index=-1]) - list.remove(obj) - lis...