2019-07-10 15:26:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../code_reuse_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Gitlab
|
|
|
|
class RailsLogger < ::RuboCop::Cop::Cop
|
|
|
|
include CodeReuseHelpers
|
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
# This cop checks for the Rails.logger log methods in the codebase
|
2019-07-10 15:26:47 -04:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# Rails.logger.error("Project #{project.full_path} could not be saved")
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# Gitlab::AppLogger.error("Project %{project_path} could not be saved" % { project_path: project.full_path })
|
2020-09-11 08:08:50 -04:00
|
|
|
#
|
|
|
|
# # OK
|
|
|
|
# Rails.logger.level
|
2019-07-10 15:26:47 -04:00
|
|
|
MSG = 'Use a structured JSON logger instead of `Rails.logger`. ' \
|
2021-03-23 11:09:28 -04:00
|
|
|
'https://docs.gitlab.com/ee/development/logging.html'
|
2019-07-10 15:26:47 -04:00
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
# See supported log methods:
|
|
|
|
# https://ruby-doc.org/stdlib-2.6.6/libdoc/logger/rdoc/Logger.html
|
|
|
|
LOG_METHODS = %i[debug error fatal info warn].freeze
|
|
|
|
LOG_METHODS_PATTERN = LOG_METHODS.map(&:inspect).join(' ').freeze
|
2019-07-10 15:26:47 -04:00
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
def_node_matcher :rails_logger_log?, <<~PATTERN
|
|
|
|
(send
|
|
|
|
(send (const nil? :Rails) :logger)
|
|
|
|
{#{LOG_METHODS_PATTERN}} ...
|
|
|
|
)
|
|
|
|
PATTERN
|
2019-07-10 15:26:47 -04:00
|
|
|
|
|
|
|
def on_send(node)
|
2020-09-11 08:08:50 -04:00
|
|
|
return unless rails_logger_log?(node)
|
2019-07-10 15:26:47 -04:00
|
|
|
|
2022-09-14 11:12:56 -04:00
|
|
|
add_offense(node)
|
2019-07-10 15:26:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|