python3--htmltestrunner 日志显示_实时日志:腾讯云Serverless Python运行时支持日志实时输出...

开发前言

作为一名Serverless架构的重度使用者,我一直对调试感到恐慌:经常在测试接口的时候,会通过网页/PostMan触发函数,然后没得到预期的结果,我就只能傻乎乎的一直点控制台的日志,等待他能早点出来结果,看看为啥和我预期结果不同。

033db91c-2844-eb11-8da9-e4434bdf6706.png

虽然说10S,20S的日志输出还能接受,但是在调试过程中,真的就是噩梦,一直在想有什么方法可以实现实时日志,我触发函数,就马上能看到,无论是控制台/API网关还是COS触发器,只要被触发,我就能实时看到日志,这将会对我写代码,调试产生重大,超级重大帮助,所以我开发了这个组件。

使用方法

为了更加方便,清晰,直观,我这里做了两个使用方法的教程:

  • 视频教程
  • 文本教程

视频教程:

053db91c-2844-eb11-8da9-e4434bdf6706.png
https://www.zhihu.com/video/1218593142234259456

文本教程:

说明

该模块用于实现SCF Python Runtime的实时日志功能,通过该组件,您可以实时查看到函数输出的日志(包括print和logging等),本组件目前在测试阶段,欢迎测试提意见,目前不建议上业务。

准备

  • 安装scflog
pip install scflog

安装时,可能需要root权限,否则可能无法使用。安装完成,可以执行scflog -v查看是否安装成功:

scflog 0.1.1
  • 部署实时日志组件,新建项目,并且建立serverless.yaml,内容:

组件部署

PythonLogs:
  component: '@gosls/tencent-pythonlogs'
  inputs:
    region: ap-guangzhou

这里的参数是您要将这个组件部署的区域。该组件可以复用,也就是说这个组件部署完成之后可以一直被使用。

通过sls --debug部署:

DEBUG ─ Setting tags for function PythonRealTimeLogs_Cleanup
DEBUG ─ Creating trigger for function PythonRealTimeLogs_Cleanup
DEBUG ─ Deployed function PythonRealTimeLogs_Cleanup successful

PythonLogs: 
    websocket: ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs

    26s › PythonLogs › done

此时我们需要配置组件:

scflog set -w ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs

配置成功输出:

DFOUNDERLIU-MB0:~ dfounderliu$ scflog set -w ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs
设置成功
    websocket: ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs
    region: ap-guangzhou
    namespace: default

通过sls remove --debug移除

DEBUG ─ Removing any previously deployed API. api-rzm1uzik
DEBUG ─ Removing any previously deployed API. api-07wq4u9a
DEBUG ─ Removing any previously deployed service. service-laabz6zm

6s › PythonLogs › done

项目中使用

在项目中使用该组件的方法很简单。

  • 创建一个文件夹,并进入

mkdir scflogs && cd scflogs

  • 初始化项目

scflog init

  • 创建index.py文件以及serverless.yaml文件:
vim index.py

内容是:

from logs import *
import time
import logging

def main_handler(event, context):
    print("event is: ", event)
    time.sleep(1)
    logging.debug("this is debug_msg")
    time.sleep(1)
    logging.info("this is info_msg")
    time.sleep(1)
    logging.warning("this is warning_msg")
    time.sleep(1)
    logging.error("this is error_msg")
    time.sleep(1)
    logging.critical("this is critical_msg")
    time.sleep(1)
    print("context is: ", event)
    return "hello world"

vim serverless.yaml

内容是:

Hello_World:
  component: "@serverless/tencent-scf"
  inputs:
    name: Hello_World
    codeUri: ./
    handler: index.main_handler
    runtime: Python3.6
    region: ap-guangzhou
    description: My Serverless Function
    memorySize: 64
    timeout: 20
    exclude:
      - .gitignore
      - .git/**
      - node_modules/**
      - .serverless
      - .env
    events:
      - apigw:
          name: serverless
          parameters:
            protocols:
              - http
            serviceName: serverless
            description: the serverless service
            environment: release
            endpoints:
              - path: /test
                method: ANY

通过sls --debug部署:

DEBUG ─ Deployed function Hello_World successful

  Hello_World: 
    Name:        Hello_World
    Runtime:     Python3.6
    Handler:     index.main_handler
    MemorySize:  64
    Timeout:     20
    Region:      ap-guangzhou
    Namespace:   default
    Description: My Serverless Function
    APIGateway: 
      - serverless - http://service-89bjzrye-1256773370.gz.apigw.tencentcs.com/release

  30s › Hello_World › done

此时,我们配置了APIGW的触发器,地址是上面输出的地址 + endpoints中的path例如:

http://service-89bjzrye-1256773370.gz.apigw.tencentcs.com/release/test

此时,我们可以打开实时日志:

scflog logs -n Hello_World -r ap-guangzhou

此时会提醒我们实时日志开启成功:

DFOUNDERLIU-MB0:~ dfounderliu$ scflog logs -n Hello_World -r ap-guangzhou
实时日志开启 ... 

我们可以用浏览器通过刚才函数部署完成返回给我们的地址触发函数:

实时日志开启 ... 
[2020-03-04 16:36:08] :  ......}
[2020-03-04 16:36:09] :  DEBUG debug_msg 
[2020-03-04 16:36:10] :  INFO info_msg 
[2020-03-04 16:36:11] :  WARNING warning_msg 
[2020-03-04 16:36:14] :  ERROR error_msg 
[2020-03-04 16:36:14] :  CRITICAL critical_msg 
[2020-03-04 16:36:16] :  context is: .......}
.......

至此,实现实时日志功能。

总结

至此,完成了Python语言的实时日志功能,根据测试来看,性能还算不错,也还算稳定。通过3个函数+APIGW+COS+CAM完成了一个实时日志功能,理论上也可以复用到Nodejs等Runtime。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值