lib/semantic_logger/base.rb in semantic_logger-2.12.0 vs lib/semantic_logger/base.rb in semantic_logger-2.13.0

- old
+ new

@@ -179,10 +179,54 @@ # are not understood by the majority of the logging providers # Map it to :error alias :unknown :error alias :unknown? :error? + # Silence noisy log levels by changing the default_level within the block + # + # This setting is thread-safe and only applies to the current thread + # + # Any threads spawned within the block will not be affected by this setting + # + # #silence can be used to both raise and lower the log level within + # the supplied block. + # + # Example: + # + # # Perform trace level logging within the block when the default is higher + # SemanticLogger.default_level = :info + # + # logger.debug 'this will _not_ be logged' + # + # logger.silence(:trace) do + # logger.debug "this will be logged" + # end + # + # Parameters + # new_level + # The new log level to apply within the block + # Default: :error + # + # Example: + # # Silence all logging below :error level + # logger.silence do + # logger.info "this will _not_ be logged" + # logger.warn "this neither" + # logger.error "but errors will be logged" + # end + # + # 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] + 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." SemanticLogger.default_level = level end @@ -310,12 +354,20 @@ # Measure the supplied block and log the message def benchmark_internal(level, index, message, params, &block) start = Time.now begin - rc = block.call(params) if block - exception = params[:exception] - rc + 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 + exception = params[:exception] + result + end rescue Exception => exc exception = exc ensure end_time = Time.now # Extract options after block completes so that block can modify any of the options