2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../code_reuse_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Gitlab
|
|
|
|
class RailsLogger < ::RuboCop::Cop::Cop
|
|
|
|
include CodeReuseHelpers
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# This cop checks for the Rails.logger log methods in the codebase
|
2019-09-30 21:07:59 +05:30
|
|
|
#
|
|
|
|
# @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-11-24 15:15:51 +05:30
|
|
|
#
|
|
|
|
# # OK
|
|
|
|
# Rails.logger.level
|
2019-09-30 21:07:59 +05:30
|
|
|
MSG = 'Use a structured JSON logger instead of `Rails.logger`. ' \
|
|
|
|
'https://docs.gitlab.com/ee/development/logging.html'.freeze
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# 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-09-30 21:07:59 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def_node_matcher :rails_logger_log?, <<~PATTERN
|
|
|
|
(send
|
|
|
|
(send (const nil? :Rails) :logger)
|
|
|
|
{#{LOG_METHODS_PATTERN}} ...
|
|
|
|
)
|
|
|
|
PATTERN
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
def on_send(node)
|
2020-11-24 15:15:51 +05:30
|
|
|
return unless rails_logger_log?(node)
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
add_offense(node, location: :expression)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|