打印日志
#
操作场景用户可以使用原有日志打印方式(console 对象打印日志),也可以使用 SDK 封装的自定义打印日志。
- 使用 console 打印日志:可使用日志等级
log
、info
、warn
、error
不会自动建立 键值索引,其中日志内容会封装至日志内容中 msg 字段的值。 - 自定义打印日志:需要导入 SDK,可使用
log
、info
、warn
、error
,日志内容会自动增加日志字段并建立 键值索引。
#
导入 SDKconst admin = require("@cloudbase/node-sdk");
注意
因导入的 SDK 为 Node.js SDK,故下述的操作示例均为 Node.js。
#
操作用法#
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"}
用例 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"}
用例 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"}
用例 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"}
用例 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 | 否 | 简单日志内容 |
提示
#
键值索引云开发日志会默认创建键值索引,键值索引可使用于键值检索中,其中默认创建的键值索引包括上述日志默认系统参数索引,以及用户自定义的日志对象属性键值索引,如下:
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"}
则默认可以用以上输出的键值索引,例如:level
、timestamp
、 function
、 requestid
、src
、 name
, randnumber
、intlog
、 stringlog
、 floatlog
、 arrarylog
、 booleanlog
、 operationlog
, nulllog
。