python日志log
2018-10-17 本文已影响9人
曦宝
# -*- coding: utf-8 -*-
# this is a simple example
import logging
import datetime
# define the log file, file mode and logging level
logging.basicConfig(filename='example.log', filemode="w", level=logging.DEBUG)
logging.info(datetime.datetime.now())
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
![](https://img.haomeiwen.com/i659952/831b072c67ce9957.png)
![](https://img.haomeiwen.com/i659952/24a64d482c458462.png)
![](https://img.haomeiwen.com/i659952/cab56ab06b7c7671.png)
再加一个额外的功能
import time
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
fname="D:/pythonworkspace/TextSimilarity/log/"+now+"similarity.log"
把这个fname替换到log文件的文件名那里,可以生成一日期时间为名的log日志文件