lesser deps to Rails (or no deps)

This commit is contained in:
Denis Knauf 2012-10-18 19:34:04 +02:00
parent e9cba75802
commit 1108a9331c
2 changed files with 33 additions and 14 deletions

View file

@ -2,7 +2,10 @@ require 'json'
class Object class Object
def to_semlogger def to_semlogger
[self.class.name.to_sym, self.respond_to?( :serializable_hash) ? self.serializable_hash : self ] [
self.class.name.to_sym,
self.respond_to?( :serializable_hash) ? self.serializable_hash : self
]
end end
end end
@ -28,14 +31,25 @@ end
class Semlogger < ::Logger class Semlogger < ::Logger
class Base class Base
class <<self
attr_accessor :logger
end
attr_accessor :logger attr_accessor :logger
def add severity, progname = nil, &block def initialize
@logger.add severity, self, progname = nil, &block @logger = self.class.logger
end
def add severity, logger = nil, &block
(logger || @logger).add severity, self, &block
end end
::Semlogger::Severity.constants.each do |severity| ::Semlogger::Severity.constants.each do |severity|
module_eval "def #{severity.downcase}( *a, &e) add #{::Semlogger::Severity.const_get severity}, *a, &e end", __FILE__, __LINE__ module_eval <<-EOC, __FILE__, __LINE__+1
def #{severity.downcase} *a, &e
add #{::Semlogger::Severity.const_get severity}, *a, &e
end
EOC
end end
end end
@ -51,16 +65,10 @@ class Semlogger < ::Logger
attr_accessor :logdev, :level, :progname attr_accessor :logdev, :level, :progname
class <<self class <<self
attr_accessor :progname attr_accessor :progname, :logger
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self.logger } end
def custom( *a) CustomType.new *a end
end
def custom *a
r = CustomType.new *a
r.logger = self
r
end end
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self } end
@@progname = nil @@progname = nil
@ -70,6 +78,7 @@ class Semlogger < ::Logger
end end
@progname = a[0] || @@progname @progname = a[0] || @@progname
@level, @data, @tags, @logdev = DEBUG, {}, [], logdev @level, @data, @tags, @logdev = DEBUG, {}, [], logdev
self.class.logger = self if !self.class.logger && self.class.logger.is_a?( Semlogger::Default)
end end
def tagged *tags, &e def tagged *tags, &e
@ -145,3 +154,9 @@ require 'semlogger/multiplex'
require 'semlogger/rack' require 'semlogger/rack'
require 'semlogger/filter' require 'semlogger/filter'
require 'semlogger/writer' require 'semlogger/writer'
class Semlogger
class Default < Semlogger
end
self.logger ||= Default.new
end

View file

@ -3,7 +3,11 @@ end
class Semlogger::Writer < Semlogger::Output class Semlogger::Writer < Semlogger::Output
def initialize logdev = nil def initialize logdev = nil
@logdev = logdev || ::Semlogger::Rotate.new( ::Rails.root.join( 'log', ::Rails.env).to_s.gsub('%', '%%') + '.%Y-%m-%d.%$.log') @logdev = case logdev
when String then ::Semlogger::Rotate.new logdev
when nil then ::Semlogger::Rotate.new "log/#{File.basename $0}.%Y-%m-%d.%$.log"
else logdev
end
end end
def add severity, time, progname, data, tags, message def add severity, time, progname, data, tags, message