Sha256: 4e7b3c3e743b34f52301e652bb3c90f624d9624763e60e6d1e04a5b8cbcce780

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module LogToolbox
  module LogHelper
    def log_critical(error, additional = {})
      log_pattern(
        'fatal',
        additional.merge(
          message: error.try(:message),
          exception_stack: error.try(:backtrace),
          status: 500
        )
      )
    end

    def log_error(message, additional = {})
      log_pattern('error', additional.merge(message: message))
    end

    def log_warn(message, additional = {})
      log_pattern('warn', additional.merge(message: message))
    end

    def log_info(message, additional = {})
      log_pattern('info', additional.merge(message: message))
    end

    private

    # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
    def default_log(level)
      level = 'CRITICAL' if level == 'FATAL'
      {
        id: request.headers['correlation_id'],
        event_at: I18n.l(Time.now, format: :timestamp),
        level: level,
        service_name: LogUtil.system_service_name,
        version: LogUtil.system_version,
        origin_ipv4: request.env['REMOTE_ADDR'],
        destination_ipv4: request.env['SERVER_NAME'],
        username: request.headers['Authorization']
      }
    end
    # rubocop:enable Metrics/AbcSize,Metrics/MethodLength

    def log_pattern(level, log_hash)
      log = default_log(level.upcase).merge(log_hash)
      logger.send(level, normalize_hash(log))
    end

    def normalize_hash(hash)
      hash.stringify_keys.to_json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
log_toolbox-1.0.0 lib/log_toolbox/log_helper.rb