lib/td/logger/agent/access_log.rb in td-logger-0.2.5 vs lib/td/logger/agent/access_log.rb in td-logger-0.2.6

- old
+ new

@@ -20,11 +20,19 @@ ACCESS_LOG_PRESET_PARAM_KEYS = { :controller => :controller, :action => :action, } - def self.enable_access_log(tag) + def self.enable_access_log(config) + tag = config.access_log_table + + if config.rails_config.respond_to?(:filter_parameters) + filter_parameters = config.rails_config.filter_parameters + else + filter_parameters = [] + end + Middleware.before do |env| record = {} Thread.current['td.access_log'] = record env['td.access_log'] = record env['td.access_time'] = Time.now @@ -37,32 +45,81 @@ end # ignore OPTIONS request if req.request_method != "OPTIONS" record = env['td.access_log'] || {} - access_time = env['td.access_time'] - # add 'elapsed' column - if access_time - elapsed = Time.now - access_time - record[:elapsed] = elapsed - # set 'time' column to access time + # 'elapsed' column + if access_time = env['td.access_time'] + unless record.has_key?(:elapsed) + record[:elapsed] = Time.now - access_time + end + + # always overwrite 'time' column by access time record[:time] = access_time end - record[:method] ||= req.request_method - record[:ip] ||= (env['action_dispatch.remote_ip'] || req.ip).to_s - record[:uri] ||= env['REQUEST_URI'].to_s if env['REQUEST_URI'] - record[:referer] ||= env['HTTP_REFERER'].to_s if env['HTTP_REFERER'] - record[:ua] ||= env['HTTP_USER_AGENT'].to_s if env['HTTP_USER_AGENT'] - - m = env[ACCESS_LOG_PARAM_ENV] - ACCESS_LOG_PRESET_PARAM_KEYS.each_pair {|key,val| - record[key] ||= m[val] if m[val] + # merge params + req.params.each_pair {|key,val| + key = key.to_sym + unless record.has_key?(key) || filter_parameters.include?(key) + record[key] = val + end } - # result code - record[:status] ||= result[0].to_i + # 'method' column + if !record.has_key?(:method) + record[:method] = req.request_method + end + + # 'ip' column + unless record.has_key?(:ip) + record[:ip] = (env['action_dispatch.remote_ip'] || req.ip).to_s + end + + # 'path' column + # requested path before '?' + unless record.has_key?(:path) + if path = env['REQUEST_URI'] + if m = /(?:\w{1,10}\:\/\/[^\/]+)?([^\?]*)/.match(path) + record[:path] = m[1] + end + end + end + + # 'host' column + # Rack#host_with_port consideres HTTP_X_FORWARDED_HOST + unless record.has_key?(:host) + record[:host] = req.host_with_port + end + + # 'referer' column + unless record.has_key?(:referer) + if referer = env['HTTP_REFERER'] + record[:referer] = referer.to_s + end + end + + # 'agent' column + unless record.has_key?(:agent) + if agent = env['HTTP_USER_AGENT'] + record[:agent] = agent + end + end + + # 'status' column + unless record.has_key?(:status) + record[:status] = result[0].to_i + end + + # 'controller' and 'action' columns + if m = env[ACCESS_LOG_PARAM_ENV] + ACCESS_LOG_PRESET_PARAM_KEYS.each_pair {|key,val| + unless record.has_key?(key) + record[key] = m[val] if m[val] + end + } + end TreasureData.log(tag, record) end end end