最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
使用PYTHON的LOGGING模块记录日志
时间:2015-08-19 编辑:简简单单 来源:一聚教程网
Python有logging模块可以方便记录程序操作日志,本文简单示例一下logging模块的用法:
首先引入logging:
import xlrd
import MySQLdb
import os
import logging
在主程序中先要初始化配置,如:
if __name__ == "__main__":
logging.basicConfig(filename='./log.txt', level=logging.DEBUG)
path = './excel'
main(path)
指定存放日志的文件,这里是在当前目录的log.txt文件记录日志。在需要记录操作的地方:
try:
c.executemany(
"""INSERT INTO `order` VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
data)
db.commit()
except Exception as e:
logging.info(e)
db.close()
或者
def main(path):
"""
打开目录遍历excel文件并存储到mysql
"""
files = os.listdir(path)
for file in files:
save_file(path + '/' + file)
print(file)
logging.info(file)
看代码高亮的部分,这样就记录日志到指定文件中了
相关文章
- Log4net 日志记录详细介绍及应用 03-25
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24