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

- old
+ new

@@ -11,16 +11,19 @@ # method signatures. This ensures Timber can be removed without consequence. # # @example Basic example (the original ::Logger interface remains untouched): # logger.info "Payment rejected for customer #{customer_id}" # - # @example Using a map - # # The :message, :type, and :data keys are required - # logger.info message: "Payment rejected", type: :payment_rejected, data: {customer_id: customer_id, amount: 100} + # @example Using a Hash + # # The :message key is required, the other additional key is your event type and data + # # :type is the namespace used in timber for the :data + # logger.info message: "Payment rejected", payment_rejected: {customer_id: customer_id, amount: 100} # # @example Using a Struct (a simple, more structured way, to define events) # PaymentRejectedEvent = Struct.new(:customer_id, :amount, :reason) do + # # `#message` and `#type` are required, otherwise they will not be logged properly. + # # `#type` is the namespace used in timber for the struct data # def message; "Payment rejected for #{customer_id}"; end # def type; :payment_rejected; end # end # Logger.info PaymentRejectedEvent.new("abcd1234", 100, "Card expired") # @@ -70,16 +73,27 @@ } private def build_log_entry(severity, time, progname, msg) level = SEVERITY_MAP.fetch(severity) - context = CurrentContext.instance.snapshot + context_snapshot = CurrentContext.instance.snapshot + tags = extract_active_support_tagged_logging_tags + tags += [msg.delete(:tag)] if msg.is_a?(Hash) && msg.key?(:tag) + tags += msg.delete(:tags) if msg.is_a?(Hash) && msg.key?(:tags) event = Events.build(msg) + if event - LogEntry.new(level, time, progname, event.message, context, event) + LogEntry.new(level, time, progname, event.message, context_snapshot, event, tags) else - LogEntry.new(level, time, progname, msg, context, nil) + LogEntry.new(level, time, progname, msg, context_snapshot, nil, tags) end + end + + # Because of all the crazy ways Rails has attempted this we need this crazy method. + def extract_active_support_tagged_logging_tags + Thread.current[:activesupport_tagged_logging_tags] || + Thread.current["activesupport_tagged_logging_tags:#{object_id}"] || + [] end end # Structures your log messages into Timber's hybrid format, which makes # it easy to read while also appending the appropriate metadata. \ No newline at end of file