lib/yog.rb in yog-0.1.0 vs lib/yog.rb in yog-0.2.0
- old
+ new
@@ -14,26 +14,32 @@
freeze
end
# Log a message. Logs any given block's duration as `elapsed=float` ms.
def write(msg, **fields)
+ if @output.nil?
+ return block_given? && yield
+ end
+
if msg.is_a?(Hash)
- fields, msg = msg, nil
+ msg, fields = nil, fields.merge(msg)
end
- result = if block_given?
- start = Time.now
+ start = Time.now.utc
+ prefix = { now: start }
+ result = nil
- yield.tap do
- fields[:duration] = (Time.now - start) * 1000
- end
+ if msg
+ prefix[:msg] = msg
end
- unless @output.nil?
- prefix = { now: Time.now.utc, msg: msg }
- combined = Yog.merge(prefix, fields, @context)
- @output.puts(@generator.generate(combined))
+ if block_given?
+ result = yield
+ prefix[:duration] = (Time.now - start) * 1000
end
+
+ combined = Yog.merge(prefix, fields, @context)
+ @output.puts(@generator.generate(combined))
result
end
# Log an error.