You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请教:如何在rsyslogd v8中使用global variables?

Using Global Variables in rsyslogd v8

Awesome question—let's clear this up since the deprecated mmsequence module's replacement (global variables) is indeed supported in rsyslog v8, even if the docs can be a bit tricky to navigate at first.

First, just to confirm the context you laid out:

The "mmsequence" plugin in rsyslog 8.16.0 is deprecated, retained only for backward compatibility. It was built as a workaround for rsyslog v7's lack of global variable support. Now that v8 includes full global variable functionality, this module will be removed entirely down the line—so definitely avoid using it in new configs, as you noted.

Now, here's exactly how to work with global variables in rsyslogd v8:

Defining Global Variables

Start by declaring your global variables at the top of your rsyslog config (usually /etc/rsyslog.conf or files under /etc/rsyslog.d/) using the global() directive. This makes them available across all rulesets and config blocks:

global(
    # String value (enclose in quotes)
    appEnvironment="production"
    # Numeric value (no quotes needed)
    eventCounter=0
)

Accessing Global Variables

To use a global variable anywhere in your config, prefix its name with a $:

# Example: Include the variable in a log template
template(name="taggedLog" type="string" string="[$appEnvironment] %timestamp% %msg%\n")

# Example: Use the variable to filter logs
if $appEnvironment == "production" then {
    action(type="omfile" file="/var/log/production/main.log" template="taggedLog")
}

Updating Global Variables

You can modify global variables on the fly using the set directive. This works anywhere in your config rules:

# Increment a numeric counter for each incoming log
set $eventCounter = $eventCounter + 1;

# Update a string variable based on log content
if $msg contains "critical" then {
    set $appEnvironment="alert_mode";
}

Quick Migration Tip from mmsequence

If you were using mmsequence to generate sequence numbers, replace that with a global counter:

# Instead of loading mmsequence
# module(load="mmsequence")

# Define a global counter
global(seqCounter=0)

# Increment it for each log and use in your template
set $seqCounter = $seqCounter + 1;
template(name="seqLog" type="string" string="[%seqCounter%] %msg%\n")

Key Notes

  • Global variables persist for the full runtime of the rsyslogd process—their values will survive configuration reloads, unless you redefine them in the updated config.
  • Unlike the limited mmsequence module, global variables aren't restricted to sequence numbers; they can store any arbitrary string or numeric data you need.
  • When migrating from mmsequence, replace its sequence tracking with a global numeric variable that you increment using the set directive (as shown in the example above).

内容的提问来源于stack exchange,提问作者the.jxc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:28:21