lib/timber/current_context.rb in timber-1.0.13 vs lib/timber/current_context.rb in timber-1.1.0

- old
+ new

@@ -36,15 +36,23 @@ # Adds a context and then removes it when the block is finished executing. # # @note Because context is included with every log line, it is recommended that you limit this # to only neccessary data. # - # @example Adding a custom context - # custom_context = Timber::Contexts::Custom.new(type: :organization, data: %{id: 1, name: "Timber"}) - # Timber::CurrentContext.with(custom_context) do + # @example Adding a custom context with a map + # Timber::CurrentContext.with({build: {version: "1.0.0"}}) do # # ... anything logged here will include the context ... # end + # + # @example Adding a custom context with a struct + # BuildContext = Struct.new(:version) do + # def type; :build; end + # end + # build_context = BuildContext.new("1.0.0") + # Timber::CurrentContext.with(build_context) do + # # ... anything logged here will include the context ... + # end # # Be sure to checkout Timber::Contexts! These are officially supported and many of these # # will be automatically included via Timber::Probes # # @example Adding multiple contexts # Timber::CurrentContext.with(context1, context2) { ... } @@ -65,11 +73,12 @@ # Adds contexts but does not remove them. See {#with} for automatic maintenance and {#remove} # to remove them yourself. # # @note Because context is included with every log line, it is recommended that you limit this # to only neccessary data. - def add(*contexts) - contexts.each do |context| + def add(*objects) + objects.each do |object| + context = Contexts.build(object) # Normalizes objects into a Timber::Context descendant. key = context.keyspace json = context.as_json # Convert to json now so that we aren't doing it for every line if key == :custom # Custom contexts are merged into the space hash[key] ||= {} \ No newline at end of file