3.10. Rewriting messages

The syslog-ng application can rewrite parts of log messages: it can search and replace text, and also set a specific field to a specified value. Rewriting messages is often used in conjunction with message parsing Section 3.8, “Parsing messages”.

Rewrite rules are similar to filters: they must be defined in the syslog-ng configuration file and used in the log statement.

[Note] Note

The order of filters, rewriting rules, and parsers in the log statement is important, as they are processed sequentially.

To create replace a part of the log message, define the string or regular expression to replace, the string to replace the original text (macros can be used as well), and the field of the message that the rewrite rule should process. Substitution rules can operate on any value available via macros, e.g., HOST, MESSAGE, PROGRAM, or any user-defined macros created using parsers (see Section 8.6, “Message parsers” for details.). Substitution rules use the following syntax:

Declaration:
            rewrite <name_of_the_rule>
                {subst("<string or regular expression to find>", "<replacement string>", value(<field name>), flags());};

A single substitution rule can include multiple substitutions that are applied sequentially to the message. Note that rewriting rules must be included in the log statement to have any effect.

[Tip] Tip

For case-insensitive searches, add the flags(ignore-case) option; to replace every occurrence of the string, add flags(global) option.

[Example] Example 3.43. Using substitution rules

The following example replaces the first occurrence of the string IP in the text of the message with the string IP-Address.

rewrite r_rewrite_subst{subst("IP", "IP-Address", value("MESSAGE"));};

To replace every occurrence, use:

rewrite r_rewrite_subst{subst("IP", "IP-Address", value("MESSAGE"), flags("global"));};

Multiple substitution rules are applied sequentially; the following rules replace the first occurrence of the string IP with the string IP-Addresses.

rewrite r_rewrite_subst{subst("IP", "IP-Address", value("MESSAGE")); subst("Address", "Addresses", value("MESSAGE"));};

To set a field of the message to a specific value, define the string to include in the message, and the field where it should be included. Setting a field can operate on any value available via macros, e.g., HOST, MESSAGE, PROGRAM, or any user-defined macros created using parsers (see Section 8.6, “Message parsers” for details.). Note that this operation completely replaces any previous value of that field. Use the following syntax:

Declaration:
            rewrite <name_of_the_rule>
                {set("<string to include>", value(<field name>));};
[Example] Example 3.44. Setting message fields to a particular value

The following example sets the HOST field of the message to myhost.

rewrite r_rewrite_set{set("myhost", value("HOST"));};

The following example sets the sequence ID field of the RFC5424-formatted (IETF-syslog) messages to a fixed value.

rewrite r_sd { set("55555" value(".SDATA.meta.sequenceId")); };

© 2007-2010 BalaBit IT Security
Please send your comments or documentation bugs to: documentation@balabit.com