Compare commits
26 commits
Author | SHA1 | Date | |
---|---|---|---|
d1694702b8 | |||
6d2157caff | |||
25ff6d52d8 | |||
595f4ac6f1 | |||
8edb8ccdea | |||
26853e31e2 | |||
fd8860766b | |||
3874aa0e43 | |||
3b20edf5cf | |||
e04b61dd4a | |||
eba19017af | |||
1108a9331c | |||
e9cba75802 | |||
57896e3145 | |||
4e803a26e7 | |||
95417dec97 | |||
eee7d885cc | |||
6c9b038de0 | |||
d4dd5dfd0e | |||
c44010dd64 | |||
a7745f28a1 | |||
742bb3b01e | |||
59957f03b7 | |||
0f6eee9e91 | |||
868507672a | |||
f06b2be077 |
2
Rakefile
2
Rakefile
|
@ -43,7 +43,7 @@ end
|
|||
|
||||
#task :default => :test
|
||||
|
||||
require 'rake/rdoctask'
|
||||
require 'rdoc/task'
|
||||
Rake::RDocTask.new do |rdoc|
|
||||
if File.exist? 'VERSION'
|
||||
version = File.read 'VERSION'
|
||||
|
|
124
lib/semlogger.rb
124
lib/semlogger.rb
|
@ -1,26 +1,123 @@
|
|||
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 Base
|
||||
class <<self
|
||||
attr_accessor :logger
|
||||
end
|
||||
attr_accessor :logger
|
||||
|
||||
def initialize
|
||||
@logger = self.class.logger
|
||||
end
|
||||
|
||||
def add severity, logger = nil, &block
|
||||
(logger || @logger).add severity, self, &block
|
||||
end
|
||||
|
||||
::Semlogger::Severity.constants.each do |severity|
|
||||
module_eval <<-EOC, __FILE__, __LINE__+1
|
||||
def #{severity.downcase} *a, &e
|
||||
add #{::Semlogger::Severity.const_get severity}, *a, &e
|
||||
end
|
||||
EOC
|
||||
end
|
||||
end
|
||||
|
||||
class CustomType < Base
|
||||
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
|
||||
|
||||
# some libs use #log_level
|
||||
def log_level=( level) @level = level end
|
||||
def log_level() @level end
|
||||
|
||||
class <<self
|
||||
attr_accessor :progname, :logger
|
||||
|
||||
def new_rails_logger config, logdev = nil
|
||||
require 'semlogger/rack'
|
||||
logdev ||= ::Rails.root.join 'log', "#{::Rails.env.to_s.gsub('%', '%%')}.%Y-%m-%d.%$.log"
|
||||
logdev = logdev.to_s
|
||||
logger = nil
|
||||
if Rails.env.production?
|
||||
logger = new logdev
|
||||
logger.level = Semlogger::INFO
|
||||
elsif Rails.env.development?
|
||||
logger = new Semlogger::Multiplex.new( Semlogger::FInfo.new( Semlogger::Printer.new), Semlogger::Writer.new( logdev))
|
||||
logger.level = Semlogger::DEBUG
|
||||
else
|
||||
logger = new logdev
|
||||
logger.level = Semlogger::DEBUG
|
||||
end
|
||||
config.middleware.swap Rails::Rack::Logger, Semlogger::Rack, [], {reqid: :uuid}
|
||||
config.logger = logger
|
||||
end
|
||||
|
||||
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self.logger } end
|
||||
end
|
||||
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self } 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
|
||||
self.class.logger = self if !self.class.logger && self.class.logger.is_a?( Semlogger::Default)
|
||||
end
|
||||
|
||||
def tagged tags, &e
|
||||
@tags += tags
|
||||
def tagged *tags, &e
|
||||
@tags += tags.flatten.compact
|
||||
tags = tags.size
|
||||
yield
|
||||
ensure
|
||||
tags.times { @tags.pop }
|
||||
#tags.times { @tags.pop }
|
||||
@tags.slice! -tags .. -1
|
||||
end
|
||||
|
||||
def add severity, message = nil, progname = nil, &block
|
||||
severity ||= UNKNOWN
|
||||
if @logdev.nil? or severity < @level
|
||||
return true
|
||||
end
|
||||
return true if @logdev.nil? or severity < @level
|
||||
progname ||= @progname
|
||||
if message.nil?
|
||||
if block_given?
|
||||
|
@ -34,11 +131,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 +156,7 @@ class Semlogger < ::Logger
|
|||
end
|
||||
|
||||
def data data, &e
|
||||
return @data unless e
|
||||
@data.update data
|
||||
keys = data.keys
|
||||
yield
|
||||
|
@ -78,6 +175,11 @@ end
|
|||
|
||||
require 'semlogger/rotate'
|
||||
require 'semlogger/multiplex'
|
||||
require 'semlogger/rack'
|
||||
require 'semlogger/filter'
|
||||
require 'semlogger/writer'
|
||||
|
||||
class Semlogger
|
||||
class Default < Semlogger
|
||||
end
|
||||
self.logger ||= Default.new
|
||||
end
|
||||
|
|
|
@ -1,29 +1,43 @@
|
|||
class Semlogger::Rack < Rails::Rack::Logger
|
||||
def initialize app, tags = nil, data = nil
|
||||
super app, tags
|
||||
@data = data
|
||||
@data = data || {}
|
||||
end
|
||||
|
||||
def call_app env
|
||||
request = ActionDispatch::Request.new env
|
||||
def call_app request, env
|
||||
path = request.filtered_path
|
||||
Rails.logger.info [:connection, request.ip, Thread.current.object_id, request.request_method, path]
|
||||
Rails.logger.custom( :connection, request.ip, Thread.current.object_id, request.request_method, path).info
|
||||
@app.call env
|
||||
ensure
|
||||
ActiveSupport::LogSubscriber.flush_all!
|
||||
end
|
||||
|
||||
def call env
|
||||
if @data
|
||||
Rails.logger.data( compute_data( env)) { super env }
|
||||
else
|
||||
super env
|
||||
request = ActionDispatch::Request.new env
|
||||
compute_tagged_ request do
|
||||
compute_data_ request do
|
||||
call_app request, env
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def compute_data env
|
||||
request = ActionDispatch::Request.new env
|
||||
def compute_tagged_ request
|
||||
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.each do |k, v|
|
||||
case v
|
||||
|
|
|
@ -9,11 +9,7 @@ class Semlogger::Rotate
|
|||
|
||||
def open_if
|
||||
name = Time.now.strftime( @filename).gsub /%\$/, $$.to_s
|
||||
#p at: Time.now, open_if: name, cur: @file ? @file.path : nil, e: @file ? name == @file.path : nil
|
||||
if @file and name == @file.path
|
||||
@file.close
|
||||
@file = nil
|
||||
end
|
||||
close if @file and name != @file.path
|
||||
@file = File.open name, 'a' unless @file
|
||||
@file
|
||||
end
|
||||
|
@ -24,5 +20,6 @@ class Semlogger::Rotate
|
|||
|
||||
def close
|
||||
@file.close
|
||||
@file = nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,11 +3,15 @@ end
|
|||
|
||||
class Semlogger::Writer < Semlogger::Output
|
||||
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
|
||||
|
||||
def add severity, time, progname, data, tags, message
|
||||
@logdev.write [severity, time, progname, data, tags, message].to_json+"\n"
|
||||
@logdev.write [severity, time.xmlschema(9), progname, data, tags, message].to_json+"\n"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -24,8 +28,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]
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{semlogger}
|
||||
s.version = "0.0.2"
|
||||
s.name = "semlogger"
|
||||
s.version = "0.0.10"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Denis Knauf"]
|
||||
s.date = %q{2012-05-20}
|
||||
s.description = %q{(Semi-)Structured Logger for Ruby (and Rails)}
|
||||
s.date = "2012-11-21"
|
||||
s.description = "(Semi-)Structured Logger for Ruby (and Rails)"
|
||||
s.email = ["Denis.Knauf@gmail.com"]
|
||||
s.extra_rdoc_files = [
|
||||
"LICENSE",
|
||||
|
@ -30,13 +30,12 @@ Gem::Specification.new do |s|
|
|||
"lib/semlogger/tailer.rb",
|
||||
"lib/semlogger/writer.rb"
|
||||
]
|
||||
s.homepage = %q{http://github.com/DenisKnauf/semlogger}
|
||||
s.homepage = "http://github.com/DenisKnauf/semlogger"
|
||||
s.require_paths = ["lib"]
|
||||
s.rubygems_version = %q{1.3.7}
|
||||
s.summary = %q{(Semi-)Structured Logger}
|
||||
s.rubygems_version = "1.8.23"
|
||||
s.summary = "(Semi-)Structured Logger"
|
||||
|
||||
if s.respond_to? :specification_version then
|
||||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
||||
s.specification_version = 3
|
||||
|
||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
||||
|
|
Loading…
Reference in a new issue