class Fluent::Plugin::SyslogTlsOutput

Constants

DEFAULT_FORMAT_TYPE
SYSLOG_HEADERS

Allow to map keys from record to syslog message headers

Attributes

formatter[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_syslog_tls.rb, line 55
def initialize
  super
  @loggers = {}
end

Public Instance Methods

configure(conf) click to toggle source

This method is called before starting.

Calls superclass method
# File lib/fluent/plugin/out_syslog_tls.rb, line 66
def configure(conf)
  if conf['output_type'] && !conf['format']
    conf['format'] = conf['output_type']
  end
  compat_parameters_convert(conf, :inject, :formatter)

  super
  @host = conf['host']
  @port = conf['port']
  @token = conf['token']
  @hostname = conf['hostname'] || Socket.gethostname.split('.').first

  # Determine mapping of record keys to syslog keys
  @mappings = {}
  SYSLOG_HEADERS.each do |key_name|
    conf_key = "#{key_name}_key"
    @mappings[key_name] = conf[conf_key] if conf.key?(conf_key)
  end

  @formatter = formatter_create(conf: conf.elements('format').first, default_type: DEFAULT_FORMAT_TYPE)
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_syslog_tls.rb, line 117
def format(tag, time, record)
  record = inject_values_to_record(tag, time, record)
  @formatter.format(tag, time, record)
end
logger(tag) click to toggle source

Get logger for given tag

# File lib/fluent/plugin/out_syslog_tls.rb, line 89
def logger(tag)
  # Try to reuse existing logger
  @loggers[tag] ||= new_logger(tag)

  # Create new logger if old one is closed
  if @loggers[tag].closed?
    @loggers[tag] = new_logger(tag)
  end

  @loggers[tag]
end
new_logger(tag) click to toggle source
# File lib/fluent/plugin/out_syslog_tls.rb, line 101
def new_logger(tag)
  transport = ::SyslogTls::SSLTransport.new(host, port,
    idle_timeout: idle_timeout,
    ca_cert: ca_cert,
    client_cert: client_cert,
    client_key: client_key,
    verify_cert_name: verify_cert_name,
    max_retries: 30,
  )
  logger = ::SyslogTls::Logger.new(transport, token)
  logger.facility(facility)
  logger.hostname(hostname)
  logger.app_name(tag)
  logger
end
process(tag, es) click to toggle source
# File lib/fluent/plugin/out_syslog_tls.rb, line 122
def process(tag, es)
  es.each do |time, record|
    record.each_pair do |_, v|
      v.force_encoding('utf-8') if v.is_a?(String)
    end

    # Check if severity has been provided in record otherwise use INFO
    # by default.
    severity = if @mappings.key?(:severity)
                 record[@mappings[:severity]] || 'INFO'
               else
                 'INFO'
               end

    # Send message to Syslog
    begin
      logger(tag).log(severity, format(tag, time, record), time: Time.at(time)) do |header|
        # Map syslog headers from record
        @mappings.each do |name, record_key|
          header.send("#{name}=", record[record_key]) unless record[record_key].nil?
        end
      end
    rescue => e
      log.error e.to_s
    end
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_syslog_tls.rb, line 60
def shutdown
  @loggers.values.each(&:close)
  super
end