[py]python logging模块
来源:互联网 发布:js input不可编辑属性 编辑:程序博客网 时间:2024/05/29 08:34
默认级别是warning,输出到console
import logginglogging.info(1213231)logging.warning(1213231)
调整级别为info,输出到console
import logginglogging.basicConfig(level=logging.DEBUG)logging.info(1213231)logging.warning(1213231)
日志内容输出到文件
import logginglogging.basicConfig(filename="test.log", level=logging.DEBUG)logging.info(1213231)logging.warning(1213231)
日志基本操作
import logging# 文件 格式f1 = logging.FileHandler("f1.log", 'a', encoding='utf-8')f1.setFormatter(fmt="")# 级别logger1 = logging.Logger("s1", level=logging.INFO)logger1.addHandler(f1)logger1.debug("hello world")
日志基本操作–带格式
import loggingf2 = logging.FileHandler("f2.txt", 'a', encoding='utf8')fmt = logging.Formatter("%(name)s - %(asctime)s - %(message)s")f2.setFormatter(fmt)logger2 = logging.Logger("s1", level=logging.INFO)logger2.addHandler(f2)# logger1.debug("hi1")logger2.info("hi2")
输出到屏幕和文件
#!/usr/bin/env python# encoding: utf-8import logging# 定义handler的输出格式formatter = logging.Formatter( '%(asctime)s--%(name)s--%(filename)s--%(message)s')# 创建一个handler,用于写入日志文件,只输出debug级别以上的日志fh = logging.FileHandler('f3.log')fh.setFormatter(formatter)# 再创建一个handler,用于输出到控制台ch = logging.StreamHandler()ch.setFormatter(formatter)# 创建一个logging命名为mylogger,%(name)s日志格式里可调用这个名字# logger = logging.Logger("mylogger", level=logging.DEBUG)logger = logging.getLogger('mylogger')logger.setLevel(logging.DEBUG)# 给logger添加handlerlogger.addHandler(fh)logger.addHandler(ch)# 记录两条日志logger.info('foorbar')logger.debug('just a test')
封装成函数
import loggingdef InitLogger(filename, level, name): # create a logging object logger = logging.getLogger(name) logger.setLevel(level) # format log file formatter = logging.Formatter( '%(asctime)s %(name)s-%(levelname)s-%(filename)s-[line:%(lineno)s]: %(message)s') # create the logging file handler and format the log file fh = logging.FileHandler(filename, mode='a+') fh.setFormatter(formatter) # create logging print Stream ch = logging.StreamHandler() ch.setFormatter(formatter) # logger object load the hander logger.addHandler(fh) logger.addHandler(ch) return loggerlogger = InitLogger("ma.log", "DEBUG", "maxiaolang")logger.info("hello info")
参考: http://aolens.blog.51cto.com/7021142/1902668
阅读全文
0 0
- [py]python logging模块
- python模块 ---logging模块
- python logging模块学习
- Python标准模块logging
- Python标准模块logging
- python logging模块
- python 之logging模块
- python的logging模块
- Python标准模块logging
- [python相关]logging模块
- Python标准模块logging
- python logging模块学习
- Python标准模块logging
- Python日志模块-logging
- Python模块logging
- python的logging模块
- Python标准模块logging
- Python标准模块logging
- i.MX6 uboot 中 i2c 总线配置使用
- HBase入门教程
- 关于7.0去掉Chrome后,平台默认浏览器报错webview not installed的问题
- 添加
- 剑指offer--二进制中1的个数
- [py]python logging模块
- windows查看端口占用以及关闭相应的进程
- 17102002_(转)tomcat相关脚本的作用
- SpringMVC入门基础
- Java内存区域与内存溢出异常
- Spring Cloud Sleuth (2)-与ELK集成
- JDK1.7的新特性
- MindManager2018,双11走在人前!
- js或php压缩图片