Class: Log4r::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/log4r/railtie.rb

Class Method Summary collapse

Class Method Details

.controller_log(payload) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/log4r/railtie.rb', line 155

def controller_log(payload)
  logger = Rails.logger
  params_logger = Log4r::Logger["rails::params"] || Log4r::Logger.root
  
  duration = payload[:duration]
  unless payload[:exception].nil?
    logger.warn {
      db = (payload[:db_runtime] * 100).round/100.0 rescue "-"
      view = (payload[:view_runtime] * 100).round/100.0 rescue "-"
      "#{payload[:method]} #{payload[:path]} (TIMING[ms]: sum:#{duration} db:#{db} view:#{view}) EXCEPTION: #{payload[:exception]}"
    }
    params_logger.info { "request params: " + payload[:params].to_json }
    return
  end
  if duration >= Log4r::Railtie.options[:action_mht]
    logger.warn {
      db = (payload[:db_runtime] * 100).round/100.0 rescue "-"
      view = (payload[:view_runtime] * 100).round/100.0 rescue "-"
      "#{payload[:method]} #{payload[:path]} (TIMING[ms]: sum:#{duration} db:#{db} view:#{view})"
    }
  else
    logger.info {
      db = (payload[:db_runtime] * 100).round/100.0 rescue "-"
      view = (payload[:view_runtime] * 100).round/100.0 rescue "-"
      "#{payload[:method]} #{payload[:path]} (TIMING[ms]: sum:#{duration} db:#{db} view:#{view})"
    }
  end
  params_logger.info { "request params: " + payload[:params].to_json }
end

.post_initObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/log4r/railtie.rb', line 136

def post_init
  setup_logger Rails, "rails"
  # disable rack development output, e.g. Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700
  if Rails.const_defined?(:Rack) && Rails::Rack.const_defined?(:Logger)
    setup_logger Rails::Rack::Logger, "root"
  end
  # override DebugExceptions output
  ActionDispatch::DebugExceptions.module_eval %-
    def log_error(env, wrapper)
      logger = Rails.logger
      exception = wrapper.exception
      # trace = wrapper.application_trace
      # trace = wrapper.framework_trace if trace.empty?
      logger.info "ActionDispatch Exception: \#{exception.class} (\#{exception.message})"
    end
    private :log_error
  -
end

.setup_logger(clazz, logger_name) ⇒ Object

convenient static method to setup logger for class and descendants.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/log4r/railtie.rb', line 186

def setup_logger(clazz, logger_name)
  clazz.module_eval %(
    class << self
      custom_logger = nil
      define_method :logger do
        custom_logger || Log4r::Logger['#{logger_name}'] || Log4r::Logger.root
      end
      define_method :logger= do |l|
        (l || custom_logger).debug "Log4rails is preventing set of logger. Use #custom_logger= if you really want it set."
      end
      define_method :custom_logger= do |l|
        custom_logger = l
      end
    end
    
    def logger
      #{clazz.name}.logger
    end
  )
end