RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

    • Standard Components Overview
    • filter

    • action

      • log
        • Configuration
        • Data Type Support
        • Relation Type
        • Execution Result
        • Configuration Examples
          • Basic Example
          • JSON Data Processing Example
          • Binary Data Processing Example
          • Intelligent Multi-Type Processing Example
      • functions
      • delay
      • groupAction
      • iterator
      • for
      • exec
      • join
    • transform

    • external

    • flow

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

目录

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
    3

    Function parameter descriptions:

    • msg: Message content, intelligently converted based on data type
      • When dataType=JSON, type is jsonObject, fields can be accessed using msg.field
      • When dataType=BINARY, type is Uint8Array, byte-level operations are possible
      • For other dataTypes, type is string
    • 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.

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

# 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

# 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

# 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
Edit this page on GitHub (opens new window)
Last Updated: 2025/06/29, 01:38:12
Switch
functions

← Switch functions→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式