python logging.basicConfig 封装,,第一次使用python的
python logging.basicConfig 封装,,第一次使用python的
第一次使用python的logging模块,可以如下做基本的配置
logging.basicConfig(level=logging.DEBUG, filename='debug.log', format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
下面的代码用一个简单的函数封装了对logging的基础调用。
# -*- coding: utf-8 -*-import datetimeimport loggingimport osimport sysdef quick_start_log(log_fn=None, mode=None, level=logging.DEBUG, \ format='%(asctime)s|%(name)s|%(levelname)s| %(message)s'): ''' simplest basicConfig wrapper, open log file and return default log handler ''' if log_fn is None: now = datetime.datetime.now() ts = now.strftime('%Y-%m-%d_%H%M%S') log_fn = '%s.%s.log' % (sys.argv[0], ts) if mode is None: mode = 'w' logging.basicConfig(level=level, format=format, filename=log_fn, filemode=mode) logger = logging.getLogger('main') if mode.lower() == 'a': logger.info('---=== START ===---') return loggerif __name__ == '__main__': log = quick_start_log() log.info('message') log.fatal('exit')
相关内容
- Python实现简单的缓存和缓存decorator,pythondecorator,一个简
- python解决八皇后算法,python皇后算法,python解决经典算法
- python下载Gmail邮箱中的所有邮件,pythongmail,#!/usr/bin/p
- python管理windows的环境变量,python环境变量,下面的代码
- python webpy中显示进程中的所有类型对象占用的内存大小
- python读写文件,和设置文件的字符编码比如utf-8,pyth
- python抓取图片示例,,[Python]代码#!
- Python sql server和postgresql的表结构转换,pythonpostgresql,[
- Python 计算已经过去多少个周末,python已经过去,计算已
- python通过MySQLdb访问mysql数据库,mysqldbmysql,需要安装My
评论关闭