lib/semantic_logger/log.rb in semantic_logger-4.2.0 vs lib/semantic_logger/log.rb in semantic_logger-4.2.1
- old
+ new
@@ -95,10 +95,11 @@
self.exception = exception
when :partial
self.message = "#{message} -- Exception: #{exception.class}: #{exception.message}"
when nil, :none
# Log the message without the exception that was raised
+ nil
else
raise(ArgumentError, "Invalid value:#{log_exception.inspect} for argument :log_exception")
end
# On exception change the log level
if on_exception_level
@@ -117,11 +118,11 @@
self.metric = metric
self.metric_amount = metric_amount
self.dimensions = dimensions
end
- self.payload = payload if payload && (payload.size > 0)
+ self.payload = payload if payload&.size&.positive?
true
end
# Assign positional arguments to this log entry, supplying defaults where applicable
#
@@ -148,11 +149,11 @@
if result.is_a?(String)
message = message.nil? ? result : "#{message} -- #{result}"
assign(message: message, payload: payload, exception: exception)
elsif message.nil? && result.is_a?(Hash)
assign(result)
- elsif payload && payload.respond_to?(:merge)
+ elsif payload&.respond_to?(:merge)
assign(message: message, payload: payload.merge(result), exception: exception)
else
assign(message: message, payload: result, exception: exception)
end
else
@@ -165,11 +166,11 @@
def each_exception
# With thanks to https://github.com/bugsnag/bugsnag-ruby/blob/6348306e44323eee347896843d16c690cd7c4362/lib/bugsnag/notification.rb#L81
depth = 0
exceptions = []
ex = exception
- while ex != nil && !exceptions.include?(ex) && exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP
+ while !ex.nil? && !exceptions.include?(ex) && exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP
exceptions << ex
yield(ex, depth)
depth += 1
ex =
@@ -185,11 +186,11 @@
# Returns [String] the exception backtrace including all of the child / caused by exceptions
def backtrace_to_s
trace = ''
each_exception do |exception, i|
- if i == 0
+ if i.zero?
trace = (exception.backtrace || []).join("\n")
else
trace << "\nCause: #{exception.class.name}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}"
end
end
@@ -204,26 +205,26 @@
"#{duration.to_i}ms" if duration
end
else
def duration_to_s
return unless duration
- duration < 10.0 ? "#{'%.3f' % duration}ms" : "#{'%.1f' % duration}ms"
+ duration < 10.0 ? "#{format('%.3f', duration)}ms" : "#{format('%.1f', duration)}ms"
end
end
# Returns [String] the duration in human readable form
def duration_human
return nil unless duration
seconds = duration / 1000
- if seconds >= 86400.0 # 1 day
- "#{(seconds / 86400).to_i}d #{Time.at(seconds).strftime('%-Hh %-Mm')}"
+ if seconds >= 86_400.0 # 1 day
+ "#{(seconds / 86_400).to_i}d #{Time.at(seconds).strftime('%-Hh %-Mm')}"
elsif seconds >= 3600.0 # 1 hour
Time.at(seconds).strftime('%-Hh %-Mm')
elsif seconds >= 60.0 # 1 minute
Time.at(seconds).strftime('%-Mm %-Ss')
elsif seconds >= 1.0 # 1 second
- "#{'%.3f' % seconds}s"
+ "#{format('%.3f', seconds)}s"
else
duration_to_s
end
end
@@ -237,11 +238,11 @@
# 18934:thread 23 test_logging.rb:51
def process_info(thread_name_length = 30)
file, line = file_name_and_line(true)
file_name = " #{file}:#{line}" if file
- "#{$$}:#{"%.#{thread_name_length}s" % thread_name}#{file_name}"
+ "#{$PROCESS_ID}:#{format("%.#{thread_name_length}s", thread_name)}#{file_name}"
end
CALLER_REGEXP = /^(.*):(\d+).*/
# Extract the filename and line number from the last entry in the supplied backtrace
@@ -251,33 +252,34 @@
end
# Returns [String, String] the file_name and line_number from the backtrace supplied
# in either the backtrace or exception
def file_name_and_line(short_name = false)
- if backtrace || (exception && exception.backtrace)
- stack = backtrace || exception.backtrace
- extract_file_and_line(stack, short_name) if stack && stack.size > 0
- end
+ stack = backtrace || exception&.backtrace
+ extract_file_and_line(stack, short_name) if stack&.size&.positive?
end
# Strip the standard Rails colorizing from the logged message
def cleansed_message
message.to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip
end
# Return the payload in text form
# Returns nil if payload is missing or empty
def payload_to_s
- payload.inspect if has_payload?
+ payload.inspect if payload?
end
# Returns [true|false] whether the log entry has a payload
- def has_payload?
+ def payload?
!(payload.nil? || (payload.respond_to?(:empty?) && payload.empty?))
end
# DEPRECATED
+ alias has_payload? payload?
+
+ # DEPRECATED
def formatted_time
time.strftime(Formatters::Base::TIME_FORMAT)
end
DeprecatedLogger = Struct.new(:host, :application)
@@ -295,9 +297,7 @@
# A metric only event has a metric but no message, exception, or payload.
def metric_only?
metric && message.nil? && exception.nil? && payload.nil?
end
-
end
-
end