2012-05-14 11:52:45 +02:00
|
|
|
require 'json'
|
|
|
|
|
2012-05-21 13:54:21 +02:00
|
|
|
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
|
|
|
|
|
2012-05-14 12:02:50 +02:00
|
|
|
class Semlogger < ::Logger
|
2012-10-17 23:14:09 +02:00
|
|
|
class Base
|
2012-10-15 16:08:35 +02:00
|
|
|
attr_accessor :logger
|
|
|
|
|
2012-10-17 23:14:09 +02:00
|
|
|
def add severity, progname = nil, &block
|
|
|
|
@logger.add severity, self, progname = nil, &block
|
|
|
|
end
|
|
|
|
|
|
|
|
::Semlogger::Severity.constants.each do |severity|
|
|
|
|
module_eval "def #{severity.downcase}( *a, &e) add #{::Semlogger::Severity.const_get severity}, *a, &e end", __FILE__, __LINE__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class CustomType < Base
|
2012-05-21 13:54:21 +02:00
|
|
|
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
|
|
|
|
|
2012-10-15 16:08:35 +02:00
|
|
|
def custom( *a) CustomType.new *a end
|
2012-05-21 13:54:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom *a
|
2012-10-15 16:08:35 +02:00
|
|
|
r = CustomType.new *a
|
|
|
|
r.logger = self
|
|
|
|
r
|
2012-05-21 13:54:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@@progname = nil
|
|
|
|
|
2012-05-14 11:52:45 +02:00
|
|
|
def initialize logdev = nil, *a, &e
|
|
|
|
case logdev
|
2012-05-14 12:02:50 +02:00
|
|
|
when String, nil then logdev = ::Semlogger::Writer.new logdev
|
2012-05-14 11:52:45 +02:00
|
|
|
end
|
2012-05-21 13:54:21 +02:00
|
|
|
@progname = a[0] || @@progname
|
2012-05-14 11:52:45 +02:00
|
|
|
@level, @data, @tags, @logdev = DEBUG, {}, [], logdev
|
|
|
|
end
|
|
|
|
|
2012-10-15 16:08:35 +02:00
|
|
|
def tagged *tags, &e
|
2012-05-14 11:52:45 +02:00
|
|
|
@tags += tags
|
|
|
|
tags = tags.size
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
tags.times { @tags.pop }
|
|
|
|
end
|
|
|
|
|
|
|
|
def add severity, message = nil, progname = nil, &block
|
|
|
|
severity ||= UNKNOWN
|
|
|
|
if @logdev.nil? or severity < @level
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
progname ||= @progname
|
|
|
|
if message.nil?
|
|
|
|
if block_given?
|
|
|
|
message = yield
|
|
|
|
else
|
|
|
|
message = progname
|
|
|
|
progname = @progname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@logdev.add severity, Time.new, progname, format_data( @data), format_tags( @tags), format_msg( message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_msg msg
|
2012-05-21 13:54:21 +02:00
|
|
|
msg = msg.to_semlogger
|
2012-05-14 11:52:45 +02:00
|
|
|
case msg
|
2012-05-21 13:54:21 +02:00
|
|
|
when Array then msg
|
|
|
|
else [msg.class.name.to_sym, msg.inspect]
|
2012-05-14 11:52:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_obj obj
|
|
|
|
case obj
|
|
|
|
when Proc then obj.call
|
|
|
|
else obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_tags tags
|
|
|
|
tags = tags ? tags.dup : []
|
|
|
|
tags.map &method( :format_obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_data data
|
|
|
|
data = data ? data.dup : {}
|
|
|
|
data.each {|k,v| data[k] = format_obj v }
|
|
|
|
end
|
|
|
|
|
|
|
|
def data data, &e
|
2012-05-21 13:54:21 +02:00
|
|
|
return @data unless e
|
2012-05-14 11:52:45 +02:00
|
|
|
@data.update data
|
|
|
|
keys = data.keys
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
keys.each &data.method( :delete)
|
|
|
|
end
|
|
|
|
|
|
|
|
def caller &e
|
|
|
|
data caller: Kernel.method(:caller), &e
|
|
|
|
end
|
|
|
|
|
|
|
|
def thread &e
|
|
|
|
data thread: Proc.new { Thread.current.object_id }, &e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-14 12:02:50 +02:00
|
|
|
require 'semlogger/rotate'
|
|
|
|
require 'semlogger/multiplex'
|
|
|
|
require 'semlogger/rack'
|
|
|
|
require 'semlogger/filter'
|
|
|
|
require 'semlogger/writer'
|