Sha256: 4bfe75876a4e5e7075fbbd2d1ee61254593b896a293bd8e84aca997b1e4da443

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module LogToolbox
  module LogHelper
    def log_critical(error, additional = {})
      log_pattern(
        'fatal',
        additional.merge(
          message: error.try(:message),
          backtrace: 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

    def log_debug(message, additional = {})
      log_pattern('debug', additional.merge(message: message))
    end

    private

    # rubocop:disable Metrics/MethodLength
    def default_log(level)
      level = 'CRITICAL' if level == 'FATAL'
      {
        activityID: request.headers['activityID'],
        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/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

9 entries across 9 versions & 1 rubygems

Version Path
log_toolbox-1.3.11 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.10 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.9 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.8 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.7 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.6 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.5 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.4 lib/log_toolbox/log_helper.rb
log_toolbox-1.3.3 lib/log_toolbox/log_helper.rb