lib/timber/logger.rb in timber-1.1.14 vs lib/timber/logger.rb in timber-2.0.0

- old
+ new

@@ -14,11 +14,11 @@ # logger.info "Payment rejected for customer #{customer_id}" # # @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} + # logger.info "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 @@ -67,11 +67,11 @@ SEVERITY_MAP = { "DEBUG" => :debug, "INFO" => :info, "WARN" => :warn, "ERROR" => :error, - "FATAL" => :datal, + "FATAL" => :fatal, "UNKNOWN" => :unknown } private def build_log_entry(severity, time, progname, msg) @@ -79,13 +79,16 @@ context_snapshot = CurrentContext.instance.snapshot tags = extract_active_support_tagged_logging_tags time_ms = nil if msg.is_a?(Hash) - tags << msg.delete(:tag) if msg.key?(:tag) - tags += msg.delete(:tags) if msg.key?(:tags) - tags.uniq! + if msg.key?(:tag) || msg.key?(:tags) + tags = tags.clone + tags << msg.delete(:tag) if msg.key?(:tag) + tags += msg.delete(:tags) if msg.key?(:tags) + tags.uniq! + end time_ms = msg.delete(:time_ms) if msg.key?(:time_ms) msg = msg[:message] if msg.length == 1 end @@ -104,10 +107,20 @@ Thread.current["activesupport_tagged_logging_tags:#{object_id}"] || [] end end + # For use in development and test environments where you do not want metadata + # included in the log lines. + class SimpleFormatter < Formatter + + # This method is invoked when a log event occurs + def call(severity, timestamp, progname, msg) + "#{String === msg ? msg : msg.inspect}\n" + end + end + # Structures your log messages into Timber's hybrid format, which makes # it easy to read while also appending the appropriate metadata. # # logger = Timber::Logger.new(STDOUT) # logger.formatter = Timber::JSONFormatter.new @@ -151,10 +164,15 @@ def call(severity, time, progname, msg) build_log_entry(severity, time, progname, msg) end end + class << self + def new_from_config + end + end + # Creates a new Timber::Logger instances. Accepts the same arguments as `::Logger.new`. # The only difference is that it default the formatter to {HybridFormatter}. Using # a different formatter is easy. For example, if you prefer your logs in JSON. # # @example Changing your formatter @@ -173,29 +191,37 @@ else self.formatter = HybridFormatter.new end self.level = environment_level + + @initialized = true end def formatter=(value) - if @dev.is_a?(Timber::LogDevices::HTTP) + if @initialized && @logdev && @logdev.dev.is_a?(Timber::LogDevices::HTTP) && !value.is_a?(PassThroughFormatter) raise ArgumentError.new("The formatter cannot be changed when using the " + "Timber::LogDevices::HTTP log device. The PassThroughFormatter must be used for proper " + "delivery.") end - if !value.is_a?(Timber::Logger::Formatter) - # silently discard this value since rails calls this during initialization :/ - nil - else - super - end + super end # Backwards compatibility with older ActiveSupport::Logger versions Logger::Severity.constants.each do |severity| class_eval(<<-EOT, __FILE__, __LINE__ + 1) + def #{severity.downcase}(*args, &block) + progname = args.first + options = args.last + + if args.length == 2 and options.is_a?(Hash) && options != {} + progname = options.merge(message: progname) + end + + add(#{severity}, nil, progname, &block) + end + def #{severity.downcase}? # def debug? Logger::#{severity} >= level # DEBUG >= level end # end EOT end \ No newline at end of file