Real-time log: Tencent Cloud Serverless Python supports real-time log output

Development Preface

As a heavy user of the Serverless architecture, I have been panicked about debugging: often when testing the interface, the function will be triggered through the web page / PostMan, and then I did not get the expected result. Diary, waiting for him to come out with the results earlier to see why it is different from what I expected.

Although the log output of 10S and 20S is acceptable, but during the debugging process, it is really a nightmare. I have been thinking about any way to achieve real-time logging. I trigger the function and I can see it immediately, whether it is the console / API The gateway is still a COS trigger. As long as it is triggered, I can see the logs in real time. This will write code for me, and it will have a great help for debugging. So I developed this component.

In order to be more convenient, clear and intuitive, I made a tutorial on how to use it here:

How to use tutorial:

Explanation

This module is used to realize the real-time log function of the cloud function SCF Python Runtime. Through this component, you can view the log output by the function in real time (including print and logging, etc.). Suggest business.

ready

  • Install scflog:
pip install scflog

When you install, you may need rootpermission or may not be available. The installation is complete, you can perform scflog -vto see if the installation was successful:

scflog 0.1.1
  • Deploy real-time log components, create new projects, and build serverless.yaml, content:

Component deployment

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

The parameter here is the area where you want to deploy this component. This component can be reused, which means that this component can be used all the time after deployment.

By sls --debugdeploying:

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

At this point we need to configure the components:

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

Successful configuration output:

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

By sls remove --debugremoving

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

Used in the project

The method of using this component in a project is simple.

  • Create a folder and enter

mkdir scflogs && cd scflogs

  • Initialize the project

scflog init

  • Create a index.pyfile and serverless.yamlfile:
vim index.py

The content is:

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

The content is:

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

By sls --debugdeploying:

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

At this time, we configured the trigger of APIGW, the address is the address output above + the path in endpoints, for example:

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

At this point, we can open the real-time log:

scflog logs -n Hello_World -r ap-guangzhou

At this time, it will remind us that the real-time log is successfully opened:

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

We can use the browser to trigger the function returned to us through the function deployment just completed:

实时日志开启 ... 
[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: .......}
.......

At this point, the real-time log function is realized.

to sum up

So far, the real-time log function of the Python language has been completed. According to the test, the performance is quite good and stable. Through 3 functions + APIGW + COS + CAM to complete a real-time log function, in theory, it can also be reused to Runtime such as Nodejs.


Portal:

Welcome: Serverless Chinese network , you can best practice experience to develop more applications on Serverless in!


Recommended reading: "Serverless Architecture: From Principle, Design to Project Combat"

Guess you like

Origin www.cnblogs.com/serverlesscloud/p/12712096.html