Compare commits

..

26 commits

Author SHA1 Message Date
Denis Knauf d1694702b8 Regenerate gemspec for version 0.0.10 2012-11-21 16:11:47 +01:00
Denis Knauf 6d2157caff #new_rails_logger: custom logdev-template. Semlogger::Rack: #call will never super anymore. 2012-11-21 16:11:34 +01:00
Denis Knauf 25ff6d52d8 Regenerate gemspec for version 0.0.9 2012-11-19 17:33:37 +01:00
Denis Knauf 595f4ac6f1 rdoc-deprecation fixed 2012-11-19 17:33:17 +01:00
Denis Knauf 8edb8ccdea v0.0.9 2012-11-19 17:08:00 +01:00
Denis Knauf 26853e31e2 #log_level added, rack-logger 3.2.9 -compatible 2012-11-19 17:07:39 +01:00
Denis Knauf fd8860766b Regenerate gemspec for version 0.0.8 2012-10-19 11:39:07 +02:00
Denis Knauf 3874aa0e43 v0.0.8 2012-10-19 11:38:57 +02:00
Denis Knauf 3b20edf5cf ... fixes 2012-10-19 11:38:12 +02:00
Denis Knauf e04b61dd4a Semlogger.new_rails_logger(config) can be used in config/environment.rb for initialize Semlogger for Rails. Different behaviour for different environments. 2012-10-19 11:32:29 +02:00
Denis Knauf eba19017af To use Semlogger::Rack, require "semlogger/rack". It will not be required by default anymore. 2012-10-19 11:18:20 +02:00
Denis Knauf 1108a9331c lesser deps to Rails (or no deps) 2012-10-18 19:34:04 +02:00
Denis Knauf e9cba75802 Regenerate gemspec for version 0.0.7 2012-10-17 23:15:12 +02:00
Denis Knauf 57896e3145 v0.0.7 2012-10-17 23:14:58 +02:00
Denis Knauf 4e803a26e7 Semlogger::Base for types added. 2012-10-17 23:14:09 +02:00
Denis Knauf 95417dec97 Regenerate gemspec for version 0.0.6 2012-10-15 16:15:42 +02:00
Denis Knauf eee7d885cc v0.0.6 2012-10-15 16:15:30 +02:00
Denis Knauf 6c9b038de0 tagged: expects not an array anymore. list your tags naturaly: tagged(:a,:b). CustomType: attr logger and severity-methods added for simplier custom loglines: logger.custom( :conn, ip, port).info 2012-10-15 16:08:35 +02:00
Denis Knauf d4dd5dfd0e Regenerate gemspec for version 0.0.5 2012-09-17 13:47:10 +02:00
Denis Knauf c44010dd64 v0.0.5 rotate: close-bedingung fixed 2012-09-17 13:46:57 +02:00
Denis Knauf a7745f28a1 Regenerate gemspec for version 0.0.4 2012-09-17 13:21:14 +02:00
Denis Knauf 742bb3b01e 0.0.4 2012-09-17 13:20:54 +02:00
Denis Knauf 59957f03b7 rotate: close-bedingung fixed 2012-09-17 13:20:31 +02:00
Denis Knauf 0f6eee9e91 Regenerate gemspec for version 0.0.3 2012-05-21 13:54:30 +02:00
Denis Knauf 868507672a v0.0.3: customtypes 2012-05-21 13:54:21 +02:00
Denis Knauf f06b2be077 timestamp: xmlschema in nanoseconds 2012-05-20 21:39:31 +02:00
7 changed files with 155 additions and 40 deletions

View file

@ -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'

View file

@ -1 +1 @@
0.0.2
0.0.10

View file

@ -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

View file

@ -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
end
end
def compute_data 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.each do |k, v|
case v

View file

@ -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

View file

@ -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]

View file

@ -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