lib/semantic_logger/log.rb in semantic_logger-4.6.1 vs lib/semantic_logger/log.rb in semantic_logger-4.7.0

- old
+ new

@@ -85,11 +85,11 @@ self.duration = duration return false if (duration < min_duration) && exception.nil? end self.message = message - if payload && payload.is_a?(Hash) + if payload&.is_a?(Hash) self.payload = payload elsif payload self.message = message.nil? ? payload.to_s : "#{message} -- #{payload}" self.payload = nil end @@ -153,11 +153,11 @@ if block_given? && (result = yield) 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) && %i[message payload exception].any? { |k| result.key? k } - assign(result) + assign(**result) elsif payload&.respond_to?(:merge) assign(message: message, payload: payload.merge(result), exception: exception) else assign(message: message, payload: result, exception: exception) end @@ -176,11 +176,11 @@ while !ex.nil? && !exceptions.include?(ex) && exceptions.length < MAX_EXCEPTIONS_TO_UNWRAP exceptions << ex yield(ex, depth) depth += 1 - ex = + ex = if ex.respond_to?(:cause) && ex.cause ex.cause elsif ex.respond_to?(:continued_exception) && ex.continued_exception ex.continued_exception elsif ex.respond_to?(:original_exception) && ex.original_exception @@ -189,11 +189,11 @@ end end # Returns [String] the exception backtrace including all of the child / caused by exceptions def backtrace_to_s - trace = '' + trace = "" each_exception do |exception, i| if i.zero? trace = (exception.backtrace || []).join("\n") else trace << "\nCause: #{exception.class.name}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}" @@ -210,24 +210,26 @@ "#{duration.to_i}ms" if duration end else def duration_to_s return unless duration + 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 >= 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') + Time.at(seconds).strftime("%-Hh %-Mm") elsif seconds >= 60.0 # 1 minute - Time.at(seconds).strftime('%-Mm %-Ss') + Time.at(seconds).strftime("%-Mm %-Ss") elsif seconds >= 1.0 # 1 second "#{format('%.3f', seconds)}s" else duration_to_s end @@ -236,21 +238,19 @@ # Returns [String] single character upper case log level def level_to_s level.to_s[0..0].upcase end - # Returns [String] the available process info - # Example: - # 18934:thread 23 test_logging.rb:51 + # DEPRECATED def process_info(thread_name_length = 30) file, line = file_name_and_line(true) file_name = " #{file}:#{line}" if file "#{$$}:#{format("%.#{thread_name_length}s", thread_name)}#{file_name}" end - CALLER_REGEXP = /^(.*):(\d+).*/ + CALLER_REGEXP = /^(.*):(\d+).*/.freeze # Extract the filename and line number from the last entry in the supplied backtrace def extract_file_and_line(stack, short_name = false) match = CALLER_REGEXP.match(stack.first) [short_name ? File.basename(match[1]) : match[1], match[2].to_i] @@ -263,11 +263,11 @@ 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 + 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 @@ -275,9 +275,15 @@ end # Returns [true|false] whether the log entry has a payload def payload? !(payload.nil? || (payload.respond_to?(:empty?) && payload.empty?)) + end + + def to_h(host = SemanticLogger.host, application = SemanticLogger.application, environment = SemanticLogger.environment) + logger = Struct.new(:host, :application, :environment) + .new(host, application, environment) + SemanticLogger::Formatters::Raw.new.call(self, logger) end # Lazy initializes the context hash and assigns a key value pair. def set_context(key, value) (self.context ||= {})[key] = value