lib/yog.rb in yog-0.2.0 vs lib/yog.rb in yog-0.3.0

- old
+ new

@@ -13,13 +13,13 @@ freeze end # Log a message. Logs any given block's duration as `elapsed=float` ms. - def write(msg, **fields) + def info(msg, **fields) if @output.nil? - return block_given? && yield + return (yield if block_given?) end if msg.is_a?(Hash) msg, fields = nil, fields.merge(msg) end @@ -35,42 +35,42 @@ if block_given? result = yield prefix[:duration] = (Time.now - start) * 1000 end - combined = Yog.merge(prefix, fields, @context) + combined = prefix.merge(@context).merge(fields) @output.puts(@generator.generate(combined)) result end # Log an error. def error(ex, **fields) - write(ex.message, at: "error", type: ex.class, **fields) + info(error: ex.message, type: ex.class, **fields) end # Update the context. - def push(**fields) - self.tap { @context.merge!(fields) } + def set(**fields) + tap { @context.merge!(fields) } end # Create a new log with more context. def with(**fields) self.class.new \ - context: Yog.merge(@context, fields), + context: @context.merge(fields), generator: @generator, output: @output end ### Helpers for the default log - # The log for Yog(), Yog.push(), and Yog.with(). - def self.current + # The log for Yog(), Yog.set(), and Yog.with(). + def self.default logs.first end - # A stack of receivers with the current log first. + # A stack of receivers with the default log first. def self.logs Thread.current[:logs] ||= [new] end # Add fields to all Yog() calls in the given block. @@ -78,40 +78,35 @@ yield logs.unshift(with(fields)) ensure logs.shift end - # Update the context of the current log. - def self.push(**fields) - current.push(fields) + # Update the context of the default log. + def self.set(**fields) + default.set(fields) end - # Create a new log by extending the context of the current log. + # Create a new log by extending the context of the default log. def self.with(**fields) - current.with(fields) + default.with(fields) end - # Write a line to the current log. - def self.write(msg, **fields, &block) - current.write(msg, fields, &block) + # Write a line to the default log. + def self.info(msg, **fields, &block) + default.info(msg, fields, &block) end - # Log an error to the current log. + # Log an error to the default log. def self.error(ex, **fields) - current.error(ex, fields) + default.error(ex, fields) end - - # Internal: Combine a list of contexts. - def self.merge(*contexts) - contexts.inject { |a, b| a.merge(b) } - end end -# Write a line to the current log. +# Write a line to the default log. def Yog(msg, **fields, &block) case msg when Exception Yog.error(msg, fields) else - Yog.write(msg, fields, &block) + Yog.info(msg, fields, &block) end end