NXLog Legacy Documentation

Ruby (om_ruby)

This module provides support for forwarding log data with methods written in the Ruby language. See also the xm_ruby and im_ruby modules.

The Ruby modules are compatible with Ruby 2 versions only. More recent versions are unlikely to work.
To examine the supported platforms, see the list of installer packages in the Available Modules chapter.

The Nxlog module provides the following classes and methods.

Nxlog.log_debug(msg)

Send the message msg to the internal logger at DEBUG log level. This method does the same as the core log_debug() procedure.

Nxlog.log_info(msg)

Send the message msg to the internal logger at INFO log level. This method does the same as the core log_info() procedure.

Nxlog.log_warning(msg)

Send the message msg to the internal logger at WARNING log level. This method does the same as the core log_warning() procedure.

Nxlog.log_error(msg)

Send the message msg to the internal logger at ERROR log level. This method does the same as the core log_error() procedure.

class Nxlog.LogData

This class represents an event. It is instantiated by NXLog and passed to the method specified by the Call directive.

field_names()

This method returns an array with the names of all the fields currently in the event record.

get_field(name)

This method returns the value of the field name in the event.

set_field(name, value)

This method sets the value of field name to value.

Configuration

The om_ruby module accepts the following directives in addition to the common module directives. The RubyCode directive is required.

RubyCode

This mandatory directive specifies a file containing Ruby code. The om_ruby instance will call the method specified by the Call directive. The method must accept an Nxlog.LogData object as its only argument.


Call

This optional directive specifies the Ruby method to call. The default is write_data.

Examples

Example 1. Forwarding Events With om_ruby

This example uses a Ruby script to choose an output file according to the severity of the event. Normalized severity fields are added by most modules; see, for example, the xm_syslog $SeverityValue field.

See Using dynamic filenames for a way to implement this functionality natively.
nxlog.conf
<Output out>
    Module      om_ruby
    RubyCode    ./modules/output/ruby/proc2.rb
    Call        write_data
</Output>
proc2.rb
def write_data event
  if event.get_field('SeverityValue') >= 4
    Nxlog.log_debug('Writing out high severity event')
    File.open('tmp/high_severity', 'a') do |file|
      file.write("#{event.get_field('raw_event')}\n")
      file.flush
    end
  else
    Nxlog.log_debug('Writing out low severity event')
    File.open('tmp/low_severity', 'a') do |file|
      file.write("#{event.get_field('raw_event')}\n")
      file.flush
    end
  end
end