Python标准模块logging
来源:互联网 发布:淘宝压缩包怎么做 编辑:程序博客网 时间:2024/04/29 21:19
#!/usr/bin/python#coding:utf-8import loggingimport timedef log(error_msg): #获取时间 fm = '%Y-%m-%d %H:%M:%S' YMD = time.strftime(fm,time.localtime()) #设置文件的存储路径,及文件 save_log_file = 'error.log' # 第一步创建一个logger的句柄 logger = logging.getLogger('error') #创建一个处理写入日志文件的handler,参数保存的"日志文件 handler = logging.FileHandler(save_log_file) #设置日志输出的格式 formatter = logging.Formatter("%(asctime)s-%(levelname)s-%(message)s") handler.setFormatter(formatter) #将处理文件的的handler假如到logger中去 logger.addHandler(handler) # Logging有如下级别: DEBUG,INFO,WARNING,ERROR,CRITICAL # 默认级别是WARNING, logging模块只会输出指定level以上的log # 这样的好处, 就是在项目开发时debug用的log, 在产品release阶段不用一一注释, # 只需要调整logger的级别就可以了, 很方便的. logger.setLevel(logging.INFO) #写入日志 logger.info(error_msg) #其他的写入方法 logger.debug(error_msg) logger.warn(error_msg) logger.error(error_msg) logger.critical(error_msg) logger.removeHandler(handler)if __name__ == '__main__': log_info = '传入日志信息' log(log_info)
GetLogger
GetLogger() returns a reference to a logger instance with the specified name if it is provided, or root if not. The names are period-separated hierarchical structures. Multiple calls to getLogger() with the same name will return a reference to the same logger object.
后面会看到这种以'.'分隔的hierarchical structures有什么用.
Formatter
Formatter对象定义了最终log信息的顺序,结构和内容, 后面会详细解释.
Handler
这儿用到了StreamHandler和FileHandler, 用于向不同的输出端打log.
SetLevel
Logging有如下级别: DEBUG,INFO,WARNING,ERROR,CRITICAL
默认级别是WARNING, logging模块只会输出指定level以上的log
这样的好处, 就是在项目开发时debug用的log, 在产品release阶段不用一一注释, 只需要调整logger的级别就可以了, 很方便的.
Formatter
Formatter对象定义了最终log信息的顺序,结构和内容.于基本的logging.Handler类不同,应用可以直接实例化formatter类,当然,如果需要你也可以子例化formatter以便定制它的一些行为.构造函数接受两个可选参数:一个信息格式字符串和一个日期格式字符串.如果没有信息格式字符串,直接输出log信息.如果没有日期格式字符串,默认的格式是:%Y-%m-%d %H:%M:%S
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- Python标准模块logging
- 【python】python标准模块logging
- Python的标准logging模块
- Python的标准logging模块
- 【转】Python标准模块logging
- Python的标准logging模块(1)
- Python的标准logging模块(2)
- Python的标准logging模块(3)
- web服务器中默认首页和默认网站的配置(以apache服务器为例)
- 使用logstash+elasticsearch+kibana快速搭建日志平台
- connman.conf
- Hadoop 1.0与Hadoop 2.0资源管理方案对比
- ACM(935)A Simple Problem(1)
- Python标准模块logging
- Android_Android工程res目录下的资源文件夹屏幕适配
- struts2防止重复提交
- jQuery CSS 选择符
- 【Unity3D】Opening file Library/FailedAssetImports.txt failed
- SetLayeredWindowAttributes函数:
- RegisterUserFunc方法
- vim命令
- tomcat tomcat tmpFile.renameTo(classFile) failed