v0.0.3: customtypes
This commit is contained in:
parent
f06b2be077
commit
868507672a
|
@ -1,10 +1,62 @@
|
|||
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 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
|
||||
case logdev
|
||||
when String, nil then logdev = ::Semlogger::Writer.new logdev
|
||||
end
|
||||
@progname = a[0] || @@progname
|
||||
@level, @data, @tags, @logdev = DEBUG, {}, [], logdev
|
||||
end
|
||||
|
||||
|
@ -34,11 +86,10 @@ class Semlogger < ::Logger
|
|||
end
|
||||
|
||||
def format_msg msg
|
||||
msg = msg.to_semlogger
|
||||
case msg
|
||||
when Numeric, true, false, nil then [:const, msg]
|
||||
when String then [:str, msg]
|
||||
when Exception then [:exception, msg.class.name, msg.message.to_s, msg.backtrace]
|
||||
else [:obj, msg]
|
||||
when Array then msg
|
||||
else [msg.class.name.to_sym, msg.inspect]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -60,6 +111,7 @@ class Semlogger < ::Logger
|
|||
end
|
||||
|
||||
def data data, &e
|
||||
return @data unless e
|
||||
@data.update data
|
||||
keys = data.keys
|
||||
yield
|
||||
|
|
|
@ -7,7 +7,7 @@ class Semlogger::Rack < Rails::Rack::Logger
|
|||
def call_app env
|
||||
request = ActionDispatch::Request.new env
|
||||
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
|
||||
ensure
|
||||
ActiveSupport::LogSubscriber.flush_all!
|
||||
|
|
|
@ -24,8 +24,7 @@ class Semlogger::Printer < Semlogger::Output
|
|||
r = "Exception: #{message[2]} (#{message[1]}"
|
||||
r << "\n\t" << message[3].join( "\n\t") if message[3]
|
||||
r
|
||||
when :str, :const then message[1]
|
||||
when :obj then message[1].inspect
|
||||
when :String, :const then message[1]
|
||||
else message.inspect
|
||||
end
|
||||
reqid = data[:reqid]
|
||||
|
|
Loading…
Reference in a new issue