首页 > 编程语言 > 详细

python-logging

时间:2019-11-13 14:05:22      阅读:65      评论:0      收藏:0      [点我收藏+]

# -*- coding: utf-8 -*-
# 日志系统
# 时间:2017-08-31
# 姓名:xx

import logging
import os
from datetime import datetime


class MainLog:
def __init__(self):
pass

@staticmethod
def getLog():
logging.basicConfig(
# filename=MainLog.logTxt(), # 日志输出到文件
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
return logging

@staticmethod
def logTxt():
today_name = datetime.today().strftime("%Y%m%d")
log_name = datetime.today().strftime("%Y%m%d%H%M%S") + ".txt"
# 新建当天的文件夹路径
path = "F:\\Logs\\" + today_name + "\\"
if os.path.exists(path):
pass
else:
os.makedirs(path)
# 日志文件路径名称
file_path = path + log_name
return file_path

 

 

 

=======================

# -*- coding: utf_8 -*-
# 基础接口请求方法
# 时间:2017-08-31
import requests
from requests.exceptions import ReadTimeout, ConnectionError, RequestException
from Log.main_log import MainLog


class MainResponse:
def __init__(self):
pass

@staticmethod
def mainPost(url=None, params=None, json=None):
u"""
url: 接口请求的url
params: 接口请求的请求头
json: 接口请求的json
"""
logger = MainLog.getLog()
try:
response = requests.post(url=url, params=params, json=json, timeout=10)
code = response.status_code
text = response.text
logger.debug(u"接口请求地址:")
logger.debug(url)
logger.debug(u"接口请求头:")
logger.debug(params)
logger.debug(u"接口请求参数:")
logger.debug(json)
logger.debug(u"接口请求返回码:")
logger.debug(code)
logger.debug(u"接口返回信息:")
logger.debug(text)
return text
except ReadTimeout:
logger.error(u"请求超时")
except ConnectionError:
logger.error(u"请求连接错误")
except RequestException:
logger.error(u"返回错误")

python-logging

原文:https://www.cnblogs.com/oracle614/p/11848328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!