本文共 2478 字,大约阅读时间需要 8 分钟。
目前函数计算默认会将用户的标准输出写入到用户指定的 logstore ,如果使用了 logger.log()
console.log()
这类函数还会带上时间戳及requestID。
但在很多情况下,我们可能需要做自定义的特定字段的查询,例如比较数字大小,比较字符串长度,做柱状图、饼图,曲线图等等。本文会介绍如何在函数计算中使用创建 json 格式的日志来做结构化的日志输出和查询。
本文假定您已经掌握了的创建函数、调用函数等基本功能。
我们可以参考,先开通日志服务,并为您的 service 增加日志记录。
特别注意的是:我们需要将 json 格式的整个对象序列化为一行,并输出到标准输出。
在 python 中,我们可以使用 print
或者 sys.stdout.write
,值得注意的是 sys.stdout.write
最后的换行符 \n
需要自己加上,否则上下文会串行导致解析失败。
我们先使用 python2.7 创建函数,复制下面的代码,粘贴到编辑框保存。
# -*- coding: utf-8 -*-import jsondef handler(event, context): print "hello function compute" print '{"name":"Amy", "sex":"female", "age":18, "city":"Beijing", "slogen":"function compute is awsome"}' print '{"name":"Alex", "sex":"male", "age":25, "city":"Shanghai", "opts":{"phone":12345678, "hobby":"basketball"}}' print '{"name":"Jack", "sex":"male", "age":35, "city":"Hangzhou", "opts":{"phone":23456789}}' print json.dumps({"msg":"test message", "requestID": context.request_id}) return 'hello world'
可以看到,我们的5个 print 中有3个都是标准的 json 字符串。
其中还有一个是我们使用json.dumps
序列化的 json 字符串。
建议在后续的实际项目中使用 json.dumps
来做 json 字符串的序列化保证代码的正确性,避免使用 string format 来做序列化。
module.exports.handler = function(event, context, callback) { process.stdout.write('hello function compute\n'); process.stdout.write('{"name":"Amy", "sex":"female", "age":18, "city":"Beijing", "slogen":"function compute is awsome"}\n'); process.stdout.write('{"name":"Alex", "sex":"male", "age":25, "city":"Shanghai", "opts":{"phone":12345678, "hobby":"basketball"}}\n'); process.stdout.write('{"name":"Jack", "sex":"male", "age":35, "city":"Hangzhou", "opts":{"phone":23456789}}\n'); process.stdout.write(JSON.stringify({"msg":"test message", "requestID": context.requestID }) + '\n'); callback(null, 'hello world'); };
注意的是,nodejs中,我们不能使用 console.log()
来做输出,而是使用process.stdout.write()
,且需要加上最后的换行符。
配置好函数后,我们来到对应的log store。
查询分析属性>设置
,如图:如图,分别为各个字段创建索引
opts.hobby
创建 text 类型的索引函数调用成功后,我们使用日志查询,预期可以看到如下结果:
关于如何使用日志服务的查询语言,可以详细参考[](。
message.requestID: 7ec6a4bc-86fb-8dd9-7e1e-929a08316875
message.age > 20 and message.sex = "male"
message.opts.hobby = "basketball"
例如在上面的日志中,我们查询分析 message.age < 20
及 message.age >= 20
的占比饼图
详细语法可参考
message.age >= 0 | SELECT case when "message.age" < 20 then '<20' else '>=20' end as age, count(1) group by age
转载地址:http://xfibl.baihongyu.com/