log
log
component: Log component. Used to format message content and record it to logs, supports customizing log format using JavaScript scripts.
JavaScript scripts support ECMAScript 5.1(+) syntax specifications and some ES6 specifications, such as: async/await/Promise/let. Custom Go functions can be called within scripts, please refer to udf.
# Configuration
Field | Type | Description | Default Value |
---|---|---|---|
jsScript | string | Log formatting script | None |
jsScript
: The JavaScript script used to format log content. This field serves as the body of the following function:function ToString(msg, metadata, msgType, dataType) { ${jsScript} }
1
2
3Function parameter descriptions:
- msg: Message content, intelligently converted based on data type
- When
dataType=JSON
, type isjsonObject
, fields can be accessed usingmsg.field
- When
dataType=BINARY
, type isUint8Array
, byte-level operations are possible - For other dataTypes, type is
string
- When
- metadata: Message metadata, type is
jsonObject
- msgType: Message type, type is
string
- dataType: Message data type, type is
string
(e.g., JSON, TEXT, BINARY, etc.)
The function return value must be of type
string
, and the returned string will be recorded as the log content.- msg: Message content, intelligently converted based on data type
TIP
- The logger can be configured via config.Logger
- Default output is to the console
- Supports configuring log level, output format, etc.
- Supports intelligent processing of multiple data types, including JSON object access and binary data handling
# Data Type Support
This component supports multiple message data types:
- JSON type: Automatically parsed into a JavaScript object, properties can be accessed directly
- BINARY type: Converted to Uint8Array, convenient for byte-level operations
- TEXT type: Remains in string format
- Other types: Uniformly processed as strings
# Relation Type
- Success: Execution successful, message is sent to the
Success
chain - Failure: Execution failed, message is sent to the
Failure
chain
# Execution Result
This component does not modify the content of msg.Data
and msg.Metadata
.
# Configuration Examples
# Basic Example
{
"id": "s1",
"type": "log",
"name": "Log Message",
"configuration": {
"jsScript": "return 'Incoming message:\\n' + JSON.stringify(msg) + '\\nIncoming metadata:\\n' + JSON.stringify(metadata);"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# JSON Data Processing Example
{
"id": "s2",
"type": "log",
"name": "Log Temperature",
"configuration": {
"jsScript": "if (dataType === 'JSON' && msg.temperature !== undefined) { return 'Temperature: ' + msg.temperature + '°C, Device: ' + (metadata.deviceId || 'unknown'); } else { return 'Non-JSON message: ' + JSON.stringify(msg); }"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Binary Data Processing Example
{
"id": "s3",
"type": "log",
"name": "Log Binary Data",
"configuration": {
"jsScript": "if (dataType === 'BINARY') { return 'Binary data length: ' + msg.length + ' bytes, first byte: ' + (msg.length > 0 ? msg[0] : 'empty'); } else { return 'Text message: ' + msg; }"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Intelligent Multi-Type Processing Example
{
"id": "s4",
"type": "log",
"name": "Intelligent Log Formatting",
"configuration": {
"jsScript": "var prefix = '[' + msgType + '][' + dataType + '] '; switch(dataType) { case 'JSON': return prefix + 'JSON data: ' + JSON.stringify(msg); case 'BINARY': return prefix + 'Binary data: ' + msg.length + ' bytes'; default: return prefix + 'Text data: ' + msg; }"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Edit this page on GitHub (opens new window)
Last Updated: 2025/06/29, 01:38:12