#new_rails_logger: custom logdev-template. Semlogger::Rack: #call will never super anymore.

This commit is contained in:
Denis Knauf 2012-11-21 16:11:34 +01:00
parent 25ff6d52d8
commit 6d2157caff
3 changed files with 30 additions and 13 deletions

View file

@ -1 +1 @@
0.0.9 0.0.10

View file

@ -72,9 +72,10 @@ class Semlogger < ::Logger
class <<self class <<self
attr_accessor :progname, :logger attr_accessor :progname, :logger
def new_rails_logger config def new_rails_logger config, logdev = nil
require 'semlogger/rack' require 'semlogger/rack'
logdev = ::Rails.root.join( 'log', "#{::Rails.env.to_s.gsub('%', '%%')}.%Y-%m-%d.%$.log").to_s logdev ||= ::Rails.root.join 'log', "#{::Rails.env.to_s.gsub('%', '%%')}.%Y-%m-%d.%$.log"
logdev = logdev.to_s
logger = nil logger = nil
if Rails.env.production? if Rails.env.production?
logger = new logdev logger = new logdev
@ -106,11 +107,12 @@ class Semlogger < ::Logger
end end
def tagged *tags, &e def tagged *tags, &e
@tags += tags @tags += tags.flatten.compact
tags = tags.size tags = tags.size
yield yield
ensure ensure
tags.times { @tags.pop } #tags.times { @tags.pop }
@tags.slice! -tags .. -1
end end
def add severity, message = nil, progname = nil, &block def add severity, message = nil, progname = nil, &block

View file

@ -1,28 +1,43 @@
class Semlogger::Rack < Rails::Rack::Logger class Semlogger::Rack < Rails::Rack::Logger
def initialize app, tags = nil, data = nil def initialize app, tags = nil, data = nil
super app, tags super app, tags
@data = data @data = data || {}
end end
def call_app request, env def call_app request, env
path = request.filtered_path path = request.filtered_path
Semlogger.custom( :connection, request.ip, Thread.current.object_id, request.request_method, path).info Rails.logger.custom( :connection, request.ip, Thread.current.object_id, request.request_method, path).info
@app.call env @app.call env
ensure ensure
ActiveSupport::LogSubscriber.flush_all! ActiveSupport::LogSubscriber.flush_all!
end end
def call env def call env
if @data request = ActionDispatch::Request.new env
Rails.logger.data( compute_data( env)) { super env } compute_tagged_ request do
else compute_data_ request do
super env call_app request, env
end
end end
end end
def compute_data env def compute_tagged_ request
request = ActionDispatch::Request.new env if Rails.logger.respond_to? :tagged
Rails.logger.tagged( compute_tags( request)) { yield }
else
yield
end
end
def compute_data_ request
if Rails.logger.respond_to? :data
Rails.logger.data( compute_data( request)) { yield }
else
yield
end
end
def compute_data request
data = @data.dup data = @data.dup
data.each do |k, v| data.each do |k, v|
case v case v