Print Logs
Operation Scenarios
Users can use the original log printing method (printing logs via the console object) or employ the SDK-encapsulated custom log printing.
- Using console to print logs: log levels
log
,info
,warn
,error
can be used, which do not automatically establish key-value indexing, and the log content will be encapsulated into the value of the msg field. - Custom log printing: Requires importing the SDK. Log levels such as
log
,info
,warn
, anderror
can be used. The log content will automatically add fields and establish key-value indexing.
SDK Import
const admin = require("@cloudbase/node-sdk");
Since the imported SDK is the Node.js SDK, the following operation samples are all in Node.js.
Operations
log
Use case log:
admin.logger().log({ content: "this is a log" });
Log printing:
{
"level": "log",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is a log"
}
Use case log:
console.log("this is a log");
Log printing:
{
"level": "log",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is a log"
}
The SDK method requires the user to input parameters of type object.
info
Use case info:
admin.logger().info({ content: "this is an info" });
Log printing:
{
"level": "info",
"timestamp": "1565864885000003",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is an info"
}
Use case info:
console.info("this is an info");
Log printing:
{
"level": "info",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is an info"
}
The SDK method requires the user to input parameters of type object.
warn
Use case warn:
admin.logger().warn({ content: "this is a warn" });
Log printing:
{
"level": "warn",
"timestamp": "1565864885000004",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is a warn"
}
Use case warn:
console.warn("this is a warn");
Log printing:
{
"level": "warn",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is a warn"
}
The SDK method requires the user to input parameters of type object.
error
Use case error:
admin.logger().error({ content: "this is an error" });
Log printing:
{
"level": "error",
"timestamp": "1565864885000005",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"content": "this is an error"
}
Use case error:
console.error("this is an error");
Log printing:
{
"level": "error",
"timestamp": "1565864885000002",
"function": "functionName",
"requestid": "123345-123123-213123123-444",
"src": "app",
"msg": "this is an error"
}
The SDK method requires the user to input parameters of type object.
Log Format
The format of printed logs automatically includes system default fields. The default system fields are as follows:
Field | Type | Default | Description |
---|---|---|---|
level | string | Yes | (log/info/warn/error) |
timestamp | string | Yes | Timestamp of the log printed, with microsecond precision |
function | string | Yes | The function name for the current invocation |
requestId | string | Yes | ID of the current request |
src | string | Yes | system/app |
msg | string | No | Simple log content |
- If the user custom prints log object content, custom log fields will be added and key-value indexing will be established.
- If custom log fields contain system default fields, the custom log content will be prioritized during log search.
- Restrict custom log fields from containing the following keywords:
"__FILENAME__"
,"__TIMESTAMP__","__LOGSETID__","__TOPICID__"
.
Key-Value Indexing
CloudBase logs create key-value indexes by default. These key-value indexes can be used in key-value search, including the default system parameter indexes for logs mentioned above and user-defined key-value indexes for log object properties, as follows:
var array = ["tcb", 123434, true, false, "hello tcb", null, undefined, (new Date).getTime(), true && false, 0 > 1 ? "0<1" : "1 is greater than 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 is greater than 0",
nulllog: null,
};
admin.logger().log(logShort)
Log printing is as follows:
{
"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 is greater than 0\"]",
"booleanlog": "true",
"operationlog": "1 is greater than 0",
"nulllog": "null"
}
By default, the key-value indexes output above can be used, for example: level
, timestamp
, function
, requestid
, src
, name
, randnumber
, intlog
, stringlog
, floatlog
, arrarylog
, booleanlog
, operationlog
, nulllog
.