mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add ActiveSupport::Logger#silence that works the same as the old Logger#silence extension
This commit is contained in:
parent
e0176bf732
commit
f914454f3a
5 changed files with 45 additions and 20 deletions
|
@ -1,5 +1,9 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* Add ActiveSupport::Logger#silence that works the same as the old Logger#silence extension.
|
||||
|
||||
*DHH*
|
||||
|
||||
* Remove surrogate unicode character encoding from `ActiveSupport::JSON.encode`
|
||||
The encoding scheme was broken for unicode characters outside the basic multilingual plane;
|
||||
since json is assumed to be `UTF-8`, and we already force the encoding to `UTF-8`,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/deprecation'
|
||||
require 'active_support/logger_silence'
|
||||
|
||||
ActiveSupport::Deprecation.warn 'this file is deprecated and will be removed'
|
||||
|
||||
|
@ -31,27 +32,9 @@ require 'logger'
|
|||
#
|
||||
# logger.datetime_format = "%Y-%m-%d"
|
||||
#
|
||||
# Note: This logger is deprecated in favor of ActiveSupport::Logger
|
||||
# Note: This logger is deprecated in favor of ActiveSupport::BufferedLogger
|
||||
class Logger
|
||||
##
|
||||
# :singleton-method:
|
||||
# Set to false to disable the silencer
|
||||
cattr_accessor :silencer
|
||||
self.silencer = true
|
||||
|
||||
# Silences the logger for the duration of the block.
|
||||
def silence(temporary_level = Logger::ERROR)
|
||||
if silencer
|
||||
begin
|
||||
old_logger_level, self.level = level, temporary_level
|
||||
yield self
|
||||
ensure
|
||||
self.level = old_logger_level
|
||||
end
|
||||
else
|
||||
yield self
|
||||
end
|
||||
end
|
||||
include LoggerSilence
|
||||
|
||||
alias :old_datetime_format= :datetime_format=
|
||||
# Logging date-time format (string passed to +strftime+). Ignored if the formatter
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/logger_silence'
|
||||
require 'logger'
|
||||
|
||||
module ActiveSupport
|
||||
class Logger < ::Logger
|
||||
include LoggerSilence
|
||||
|
||||
# Broadcasts logs to multiple loggers.
|
||||
def self.broadcast(logger) # :nodoc:
|
||||
Module.new do
|
||||
|
|
24
activesupport/lib/active_support/logger_silence.rb
Normal file
24
activesupport/lib/active_support/logger_silence.rb
Normal file
|
@ -0,0 +1,24 @@
|
|||
require 'active_support/concern'
|
||||
|
||||
module LoggerSilence
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
cattr_accessor :silencer
|
||||
self.silencer = true
|
||||
end
|
||||
|
||||
# Silences the logger for the duration of the block.
|
||||
def silence(temporary_level = Logger::ERROR)
|
||||
if silencer
|
||||
begin
|
||||
old_logger_level, self.level = level, temporary_level
|
||||
yield self
|
||||
ensure
|
||||
self.level = old_logger_level
|
||||
end
|
||||
else
|
||||
yield self
|
||||
end
|
||||
end
|
||||
end
|
|
@ -120,4 +120,14 @@ class LoggerTest < ActiveSupport::TestCase
|
|||
byte_string.force_encoding("ASCII-8BIT")
|
||||
assert byte_string.include?(BYTE_STRING)
|
||||
end
|
||||
|
||||
def test_silencing_everything_but_errors
|
||||
@logger.silence do
|
||||
@logger.debug "NOT THERE"
|
||||
@logger.error "THIS IS HERE"
|
||||
end
|
||||
|
||||
assert !@output.string.include?("NOT THERE")
|
||||
assert @output.string.include?("THIS IS HERE")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue