Python日志模块logging格式化封装
来源:互联网 发布:拉里·伯德数据 编辑:程序博客网 时间:2024/05/21 06:11
一、前言:
对Python 日志模块logging进行了格式化输出封装,形成自己的日志格式。以后就可以拿来主义了。
1.输出到控制台。
2.输出到当前workspace指定文件。
二、代码:
##模块[log.py]
import logging
import unittest
import sys
def handleException(excType, excValue, traceback, logger):
logger.error("Uncaught exception", exc_info=(excType, excValue, traceback))
class Log(object):
@staticmethod
def log(tag, message, level=logging.INFO):
"""
Log messages to console and file(create to current workspace)
Args:
tag: message tag
message: the message
level(optional): logging level of the message
"""
logger = Log.get_logger(tag)
if level == logging.INFO:
logger.info(message)
if level == logging.WARNING:
logger.warning(message)
if level == logging.DEBUG:
logger.debug(message)
if level == logging.CRITICAL:
logger.critical(message)
if level == logging.ERROR:
logger.error(message)
@staticmethod
def get_logger(tag):
"""
Setup the logger
"""
if len(logging.root.handlers) == 0:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s [%(levelname)s][%(name)s]: %(message)s',
filename='console.log',
filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s [%(levelname)s][%(name)s]: %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
sys.excepthook = handleException
return logging.getLogger(tag)
三、测试:
from log import Log
Log.log(“tag”,"test log model~")
尊重劳动成果,转载请注明出处,Thx。
- 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 +selenium logging 模块封装
- hibernate 多对多(many-to-many)
- 一个批量删除文件的脚本
- mac下XAMPP服务器配置多站点配置局域网配置
- eclipse快捷键
- mysql优化
- Python日志模块logging格式化封装
- Linux系统编程——vfork() 函数详解
- linux环境下使用opencv训练级联分类器traincascade
- Linux系统编程——进程的控制:结束进程、等待进程结束
- C++函数模板使用中的注意事项
- HEVC,VP9,x264性能对比
- 圆形切换按钮
- 随笔:spring+quartz定时任务(1)
- Android Studio jni编译生成so文件