# 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