Sha256: 1f364657f67627b14a6852941d0aa3fa8e978b869ea491a6e3633529a658dde7

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

require 'fluent-logger'
require_relative 'log_repository'

module RTALogger
  class LogRepositoryFluentd < LogRepository
    def initialize(host = 'localhost', port = 24224, tls_options = nil)
      super()
      @host = host
      @port = port
      @fluent_logger = create_fluentd_logger(host, port, tls_options)
    end

    def load_config(config_json)
      super

      host = config_json['host'].to_s
      port = config_json['port'].to_s
      tls_options = config_json['tls_options']

      @fluent_logger = create_fluentd_logger(host, port, tls_options)
    end

    def to_builder
      json = super
      json.enable enable
      json.host @host
      json.port @port

      json
    end
    # register :fluentd

    protected

    def create_fluentd_logger(host, port, tls_options)
      unless tls_options
        fluent_logger = ::Fluent::Logger::FluentLogger.new(nil, :host => host, :port => port, :use_nonblock => true, :wait_writeable => false)
      else
        fluent_logger = ::Fluent::Logger::FluentLogger.new(nil, :host => host, :port => port, :tls_options => tls_options, :use_nonblock => true, :wait_writeable => false)
      end

      fluent_logger
    end

    def flush_and_clear
      semaphore.synchronize do
        @log_records.each do |log_record|
          fluent_tag = log_record.app_name + '.' + log_record.topic_title
          log_json_string = @formatter.format(log_record)
          log_json = JSON.parse(log_json_string)

          @fluent_logger.post(fluent_tag, log_json)
        end
      end
      super
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
RTALogger-2.0.2 lib/log_repository_fluentd.rb
RTALogger-2.0.1 lib/log_repository_fluentd.rb
RTALogger-2.0.0 lib/log_repository_fluentd.rb
RTALogger-1.1.1 lib/log_repository_fluentd.rb
RTALogger-1.1.0 lib/log_repository_fluentd.rb