时间打印到日志里
要将当前时间打印到日志中,您可以按照以下步骤进行操作:
- 导入时间模块:
import datetime
- 获取当前时间:
current_time = datetime.datetime.now()
- 将时间转换为字符串格式:
current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S")
- 将时间打印到日志中:
import logging
logging.basicConfig(filename='example.log', level=logging.INFO)
logging.info("当前时间:" + current_time_str)
- 运行代码后,当前时间将以字符串格式打印到名为"example.log"的日志文件中。
请注意,您需要在代码中指定日志文件的名称和路径。此外,您还可以根据需要调整日期和时间的格式。
原文地址: http://www.cveoy.top/t/topic/iYzZ 著作权归作者所有。请勿转载和采集!