lib/hanami/logger.rb in hanami-utils-0.9.2 vs lib/hanami/logger.rb in hanami-utils-1.0.0.beta1

- old
+ new

@@ -106,10 +106,14 @@ # @since 0.8.0 # @api private NEW_LINE = $/ + # @since 1.0.0.beta1 + # @api private + RESERVED_KEYS = [:app, :severity, :time].freeze + include Utils::ClassAttribute class_attribute :subclasses self.subclasses = Set.new @@ -135,19 +139,23 @@ # @since 0.5.0 # @api private attr_writer :application_name + # @since 1.0.0.beta1 + # @api private + attr_reader :application_name + # @since 0.5.0 # @api private # # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html#method-i-call def call(severity, time, _progname, msg) _format({ - app: @application_name, + app: application_name, severity: severity, - time: time.utc + time: time }.merge( _message_hash(msg) )) end @@ -171,12 +179,32 @@ end # @since 0.8.0 # @api private def _format(hash) - hash.map { |k, v| "#{k}=#{v}" }.join(SEPARATOR) + NEW_LINE + result = RESERVED_KEYS.map { |k| "[#{hash[k]}]" }.join(SEPARATOR) + return _format_error(result, hash) if hash.key?(:error) + + values = hash.each_with_object([]) do |(k, v), memo| + memo << v unless RESERVED_KEYS.include?(k) + end + + result << " #{values.join(SEPARATOR)}#{NEW_LINE}" + result end + + def _format_error(result, hash) + result << " #{hash[:error]}:" if hash.key?(:error) + result << " #{hash[:message]}#{NEW_LINE}" + if hash.key?(:backtrace) + hash[:backtrace].each do |line| + result << "from #{line}#{NEW_LINE}" + end + end + + result + end end # Hanami::Logger JSON formatter. # This formatter returns string in JSON format. # @@ -190,19 +218,20 @@ private # @since 0.8.0 # @api private def _format(hash) - Hanami::Utils::Json.dump(hash) + hash[:time] = hash[:time].utc.iso8601 + Hanami::Utils::Json.generate(hash) + NEW_LINE end end # Default application name. # This is used as a fallback for tagging purposes. # # @since 0.5.0 # @api private - DEFAULT_APPLICATION_NAME = 'Hanami'.freeze + DEFAULT_APPLICATION_NAME = 'hanami'.freeze # @since 0.8.0 # @api private LEVELS = Hash[ 'debug' => DEBUG,