Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rails_logger.rb « gitlab « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a1695ce56e8c32bdffc73a103a3fdb5099e1059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

require_relative '../../code_reuse_helpers'

module RuboCop
  module Cop
    module Gitlab
      class RailsLogger < ::RuboCop::Cop::Cop
        include CodeReuseHelpers

        # This cop checks for the Rails.logger log methods in the codebase
        #
        # @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 })
        #
        #   # OK
        #   Rails.logger.level
        MSG = 'Use a structured JSON logger instead of `Rails.logger`. ' \
          'https://docs.gitlab.com/ee/development/logging.html'

        # 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

        def_node_matcher :rails_logger_log?, <<~PATTERN
          (send
            (send (const nil? :Rails) :logger)
            {#{LOG_METHODS_PATTERN}} ...
          )
        PATTERN

        def on_send(node)
          return unless rails_logger_log?(node)

          add_offense(node, location: :expression)
        end
      end
    end
  end
end