Message rewriting with Drools


Since Graylog 2.0 you can use the processing pipelines for more flexible message rewriting.

Graylog can optionally use Drools Expert to evaluate all incoming messages against a user defined rules file. Each message will be evaluated prior to being written to the outputs.

The rule file location is defined in the Graylog configuration file:

# Drools Rule File (Use to rewrite incoming log messages)
rules_file = /etc/graylog.d/rules/graylog.drl

The rules file is located on the file system with a .drl file extension. The rules file can contain multiple rules, queries and functions, as well as some resource declarations like imports, globals, and attributes that are assigned and used by your rules and queries.

For more information on the DRL rules syntax please read the Drools User Guide.

Getting Started

  1. Uncomment the rules_file line in the Graylog configuration file.
  2. Copy the sample rules file to the location specified in your Graylog configuration file.
  3. Modify the rules file to parse/rewrite/filter messages as needed.

Example rules file

This is an example rules file:

rule "Overwrite localhost"
        m : Message( source == "localhost" )
        m.addField("source", "" );"[Overwrite localhost] rule fired: {}", m);

rule "Drop UDP and ICMP Traffic from firewall"
        m : Message( getField("full_message") matches "(?i).*(ICMP|UDP) Packet(.|\n|\r)*" && source == "firewall" )
        m.setFilterOut(true);"[Drop UDP and ICMP Traffic from firewall] rule fired: {}", m);

The log object being used to write log messages from within Drools rules is an instance of the SLF4J Logger interface.

Parsing Message and adding fields

In the following script we turn the PID and the source IP into additional fields:

import org.graylog2.plugin.Message
import java.util.regex.Matcher
import java.util.regex.Pattern

// Raw Syslog
// Example:
//   Apr 18 15:34:58 server01 smtp-glass[3371]: NEW (1/0) on=, src=, ident=, dst=, id=1303151698.3371
rule "SMTP Glass Logging to GELF"
      m : Message( message matches "^smtp-glass.*" )
      Matcher matcher = Pattern.compile("smtp-glass\\\[(\\\d+)].* src (\\\d+.\\\d+.\\\d+.\\\d+)").matcher(m.getMessage());
      if (matcher.find()) {
         m.addField("_pid", Long.valueOf(;

Another example: Adding additional fields and changing the message itself

We send Squid access logs to Graylog using Syslog. The problem is that the host field of the message was set to the IP address of the Squid proxy, which not very useful. This rule overwrites the source and adds other fields:

import java.util.regex.Matcher
import java.util.regex.Pattern

Raw Syslog: squid[2099]: 1339551529.881  55647 TCP_MISS/200 22 GET

squid\[\d+\]: (\d+\.\d+) *(\d+) *(\d+.\d+.\d+.\d+) *(\w+\/\w+) (\d+) (\w+) (.*)
matched: 13:1339551529.881
matched: 29:55647
matched: 35:
matched: 47:TCP_MISS/200
matched: 60:22
matched: 64:GET
matched: 68:

rule "Squid Logging to GELF"
        m : Message( getField("facility") == "local5" )
        Matcher matcher = Pattern.compile("squid\\[\\d+\\]: (\\d+.\\d+) *(\\d+) *(\\d+.\\d+.\\d+.\\d+) *(\\w+\\/\\w+) (\\d+) (\\w+) (.*)").matcher(m.getMessage());

        if (matcher.find()) {
            m.addField("facility", "squid");
            InetAddress addr = InetAddress.getByName(;
            String host = addr.getHostName();
            m.addField("message", + " " +;

Blacklisting messages

You can also use Drools rules to blacklist messages.