有奖捉虫:云通信与企业服务文档专题,速来> HOT

操作场景

用户可以使用原有日志打印方式(console 对象打印日志),也可以使用 tcb-admin-node 封装的自定义打印日志。
使用 console 打印日志:可使用日志等级loginfowarnerror不会自动建立 键值索引,其中日志内容会封装至日志内容中 msg 字段的值。
自定义打印日志:需要导入 tcb-admin-node,可使用loginfowarnerror ,日志内容会自动增加日志字段并建立 键值索引

导入 SDK

const admin = require('tcb-admin-node')
注意
因导入的 SDK 为 nodejs 语言的 SDK,故下述的操作示例均为 nodejs 语言。

操作用法

log

用例 log:
admin.logger().log({content: "this is a log"})

日志打印:
{
"level": "log",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is a log"
}

console.log("this is a log")

日志打印:
{
"level": "log",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is a log"
}
说明
SDK 方式需要用户输入 object 类型参数。

info

用例 info:
admin.logger().info({content: "this is an info"})

日志打印:
{
"level": "info",
"timestamp": "1565864885000003",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is an info"
}

console.info("this is an info")

日志打印:
{
"level": "info",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is an info"
}
说明
SDK 方式需要用户输入 object 类型参数。

warn

用例 warn:
admin.logger().warn({content: "this is a warn"})

日志打印:
{
"level": "warn",
"timestamp": "1565864885000004",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is a warn"
}

console.warn("this is a warn")

日志打印:
{
"level": "warn",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is a warn"
}
说明
SDK 方式需要用户输入 object 类型参数。

error

用例 error:
admin.logger().error({content: "this is an error"})

日志打印:
{
"level": "error",
"timestamp": "1565864885000005",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is an error"
}

console.error("this is an error")

日志打印:
{
"level": "error",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is an error"
}
说明
SDK 方式需要用户输入 object 类型参数。

日志格式

日志打印后的格式会自带系统默认的字段,其中默认的系统的字段如下:
字段
类型
默认
说明
level
string
(log/info/warn/error)
timestamp
string
打印日志的时间戳,精度到微秒
function
string
当次调用的函数名称
requestId
string
当次请求的 ID
src
string
system/app
msg
string
简单日志内容
注意
用户自定义打印了日志对象内容则会增加自定义的日志字段,并建立 键值索引
用户自定义日志字段若出现系统默认字段,检索日志 时则会优先自定义日志内容。
限制用户自定义日志字段不能出现以下关键字:"\\_\\_FILENAME\\_\\_""\\_\\_TIMESTAMP\\_\\_","\\_\\_LOGSETID\\_\\_","\\_\\_TOPICID\\_\\_"

键值索引

云开发日志会默认创建键值索引,键值索引可使用于键值检索中,其中默认创建的键值索引包括上述日志默认系统参数索引,以及用户自定义的日志对象属性键值索引,如下:
var array = ["tcb", 123434, true, false, "hello tcb", null, undefined, (new Date).getTime(), true && false, 0 > 1 ? "0<1" : "1大于0"];

var logShort = {
name: "tcb",
timestamp: (new Date).getTime(),
randnumber: Math.random(),
intlog: 1236753171,
stringlog: "testlog",
floatlog: 0.5234562,
arraylog: array,
booleanlog: true,
operationlog: 0 > 1 ? "0<1" : "1大于0",
nulllog: null,
};

admin.logger().log(logShort)
日志打印如下:
{
"level": "log",
"timestamp": "1568281919939",
"function": "PqWC-i16eK",
"requestId": "f51346ea-d542-11e9-9950-525400edfec1",
"src": "app",
"name": "tcb",
"randnumber": "0.4922406878066756",
"intlog": "1236753171",
"stringlog": "testlog",
"floatlog": "0.5234562",
"arraylog": "["tcb",\\n 123434,\\n true,\\n false,\\n "hello tcb",\\n null,\\n null,\\n 1568281919939,\\n false,\\n "1大于0"]",
"booleanlog": "true",
"operationlog": "1大于0",
"nulllog": "null"
}
则默认可以用以上输出的键值索引,例如:leveltimestampfunctionrequestidsrcname , randnumberintlogstringlogfloatlogarrarylogbooleanlogoperationlog , nulllog