lib/semantic_logger/base.rb in semantic_logger-2.15.0 vs lib/semantic_logger/base.rb in semantic_logger-2.16.0

- old
+ new

@@ -19,11 +19,11 @@ # # Must be one of the values in SemanticLogger::LEVELS, or # nil if this logger instance should use the global default level def level=(level) @level_index = SemanticLogger.level_to_index(level) - @level = level + @level = level end # Returns the current log level if set, otherwise it returns the global # default log level def level @@ -66,11 +66,11 @@ # # logger = SemanticLogger['MyApplication'] # logger.debug("Only display this if log level is set to Debug or lower") # # # Log semantic information along with a text message - # logger.info("Request received", :user => "joe", :duration => 100) + # logger.info("Request received", user: "joe", duration: 100) # # # Log an exception in a semantic way # logger.info("Parsing received XML", exc) # SemanticLogger::LEVELS.each_with_index do |level, index| @@ -132,12 +132,12 @@ # Add tags to the current scope # Returns the list of tags pushed after flattening them out and removing blanks def push_tags *tags # Need to flatten and reject empties to support calls from Rails 4 - new_tags = tags.flatten.collect(&:to_s).reject(&:empty?) - t = Thread.current[:semantic_logger_tags] + new_tags = tags.flatten.collect(&:to_s).reject(&:empty?) + t = Thread.current[:semantic_logger_tags] Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_tags) new_tags end # Remove specified number of tags from the current tag list @@ -148,22 +148,22 @@ # Thread specific context information to be logged with every log entry # # Add a payload to all log calls on This Thread within the supplied block # - # logger.with_payload(:tracking_number=>12345) do + # logger.with_payload(tracking_number: 12345) do # logger.debug('Hello World') # end # # If a log call already includes a pyload, this payload will be merged with # the supplied payload, with the supplied payload taking precedence # - # logger.with_payload(:tracking_number=>12345) do - # logger.debug('Hello World', :result => 'blah') + # logger.with_payload(tracking_number: 12345) do + # logger.debug('Hello World', result: 'blah') # end def with_payload(payload) - current_payload = self.payload + current_payload = self.payload Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload yield ensure Thread.current[:semantic_logger_payload] = current_payload end @@ -216,26 +216,26 @@ # # Note: # #silence does not affect any loggers which have had their log level set # explicitly. I.e. That do not rely on the global default level def silence(new_level = :error) - current_index = Thread.current[:semantic_logger_silence] + current_index = Thread.current[:semantic_logger_silence] Thread.current[:semantic_logger_silence] = SemanticLogger.level_to_index(new_level) yield ensure Thread.current[:semantic_logger_silence] = current_index end # DEPRECATED See SemanticLogger.default_level= def self.default_level=(level) - warn "[DEPRECATION] `SemanticLogger::Logger.default_level=` is deprecated. Please use `SemanticLogger.default_level=` instead." + warn '[DEPRECATION] SemanticLogger::Logger.default_level= is deprecated. Please use SemanticLogger.default_level= instead.' SemanticLogger.default_level = level end # DEPRECATED See SemanticLogger.default_level def self.default_level - warn "[DEPRECATION] `SemanticLogger::Logger.default_level` is deprecated. Please use `SemanticLogger.default_level` instead." + warn '[DEPRECATION] SemanticLogger::Logger.default_level is deprecated. Please use SemanticLogger.default_level instead.' SemanticLogger.default_level end ############################################################################ protected @@ -258,20 +258,20 @@ # Proc: Only include log messages where the supplied Proc returns true # The Proc must return true or false def initialize(klass, level=nil, filter=nil) # Support filtering all messages to this logger using a Regular Expression # or Proc - raise ":filter must be a Regexp or Proc" unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc) + raise ':filter must be a Regexp or Proc' unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc) @filter = filter.is_a?(Regexp) ? filter.freeze : filter @name = klass.is_a?(String) ? klass : klass.name self.level = level unless level.nil? end # Write log data to underlying data storage def log(log_) - raise NotImplementedError.new("Logging Appender must implement #log(log)") + raise NotImplementedError.new('Logging Appender must implement #log(log)') end # Return the level index for fast comparisons # Returns the global default level index if the level has not been explicitly # set for this instance @@ -332,11 +332,11 @@ if exception.nil? && payload.nil? && message.kind_of?(Exception) exception = message message = exception.inspect elsif exception.nil? && payload && payload.is_a?(Exception) exception = payload - payload = nil + payload = nil end if block && (result = block.call) if result.is_a?(String) message = message.nil? ? result : "#{message} -- #{result}" @@ -355,38 +355,40 @@ log(struct) if include_message?(struct) end # Measure the supplied block and log the message def benchmark_internal(level, index, message, params, &block) - start = Time.now + start = Time.now begin if block - result = if silence_level = params[:silence] - # In case someone accidentally sets `silence: true` instead of `silence: :error` - silence_level = :error if silence_level == true - silence(silence_level) { block.call(params) } - else - block.call(params) - end + result = + if silence_level = params[:silence] + # In case someone accidentally sets `silence: true` instead of `silence: :error` + silence_level = :error if silence_level == true + silence(silence_level) { block.call(params) } + else + block.call(params) + end exception = params[:exception] result end rescue Exception => exc exception = exc ensure - end_time = Time.now + end_time = Time.now # Extract options after block completes so that block can modify any of the options log_exception = params[:log_exception] || :partial on_exception_level = params[:on_exception_level] min_duration = params[:min_duration] || 0.0 payload = params[:payload] metric = params[:metric] - duration = if block_given? - 1000.0 * (end_time - start) - else - params[:duration] || raise("Mandatory block missing when :duration option is not supplied") - end + duration = + if block_given? + 1000.0 * (end_time - start) + else + params[:duration] || raise('Mandatory block missing when :duration option is not supplied') + end # Add scoped payload if self.payload payload = payload.nil? ? self.payload : self.payload.merge(payload) end @@ -403,10 +405,10 @@ # On exception change the log level if on_exception_level level = on_exception_level index = SemanticLogger.level_to_index(level) end - message = "#{message} -- Exception: #{exception.class}: #{exception.message}" + message = "#{message} -- Exception: #{exception.class}: #{exception.message}" logged_exception = nil else # Log the message with its duration but leave out the exception that was raised logged_exception = nil end