跳到主要内容

打印日志

操作场景#

用户可以使用原有日志打印方式(console 对象打印日志),也可以使用 SDK 封装的自定义打印日志。

  • 使用 console 打印日志:可使用日志等级loginfowarnerror不会自动建立 键值索引,其中日志内容会封装至日志内容中 msg 字段的值。
  • 自定义打印日志:需要导入 SDK,可使用loginfowarnerror ,日志内容会自动增加日志字段并建立 键值索引

导入 SDK#

const 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 类型参数。

日志格式#

日志打印后的格式会自带系统默认的字段,其中默认的系统的字段如下:

字段类型默认说明
levelstring(log/info/warn/error)
timestampstring打印日志的时间戳,精度到微秒
functionstring当次调用的函数名称
requestIdstring当次请求的 ID
srcstringsystem/app
msgstring简单日志内容
提示
  • 用户自定义打印了日志对象内容则会增加自定义的日志字段,并建立 键值索引
  • 用户自定义日志字段若出现系统默认字段,检索日志 时则会优先自定义日志内容。
  • 限制用户自定义日志字段不能出现以下关键字:"__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