Sha256: bcb734d3a8d93d20330b6710dde02e0779c3d584303f51321be35db3bc8d98a4

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'lograge'
require 'logstash-logger'

module LogToolbox
  class LogrageConfig
    attr_accessor :config

    def self.configure
      Rails.application.configure do
        log_config = LogToolbox::LogrageConfig.new(config)
        log_config.configure_log_format
        if ENV['RAILS_ENV'] == 'development'
          log_config.configure_log_output_stdout
        else
          log_config.configure_log_output_logstash
        end
      end
    end

    def initialize(config)
      @config = config
    end

    def configure_log_output_logstash
      config.logger = LogStashLogger.new(type: :udp,
                                         host: ENV['LOGSTASH_URL'],
                                         port: ENV['LOGSTASH_PORT'])
    end

    def configure_log_output_stdout
      return unless ENV['RAILS_LOG_TO_STDOUT'].present?

      logger           = ActiveSupport::Logger.new(STDOUT)
      logger.formatter = config.log_formatter
      config.logger    = ActiveSupport::TaggedLogging.new(logger)
    end

    # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
    def configure_log_format
      config.lograge.enabled = true
      config.lograge.base_controller_class = 'ActionController::API'
      config.lograge.formatter = Lograge::Formatters::Json.new

      config.lograge.custom_options = lambda do |event|
        {
          activityID: event.payload[:activityID],
          event_at: event.payload[:event_at],
          level: 'INFO',
          service_name: event.payload[:service_name],
          version: event.payload[:version],
          origin_ipv4: event.payload[:origin_ipv4],
          destination_ipv4: event.payload[:destination_ipv4],
          username: event.payload[:username],
          exception_stack: event.payload[:exception_stack],
          response: event.payload[:response]
        }
      end
    end
    # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log_toolbox-1.3.2 lib/log_toolbox/lograge_config.rb
log_toolbox-1.3.1 lib/log_toolbox/lograge_config.rb