lib/pallets/logger.rb in pallets-0.10.0 vs lib/pallets/logger.rb in pallets-0.11.0
- old
+ new
@@ -7,12 +7,11 @@
%i[debug info warn error fatal unknown].each do |severity|
define_method severity do |message|
metadata = Thread.current[:pallets_log_metadata]
return super(message) if metadata.nil?
- formatted_metadata = ' ' + metadata.map { |k, v| "#{k}=#{v}" }.join(' ')
- super(formatted_metadata) { message }
+ super(metadata) { message }
end
end
def with_metadata(hash)
Thread.current[:pallets_log_metadata] = hash
@@ -22,10 +21,22 @@
end
module Formatters
class Pretty < ::Logger::Formatter
def call(severity, time, metadata, message)
- "#{time.utc.iso8601(4)} pid=#{Process.pid}#{metadata} #{severity}: #{message}\n"
+ formatted_metadata = ' ' + metadata.map { |k, v| "#{k}=#{v}" }.join(' ') if metadata
+ "#{time.utc.iso8601(4)} pid=#{Process.pid}#{formatted_metadata} #{severity}: #{message}\n"
+ end
+ end
+
+ class Json < ::Logger::Formatter
+ def call(severity, time, metadata, message)
+ {
+ timestamp: time.utc.iso8601(4),
+ pid: Process.pid,
+ severity: severity,
+ message: message
+ }.merge(metadata || {}).to_json + "\n"
end
end
end
end
end