20170621:python日志文件记录

python 日志文件记录

# -*- coding:utf-8 -*- #
import logging

log_format = '%(filename)s [%(asctime)s] [%(levelname)s] %(message)s'
logging.basicConfig(format=log_format, datefmt='%Y-%m-%d %H:%M:%S %p', level=logging.DEBUG)
logging.debug('this message should be logged')


# %(levelname):日志级别的名字格式
# %(levelno)s:日志级别的数字表示
# %(name)s:日志名字
# %(funcName)s:函数名字
# %(asctime):日志时间,可以使用datefmt去定义时间格式,如上图。
# %(pathname):脚本的绝对路径
# %(filename):脚本的名字
# %(module):模块的名字
# %(thread):thread id
# %(threadName):线程的名字
# logging.basicConfig(format=log_format,datefmt='%Y-%m-%d %H:%M:%S %p',level=logging.DEBUG)   #设置日志输出格式和级别。



log_filename = "logging_test.log"
logging.basicConfig(filename=log_filename, filemode='w', level=logging.DEBUG)
logging.debug('this message should be logged')
logging.info('So should this')
logging.warning('And this, too')
# 看了吧,日志的设置都是使用basicConfig()方法,需要注意的是,日志写入文件的默认方式是‘a’,也就是追加,如果想覆盖文件,则使用如上图那样,使用filemode='w'。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值