python3--htmltestrunner 日志显示_实时日志:Serverless Python 运行时支持日志实时输出...
開發(fā)前言
作為一名 Serverless 架構的重度使用者,我一直對調試感到恐慌:經常在測試接口的時候,會通過網頁/PostMan 觸發(fā)函數(shù),然后沒得到預期的結果,我就只能傻乎乎的一直點控制臺的日志,等待他能早點出來結果,看看為啥和我預期結果不同。
雖然說 10S,20S 的日志輸出還能接受,但是在調試過程中,真的就是噩夢,一直在想有什么方法可以實現(xiàn)實時日志,我觸發(fā)函數(shù),就馬上能看到,無論是控制臺/API 網關還是 COS 觸發(fā)器,只要被觸發(fā),我就能實時看到日志,這將會對我寫代碼,調試產生重大,超級重大幫助,所以我開發(fā)了這個組件。
為了更加方便,清晰,直觀,我這里做了個使用方法的教程:
使用方法教程:
說明
該模塊用于實現(xiàn)云函數(shù) SCF Python Runtime 的實時日志功能,通過該組件,您可以實時查看到函數(shù)輸出的日志(包括 print 和 logging 等),本組件目前在測試階段,歡迎測試提意見,目前不建議上業(yè)務。
準備
- 安裝 scflog:
安裝時,可能需要 root 權限,否則可能無法使用。安裝完成,可以執(zhí)行 scflog -v 查看是否安裝成功:
scflog 0.1.1- 部署實時日志組件,新建項目,并且建立 serverless.yaml,內容:
組件部署
PythonLogs: component: '@gosls/tencent-pythonlogs' inputs: region: ap-guangzhou這里的參數(shù)是您要將這個組件部署的區(qū)域。該組件可以復用,也就是說這個組件部署完成之后可以一直被使用。
通過 sls --debug 部署:
DEBUG ─ Setting tags for function PythonRealTimeLogs_CleanupDEBUG ─ Creating trigger for function PythonRealTimeLogs_CleanupDEBUG ─ Deployed function PythonRealTimeLogs_Cleanup successfulPythonLogs: 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-rzm1uzikDEBUG ─ Removing any previously deployed API. api-07wq4u9aDEBUG ─ Removing any previously deployed service. service-laabz6zm6s ? PythonLogs ? done項目中使用
在項目中使用該組件的方法很簡單。
- 創(chuàng)建一個文件夾,并進入
mkdir scflogs && cd scflogs
- 初始化項目
scflog init
- 創(chuàng)建 index.py 文件以及 serverless.yaml 文件:
內容是:
from logs import *import timeimport loggingdef 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的觸發(fā)器,地址是上面輸出的地址 + 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實時日志開啟 ...我們可以用瀏覽器通過剛才函數(shù)部署完成返回給我們的地址觸發(fā)函數(shù):
實時日志開啟 ... [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: .......}.......至此,實現(xiàn)實時日志功能。
總結
至此,完成了 Python 語言的實時日志功能,根據(jù)測試來看,性能還算不錯,也還算穩(wěn)定。通過 3 個函數(shù) + APIGW + COS + CAM 完成了一個實時日志功能,理論上也可以復用到 Nodejs 等 Runtime。
傳送門:
GitHub: github.com/serverless 官網:serverless.com
歡迎訪問:Serverless 中文網,您可以在 最佳實踐 里體驗更多關于 Serverless 應用的開發(fā)!
總結
以上是生活随笔為你收集整理的python3--htmltestrunner 日志显示_实时日志:Serverless Python 运行时支持日志实时输出...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序云函数获取用户昵称_小程序云开发云
- 下一篇: random输出1到10之间_第43P,