lib/semantic_logger/base.rb in semantic_logger-4.2.0 vs lib/semantic_logger/base.rb in semantic_logger-4.2.1
- old
+ new
@@ -21,11 +21,11 @@
if level.nil?
# Use the global default level for this logger
@level_index = nil
@level = nil
else
- @level_index = SemanticLogger.level_to_index(level)
+ @level_index = SemanticLogger.send(:level_to_index, level)
@level = SemanticLogger.send(:index_to_level, @level_index)
end
end
# Returns the current log level if set, otherwise it returns the global
@@ -76,11 +76,11 @@
#
# # Log an exception in a semantic way
# logger.info("Parsing received XML", exc)
#
SemanticLogger::LEVELS.each_with_index do |level, index|
- class_eval <<-EOT, __FILE__, __LINE__ + 1
+ class_eval <<~METHODS, __FILE__, __LINE__ + 1
def #{level}(message=nil, payload=nil, exception=nil, &block)
if level_index <= #{index}
log_internal(:#{level}, #{index}, message, payload, exception, &block)
true
else
@@ -105,25 +105,25 @@
measure_internal(:#{level}, #{index}, message, params, &block)
else
block.call(params) if block
end
end
- EOT
+ METHODS
end
# Dynamically supply the log level with every measurement call
def measure(level, message, params = {}, &block)
index = SemanticLogger.level_to_index(level)
if level_index <= index
measure_internal(level, index, message, params, &block)
- else
- block.call(params) if block
+ elsif block
+ yield(params)
end
end
# Backward compatibility
- alias_method :benchmark, :measure
+ alias benchmark measure
# Log a thread backtrace
def backtrace(thread: Thread.current,
level: :warn,
message: 'Backtrace:',
@@ -148,11 +148,15 @@
if backtrace
message += "\n"
message << backtrace.join("\n")
end
- if log.assign(message: message, backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && !filtered?(log)
+ if log.assign(message: message,
+ backtrace: backtrace,
+ payload: payload,
+ metric: metric,
+ metric_amount: metric_amount) && !filtered?(log)
self.log(log)
else
false
end
end
@@ -194,11 +198,11 @@
new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
SemanticLogger.tagged(*new_tags, &block)
end
# :nodoc:
- alias_method :with_tags, :tagged
+ alias with_tags tagged
# :nodoc:
def tags
SemanticLogger.tags
end
@@ -241,12 +245,12 @@
warn '#payload is deprecated, use SemanticLogger.named_tags'
SemanticLogger.named_tags
end
# Write log data to underlying data storage
- def log(log_)
- raise NotImplementedError.new('Logging Appender must implement #log(log)')
+ def log(_log_)
+ raise NotImplementedError, 'Logging Appender must implement #log(log)'
end
# Whether this log entry meets the criteria to be logged by this appender.
def should_log?(log)
meets_log_level?(log) && !filtered?(log)
@@ -296,11 +300,11 @@
# Whether to log the supplied message based on the current filter if any
def filtered?(log)
return false if @filter.nil?
if @filter.is_a?(Regexp)
- (@filter =~ log.name) == nil
+ (@filter =~ log.name).nil?
elsif @filter.is_a?(Proc)
@filter.call(log) != true
else
raise(ArgumentError, "Unrecognized semantic logger filter: #{@filter.inspect}, must be a Regexp or a Proc")
end
@@ -315,11 +319,11 @@
def log_internal(level, index, message = nil, payload = nil, exception = nil, &block)
log = Log.new(name, level, index)
should_log =
if payload.nil? && exception.nil? && message.is_a?(Hash)
# Check if someone just logged a hash payload instead of meaning to call semantic logger
- if message.has_key?(:message) || message.has_key?(:payload) || message.has_key?(:exception) || message.has_key?(:metric)
+ if message.key?(:message) || message.key?(:payload) || message.key?(:exception) || message.key?(:metric)
log.assign(message)
else
log.assign_positional(nil, message, nil, &block)
end
else
@@ -341,11 +345,11 @@
end
start = Time.now
begin
if block_given?
result =
- if silence_level = params[:silence]
+ if (silence_level = params[:silence])
# In case someone accidentally sets `silence: true` instead of `silence: :error`
silence_level = :error if silence_level == true
silence(silence_level) { yield(params) }
else
yield(params)
@@ -393,12 +397,11 @@
level:,
message:,
min_duration:,
metric:,
log_exception:,
- on_exception_level:,
- &block)
+ on_exception_level:)
# Ignores filter, silence, payload
exception = nil
start = Time.now
begin
@@ -421,8 +424,7 @@
# Log level may change during assign due to :on_exception_level
log(log) if should_log && should_log?(log)
raise exception if exception
end
end
-
end
end