Class: Fluent::SumologicCloudSyslogOutput

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin, Mixin::ConfigPlaceholders, Mixin::PlainTextFormatter
Defined in:
lib/fluent/plugin/out_sumologic_cloud_syslog.rb

Constant Summary collapse

SYSLOG_HEADERS =

Allow to map keys from record to syslog message headers

[
  :severity, :facility, :hostname, :app_name, :procid, :msgid
]

Instance Method Summary collapse

Constructor Details

#initializeSumologicCloudSyslogOutput

Returns a new instance of SumologicCloudSyslogOutput.



44
45
46
47
48
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 44

def initialize
  super
  require 'sumologic_cloud_syslog/logger'
  @loggers = {}
end

Instance Method Details

#configure(conf) ⇒ Object

This method is called before starting.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 56

def configure(conf)
  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
end

#emit(tag, es, chain) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 93

def emit(tag, es, chain)
  chain.next
  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 Sumologic
    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

#logger(tag) ⇒ Object

Get logger for given tag



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 72

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) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 84

def new_logger(tag)
  transport = ::SumologicCloudSyslog::SSLTransport.new(host, port, cert: cert, key: key, max_retries: 3)
  logger = ::SumologicCloudSyslog::Logger.new(transport, token)
  logger.facility(facility)
  logger.hostname(hostname)
  logger.app_name(tag)
  logger
end

#shutdownObject



50
51
52
53
# File 'lib/fluent/plugin/out_sumologic_cloud_syslog.rb', line 50

def shutdown
  super
  @loggers.values.each(&:close)
end