lib/timber/logger.rb in timber-1.1.7 vs lib/timber/logger.rb in timber-1.1.8

- old
+ new

@@ -58,10 +58,11 @@ # end # # Logger.info PymentRejectedEvent.new("abcd1234", 100) # class Logger < ::Logger + # @private class Formatter # Formatters get the formatted level from the logger. SEVERITY_MAP = { "DEBUG" => :debug, @@ -159,31 +160,51 @@ # @example Changing your formatter # logger = Timber::Logger.new(STDOUT) # logger.formatter = Timber::Logger::JSONFormatter.new def initialize(*args) super(*args) + + # Ensure we sync STDOUT to avoid buffering + if args.size == 1 and args.first.respond_to?(:"sync=") + args.first.sync = true + end + if args.size == 1 and args.first.is_a?(LogDevices::HTTP) self.formatter = PassThroughFormatter.new else self.formatter = HybridFormatter.new end + + self.level = environment_level end def formatter=(value) if @dev.is_a?(Timber::LogDevices::HTTP) 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 - super + + if !value.is_a?(Timber::Logger::Formatter) + # silently discard this value since rails calls this during initialization :/ + nil + else + super + end end # Backwards compatibility with older ActiveSupport::Logger versions Logger::Severity.constants.each do |severity| class_eval(<<-EOT, __FILE__, __LINE__ + 1) def #{severity.downcase}? # def debug? Logger::#{severity} >= level # DEBUG >= level end # end EOT end + + private + def environment_level + level = ([ENV['LOG_LEVEL'].to_s.upcase, "DEBUG"] & %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]).compact.first + self.class.const_get(level) + end end end \ No newline at end of file