lib/yog.rb in yog-0.3.0 vs lib/yog.rb in yog-0.4.0
- old
+ new
@@ -3,10 +3,13 @@
require "yog/logfmt"
# Writes structured log lines.
class Yog
+ # This context is included in every log line.
+ GLOBAL = {}
+
# Get ready to do some yogging.
def initialize(generator: Logfmt, output: $stdout, context: {})
@context = context
@generator = generator
@output = output
@@ -35,11 +38,11 @@
if block_given?
result = yield
prefix[:duration] = (Time.now - start) * 1000
end
- combined = prefix.merge(@context).merge(fields)
+ combined = prefix.merge(GLOBAL).merge(@context).merge(fields)
@output.puts(@generator.generate(combined))
result
end
@@ -59,17 +62,17 @@
context: @context.merge(fields),
generator: @generator,
output: @output
end
- ### Helpers for the default log
+ ### Static helpers
# The log for Yog(), Yog.set(), and Yog.with().
def self.default
logs.first
end
- # A stack of receivers with the default log first.
+ # A stack of thread-local receivers, current log first.
def self.logs
Thread.current[:logs] ||= [new]
end
# Add fields to all Yog() calls in the given block.