请教:如何在rsyslogd v8中使用global variables?
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
mmsequencemodule, 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 thesetdirective (as shown in the example above).
内容的提问来源于stack exchange,提问作者the.jxc

