InfoQServerless 架构下如何实现日志的实时输出?( 五 )
printurl = os.environ.get("real_time_log_url")cid = os.environ.get("real_time_log_id")ifurl and cid and os.environ.get("real_time_log_id",None):retmsg = {"coid": cid,"data":" ".join([str(eveObject)foreveObjectinargs])}urllib.request.urlopen(urllib.request.Request(url=url,data=http://news.hoteastday.com/a/json.dumps(retmsg).encode("utf-8")))对logging进行额外的处理 , 将文件中的defwarning(msg, *args, **kwargs):Log a message with severity 'WARNING' on the root logger. If the logger hasno handlers, call basicConfig to add a console handler with a pre-definedformat."""realTimeLogs("WARNING %s %s"%(str(msg)," ".join([str(eveObject)foreveObjectinargs])))iflen(root.handlers) ==0:上报逻辑:
defrealTimeLogs(data):ifurlandcidandos.environ.get("real_time_log_id",None):retmsg = {"coid": cid,"data": data}urllib.request.urlopen(urllib.request.Request(url=url,))封装成工具
- 将重写部分封装成客户端工具
- 将线上函数部分封装成 Component
安装部署实时日志组件 , 新建项目 , 并且建立
serverless.yaml通过sls --debugDEBUG ─ Setting tagsforfunctionPythonRealTimeLogs_CleanupDEBUG ─ Creating triggerforfunctionDEBUG ─ DeployedfunctionPythonRealTimeLogs_Cleanup successfulPythonLogs:websocket: ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs26s ? PythonLogs ?donescflogset-w ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logsDFOUNDERLIU-MB0:~ dfounderliu$ scflogset-w ws://service-laabz6zm-1256773370.gz.apigw.tencentcs.com/test/python_real_time_logs设置成功region: ap-guangzhou函数的初始化与部署在项目中使用该组件的方法很简单 。- 创建一个文件夹 , 并进入
mkdir scflogs &&cdscflogs- 初始化项目
scfloginit -l python- 创建
index.py文件以及serverless.yaml文件:
vimindex.py内容是: fromlogsimport*defmain_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)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 湖人队|扎心?一张图看湖人如何应对马赛克三分雨,神龟5米范围没人理他
- 搜狐新闻|准大学生数码三件套如何选?这些元气好物值得种草
- 想好好做医生的胖子|五十岁女性经常有周身无力和疼痛的困扰!该如何应对?医生告诉您
- 儿童饮食|秋冬季如何让宝宝爱上吃水果?稍微蒸一下美味翻倍,营养价值更高
- 队员|北极科考中“遭遇”北极熊,看科考队员如何与它们和谐相处
- 中国历史发展过程|中国历史发展过程!中国游戏外挂发展史:“辅助工具”是如何成长为“苍
- 金色年华录|都是男扮女装谈恋爱,《少女大人》比《女世子》如何?
- 上海市嘉定区人民政府网站|每年消耗3350吨蔬菜150吨肉,嘉定这家中央厨房如何践行“光盘行动”?
- 上海嘉定|如何让《民法典》走进群众心里?嘉定这些街镇和单位这样做
- 没蜡笔的小新|“整容脸”应该被鄙视吗?医生告诉你千篇一律的好看皮囊如何而来
