lib/semantic_logger/formatters/default.rb in semantic_logger-4.0.0 vs lib/semantic_logger/formatters/default.rb in semantic_logger-4.1.0

- old
+ new

@@ -1,47 +1,72 @@ module SemanticLogger module Formatters + # Default non-colored text log output class Default < Base - # Default text log format - # Generates logs of the form: - # 2011-07-19 14:36:15.660235 D [1149:ScriptThreadProcess] Rails -- Hello World - def call(log, logger) - # Date & time - message = time_format.nil? ? '' : "#{format_time(log.time)} " + attr_accessor :log, :logger - # Log level and process info - message << "#{log.level_to_s} [#{log.process_info}]" + # Formatting methods, must return nil, or a string + # Nil values are ignored - # Tags - message << ' ' << log.tags.collect { |tag| "[#{tag}]" }.join(' ') if log.tags && (log.tags.size > 0) + # Log level + def level + log.level_to_s + end - # Named Tags + # Process info + def process_info + "[#{log.process_info}]" + end + + # Tags + def tags + "[#{log.tags.join('] [')}]" if log.tags && !log.tags.empty? + end + + # Named Tags + def named_tags if (named_tags = log.named_tags) && !named_tags.empty? list = [] - named_tags.each_pair { |name, value| list << "[#{name}: #{value}]" } - message << ' ' << list.join(' ') + named_tags.each_pair { |name, value| list << "#{name}: #{value}" } + "{#{list.join(', ')}}" end + end - # Duration - message << " (#{log.duration_human})" if log.duration + # Duration + def duration + "(#{log.duration_human})" if log.duration + end - # Class / app name - message << " #{log.name}" + # Class / app name + def name + log.name + end - # Log message - message << " -- #{log.message}" if log.message + # Log message + def message + "-- #{log.message}" if log.message + end - # Payload - if payload = log.payload_to_s - message << ' -- ' << payload + # Payload + def payload + if pl = log.payload_to_s + "-- #{pl}" end + end - # Exceptions - if log.exception - message << " -- Exception: #{log.exception.class}: #{log.exception.message}\n" - message << log.backtrace_to_s - end - message + # Exception + def exception + "-- Exception: #{log.exception.class}: #{log.exception.message}\n#{log.backtrace_to_s}" if log.exception + end + + # Default text log format + # Generates logs of the form: + # 2011-07-19 14:36:15.660235 D [1149:ScriptThreadProcess] Rails -- Hello World + def call(log, logger) + self.log = log + self.logger = logger + + [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(' ') end end end end