Skip to content

Commit

Permalink
CHG: use Rails::VERSION:MAJOR instead of Rails.version.to_i becau…
Browse files Browse the repository at this point in the history
…se there version string like "4.0.0.beta1"
  • Loading branch information
snow committed Mar 27, 2013
1 parent 4c69c0b commit ac2a741
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions lib/better_logging/better_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
# logging format. See the README file for more info.
#
# This is distributed under a Creative Commons "Attribution-Share Alike"
# license: for details see:
# license: for details see:
# http://creativecommons.org/licenses/by-sa/3.0/
#
module PaulDowman
module RailsPlugins
module BetterLogging

LENGTH = ActiveSupport::Logger::Severity.constants.map{|c| c.to_s.length}.max

def self.included(base)
base.class_eval do
alias_method_chain :add, :extra_info
alias_method_chain :error, :exception_param
alias_method_chain :warn, :exception_param
end

# Get the length to format the output so that the pid column lines up.
# The severity levels probably won't change but this avoids hard-coding
# them anyway, just in case.
# Most of this is done with class_eval so it should only be done once
# Most of this is done with class_eval so it should only be done once
# while the class is being loaded.
if_stmts = ""
for c in ActiveSupport::Logger::Severity.constants
if_stmts += <<-EOT
if severity == #{c}
severity_name = sprintf("%1$*2$s", "#{c}", #{LENGTH * -1})
use_colour = false
if Rails.version.to_i >= 3
if Rails::VERSION::MAJOR >= 3
use_colour = true if ActiveSupport::LogSubscriber.colorize_logging
else
use_colour = true if defined?(ActiveRecord) && ActiveRecord::Base.colorize_logging
Expand All @@ -54,41 +54,41 @@ def self.severity_name(severity)
end
EOT
end

def self.fraction_digits=(num)
@@fraction_digits = num
end

def self.verbose=(boolean)
@@verbose = boolean
end

def self.custom=(string)
@@custom = string
@@line_prefix = format_line_prefix
end

def self.hostname_maxlen=(integer)
@@hostname_maxlen = integer
@@line_prefix = format_line_prefix
end

def self.format_line_prefix
if @@full_hostname.length < @@hostname_maxlen
hostname = @@full_hostname
else
hostname = @@full_hostname[-(@@hostname_maxlen)..-1]
end

line_prefix = sprintf("%1$*2$s", "#{hostname}.#{@@pid} ", -(7 + hostname.length))
line_prefix = "#{@@custom} #{line_prefix}" if @@custom
return line_prefix
end

def self.get_hostname
`hostname -s`.strip
end

# The following are cached as class variables for speed.

# These are configurable, put something like the following in an initializer:
Expand All @@ -99,12 +99,12 @@ def self.get_hostname
@@custom = nil
@@fraction_digits = 6


# These are not configurable
@@pid = $$
@@line_prefix = format_line_prefix


# the cached pid can be wrong after a fork(), this checks if it has changed and
# re-caches the line_prefix
def update_pid
Expand All @@ -113,7 +113,7 @@ def update_pid
@@line_prefix = BetterLogging.format_line_prefix
end
end

def add_with_extra_info(severity, message = nil, progname = nil, &block)
update_pid
time = @@verbose ? "#{Time.now.iso8601(@@fraction_digits)} " : ""
Expand All @@ -122,29 +122,29 @@ def add_with_extra_info(severity, message = nil, progname = nil, &block)
if block_given?
if severity < @level
return true
end
end
message = yield
end
end
message = "#{time}#{ActiveSupport::Logger.severity_name(severity)} #{message}"
# Make sure every line has the PID and hostname and custom string

# Make sure every line has the PID and hostname and custom string
# so we can use grep to isolate output from one process or server.
# gsub works even when the output contains "\n", though there's
# probably a small performance cost.
message = message.gsub(/^/, @@line_prefix) if @@verbose

add_without_extra_info(severity, message, progname, &block)
end


# add an optional second parameter to the error & warn methods to allow a stack trace:

def error_with_exception_param(message, exception = nil)
message += "\n#{exception.inspect}\n#{exception.backtrace.join("\n")}" if exception
error_without_exception_param(message)
end

def warn_with_exception_param(message, exception = nil)
message += "\n#{exception.inspect}\n#{exception.backtrace.join("\n")}" if exception
warn_without_exception_param(message)
Expand Down

0 comments on commit ac2a741

Please sign in to comment.