本文最后更新于:2023年4月15日 下午
本文记录logging模块的用法
创建文件logger.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import logging
LOG_FILE = 'app_history.log'
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename=LOG_FILE, filemode='a')
console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger(LOG_FILE).addHandler(console)
PYTHON
|
调用
1
| logger.logging.info("%-13s connected: %r" % (ip_address, connected))
PYTHON
|
%(asctime)s
表示这个位置上是字符串形式的当前时间
datefmt='%Y_%m_%d_%H:%M:%S'
指定了时间格式;我们也可以不指定时间格式
查看写出的log文件
1
| 2017-11-23 13:39:35,295 - xxx.py[line:122] INFO [MainWindow] --------- App Starts ---------
APACHE
|