v0.0.3: customtypes

This commit is contained in:
Denis Knauf 2012-05-21 13:54:21 +02:00
parent f06b2be077
commit 868507672a
4 changed files with 59 additions and 8 deletions

View file

@ -1 +1 @@
0.0.2 0.0.3

View file

@ -1,10 +1,62 @@
require 'json' require 'json'
class Object
def to_semlogger
[self.class.name.to_sym, self.respond_to?( :serializable_hash) ? self.serializable_hash : self ]
end
end
class Exception
def to_semlogger
[:exception] + super
end
end
class String
def to_semlogger
[:String, self]
end
end
%w[Numeric FalseClass TrueClass NilClass].each do |cl|
Object.const_get( cl).class_eval do
def to_semlogger
[:const, self]
end
end
end
class Semlogger < ::Logger class Semlogger < ::Logger
class CustomType
def initialize name, *obj
@name, @obj = name.to_s.to_sym, obj
end
def to_semlogger
[@name] + @obj
end
end
attr_accessor :logdev, :level, :progname
class <<self
attr_accessor :progname
def custom *a
CustomType.new *a
end
end
def custom *a
CustomType.new *a
end
@@progname = nil
def initialize logdev = nil, *a, &e def initialize logdev = nil, *a, &e
case logdev case logdev
when String, nil then logdev = ::Semlogger::Writer.new logdev when String, nil then logdev = ::Semlogger::Writer.new logdev
end end
@progname = a[0] || @@progname
@level, @data, @tags, @logdev = DEBUG, {}, [], logdev @level, @data, @tags, @logdev = DEBUG, {}, [], logdev
end end
@ -34,11 +86,10 @@ class Semlogger < ::Logger
end end
def format_msg msg def format_msg msg
msg = msg.to_semlogger
case msg case msg
when Numeric, true, false, nil then [:const, msg] when Array then msg
when String then [:str, msg] else [msg.class.name.to_sym, msg.inspect]
when Exception then [:exception, msg.class.name, msg.message.to_s, msg.backtrace]
else [:obj, msg]
end end
end end
@ -60,6 +111,7 @@ class Semlogger < ::Logger
end end
def data data, &e def data data, &e
return @data unless e
@data.update data @data.update data
keys = data.keys keys = data.keys
yield yield

View file

@ -7,7 +7,7 @@ class Semlogger::Rack < Rails::Rack::Logger
def call_app env def call_app env
request = ActionDispatch::Request.new env request = ActionDispatch::Request.new env
path = request.filtered_path path = request.filtered_path
Rails.logger.info [:connection, request.ip, Thread.current.object_id, request.request_method, path] Rails.logger.info Semlogger.custom :connection, request.ip, Thread.current.object_id, request.request_method, path
@app.call env @app.call env
ensure ensure
ActiveSupport::LogSubscriber.flush_all! ActiveSupport::LogSubscriber.flush_all!

View file

@ -24,8 +24,7 @@ class Semlogger::Printer < Semlogger::Output
r = "Exception: #{message[2]} (#{message[1]}" r = "Exception: #{message[2]} (#{message[1]}"
r << "\n\t" << message[3].join( "\n\t") if message[3] r << "\n\t" << message[3].join( "\n\t") if message[3]
r r
when :str, :const then message[1] when :String, :const then message[1]
when :obj then message[1].inspect
else message.inspect else message.inspect
end end
reqid = data[:reqid] reqid = data[:reqid]