Compare commits

..

No commits in common. "master" and "v0.0.2" have entirely different histories.

7 changed files with 42 additions and 157 deletions

View file

@ -43,7 +43,7 @@ end
#task :default => :test #task :default => :test
require 'rdoc/task' require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc| Rake::RDocTask.new do |rdoc|
if File.exist? 'VERSION' if File.exist? 'VERSION'
version = File.read 'VERSION' version = File.read 'VERSION'

View file

@ -1 +1 @@
0.0.10 0.0.2

View file

@ -1,123 +1,26 @@
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 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 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
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
@tags += tags.flatten.compact @tags += tags
tags = tags.size tags = tags.size
yield yield
ensure ensure
#tags.times { @tags.pop } tags.times { @tags.pop }
@tags.slice! -tags .. -1
end end
def add severity, message = nil, progname = nil, &block def add severity, message = nil, progname = nil, &block
severity ||= UNKNOWN severity ||= UNKNOWN
return true if @logdev.nil? or severity < @level if @logdev.nil? or severity < @level
return true
end
progname ||= @progname progname ||= @progname
if message.nil? if message.nil?
if block_given? if block_given?
@ -131,10 +34,11 @@ class Semlogger < ::Logger
end end
def format_msg msg def format_msg msg
msg = msg.to_semlogger
case msg case msg
when Array then msg when Numeric, true, false, nil then [:const, msg]
else [msg.class.name.to_sym, msg.inspect] when String then [:str, msg]
when Exception then [:exception, msg.class.name, msg.message.to_s, msg.backtrace]
else [:obj, msg]
end end
end end
@ -156,7 +60,6 @@ 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
@ -175,11 +78,6 @@ end
require 'semlogger/rotate' require 'semlogger/rotate'
require 'semlogger/multiplex' require 'semlogger/multiplex'
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

@ -1,43 +1,29 @@
class Semlogger::Rack < Rails::Rack::Logger class Semlogger::Rack < Rails::Rack::Logger
def initialize app, tags = nil, data = nil def initialize app, tags = nil, data = nil
super app, tags super app, tags
@data = data || {} @data = data
end end
def call_app request, env def call_app env
request = ActionDispatch::Request.new env
path = request.filtered_path path = request.filtered_path
Rails.logger.custom( :connection, request.ip, Thread.current.object_id, request.request_method, path).info Rails.logger.info [: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!
end end
def call env def call env
if @data
Rails.logger.data( compute_data( env)) { super env }
else
super env
end
end
def compute_data env
request = ActionDispatch::Request.new env request = ActionDispatch::Request.new env
compute_tagged_ request do
compute_data_ request do
call_app request, env
end
end
end
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 = @data.dup
data.each do |k, v| data.each do |k, v|
case v case v

View file

@ -9,7 +9,11 @@ class Semlogger::Rotate
def open_if def open_if
name = Time.now.strftime( @filename).gsub /%\$/, $$.to_s name = Time.now.strftime( @filename).gsub /%\$/, $$.to_s
close if @file and name != @file.path #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
@file = File.open name, 'a' unless @file @file = File.open name, 'a' unless @file
@file @file
end end
@ -20,6 +24,5 @@ class Semlogger::Rotate
def close def close
@file.close @file.close
@file = nil
end end
end end

View file

@ -3,15 +3,11 @@ end
class Semlogger::Writer < Semlogger::Output class Semlogger::Writer < Semlogger::Output
def initialize logdev = nil def initialize logdev = nil
@logdev = case logdev @logdev = logdev || ::Semlogger::Rotate.new( ::Rails.root.join( 'log', ::Rails.env).to_s.gsub('%', '%%') + '.%Y-%m-%d.%$.log')
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
@logdev.write [severity, time.xmlschema(9), progname, data, tags, message].to_json+"\n" @logdev.write [severity, time, progname, data, tags, message].to_json+"\n"
end end
end end
@ -28,7 +24,8 @@ 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 :String, :const then message[1] when :str, :const then message[1]
when :obj then message[1].inspect
else message.inspect else message.inspect
end end
reqid = data[:reqid] reqid = data[:reqid]

View file

@ -4,13 +4,13 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "semlogger" s.name = %q{semlogger}
s.version = "0.0.10" s.version = "0.0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Denis Knauf"] s.authors = ["Denis Knauf"]
s.date = "2012-11-21" s.date = %q{2012-05-20}
s.description = "(Semi-)Structured Logger for Ruby (and Rails)" s.description = %q{(Semi-)Structured Logger for Ruby (and Rails)}
s.email = ["Denis.Knauf@gmail.com"] s.email = ["Denis.Knauf@gmail.com"]
s.extra_rdoc_files = [ s.extra_rdoc_files = [
"LICENSE", "LICENSE",
@ -30,12 +30,13 @@ Gem::Specification.new do |s|
"lib/semlogger/tailer.rb", "lib/semlogger/tailer.rb",
"lib/semlogger/writer.rb" "lib/semlogger/writer.rb"
] ]
s.homepage = "http://github.com/DenisKnauf/semlogger" s.homepage = %q{http://github.com/DenisKnauf/semlogger}
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = "1.8.23" s.rubygems_version = %q{1.3.7}
s.summary = "(Semi-)Structured Logger" s.summary = %q{(Semi-)Structured Logger}
if s.respond_to? :specification_version then if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3 s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then