Module: MrLogaLoga::Extensions::RailsExtension::LoggerPatch

Defined in:
lib/mr_loga_loga/extensions/rails.rb

Overview

This patches ActiveSupport::Logger to allow properly formatting keyword arguments

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

rubocop:disable all



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mr_loga_loga/extensions/rails.rb', line 55

def included base
  base.instance_eval do
    def broadcast(logger)
      Module.new do
        # We need to patch this method as otherwise calling add with keyword arguments results in argument errors.
        # Rails calls `broadcast` for the logger configured in `Rails.logger`, which means that any invocations of
        # Rails.logger.add result in running through this method.
        #
        # TODO: Remove once patched in Rails
        define_method(:add) do |*args, **kwargs, &block|
          logger.add(*args, **kwargs, &block)
          super(*args, **kwargs, &block)
        end

        define_method(:<<) do |x|
          logger << x
          super(x)
        end

        define_method(:close) do
          logger.close
          super()
        end

        define_method(:progname=) do |name|
          logger.progname = name
          super(name)
        end

        define_method(:formatter=) do |formatter|
          logger.formatter = formatter
          super(formatter)
        end

        define_method(:level=) do |level|
          logger.level = level
          super(level)
        end

        define_method(:local_level=) do |level|
          logger.local_level = level if logger.respond_to?(:local_level=)
          super(level) if respond_to?(:local_level=)
        end

        define_method(:silence) do |level = Logger::ERROR, &block|
          if logger.respond_to?(:silence)
            logger.silence(level) do
              if defined?(super)
                super(level, &block)
              else
                block.call(self)
              end
            end
          else
            if defined?(super)
              super(level, &block)
            else
              block.call(self)
            end
          end
        end
      end
    end
  end
end