Python 语法之logging 思维导图分析

访客 阅读:348 2021-03-31 13:30:09 评论:0

Python3 日志演示功能代码:

getcwd.py

import os 
def get_cwd(): 
    path = os.path.dirname(os.path.abspath(__file__)) 
    #当前文件的绝对路径 
    return path

log.py

import logging 
import time 
import os 
import getcwd 
def get_log(logger_name): 
 
        #创建一个logger 
        logger= logging.getLogger(logger_name) 
        logger.setLevel(logging.INFO) 
 
        #设置日志存放路径,日志文件名 
        #获取本地时间,转换为设置的格式 
        rq = time.strftime('%Y%m%d%H%M',time.localtime(time.time())) 
        #设置所有日志和错误日志的存放路径 
        path = getcwd.get_cwd() 
        #通过getcwd.py文件的绝对路径来拼接日志存放路径 
        all_log_path = os.path.join(path,'Logs\\All_Logs\\') 
        error_log_path = os.path.join(path,'Logs\\Error_Logs\\') 
         # 日志文件夹创建 
        if not os.path.exists(all_log_path): 
            os.makedirs(all_log_path) 
        if not os.path.exists(error_log_path): 
            os.makedirs(error_log_path) 
        #设置日志文件名 
        all_log_name = all_log_path + rq +'.log' 
        error_log_name = error_log_path + rq +'.log' 
       # 日志文件创建 
        if not os.path.exists(all_log_name): 
            open(all_log_name,"a") 
        if not os.path.exists(error_log_name): 
            open(error_log_name,"a") 
        #创建handler 
        #创建一个handler写入所有日志 
        fh = logging.FileHandler(all_log_name) 
        fh.setLevel(logging.INFO) 
        #创建一个handler写入错误日志 
        eh = logging.FileHandler(error_log_name) 
        eh.setLevel(logging.ERROR) 
        #创建一个handler输出到控制台 
        ch = logging.StreamHandler() 
        ch.setLevel(logging.INFO) 
 
        #定义日志输出格式 
        #以时间-日志器名称-日志级别-日志内容的形式展示 
        all_log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 
        #以时间-日志器名称-日志级别-文件名-函数行号-错误内容 
        error_log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(module)s  - %(lineno)s - %(message)s') 
        #将定义好的输出形式添加到handler 
        fh.setFormatter(all_log_formatter) 
        ch.setFormatter(all_log_formatter) 
        eh.setFormatter(error_log_formatter) 
 
 
        #给logger添加handler 
        logger.addHandler(fh) 
        logger.addHandler(eh) 
        logger.addHandler(ch) 
        return  logger 
# Python 函数实例化logging 对象 
log1 = get_log("selenium")

演示功能代码:

import io 
import sys 
#import logging 
from log import log1 
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8') 
 
# logging 日志输出之函数方式一 
# logging.debug("This is a debug log.") 
# logging.info("This is a info log.") 
# logging.warning("This is a warning log.") 
# logging.error("This is a error log.") 
# logging.critical("This is a critical log.") 
# # logging 日志输出之函数方式二 
# logging.log(logging.DEBUG, "This is a debug log.") 
# logging.log(logging.INFO, "This is a info log.") 
# logging.log(logging.WARNING, "This is a warning log.") 
# logging.log(logging.ERROR, "This is a error log.") 
# logging.log(logging.CRITICAL, "This is a critical log.") 
 
# logging 日志输出之函数添加配置参数 
# LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" 
# DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p" 
 
# logging.basicConfig(filename='D:\\python_workspace\\two\\my.log', level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) 
 
# logging.debug("This is a debug log.") 
# logging.info("This is a info log.") 
# logging.warning("This is a warning log.") 
# logging.error("This is a error log.") 
# logging.critical("This is a critical log.") 
 
try: 
    log1.info("开始测试") 
    r = 10/0 
    log1.info("resuit:",r) 
except ZeroDivisionError as e : 
    log1.error("报错信息:",exc_info = 1) 
log1.info('end') 

本文参考:https://www.cnblogs.com/testdjt/p/7834856.html

                http://www.ityouknow.com/python/2019/10/13/python-logging-032.html

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号