mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
* BufferedLogger is deprecated. Use ActiveSupport::Logger, or the logger
from Ruby stdlib.
This commit is contained in:
parent
9d6e52b55e
commit
572c3d5178
17 changed files with 50 additions and 180 deletions
|
@ -1,4 +1,3 @@
|
|||
require "active_support/core_ext/logger"
|
||||
require "active_support/benchmarkable"
|
||||
|
||||
module AbstractController
|
||||
|
|
|
@ -10,7 +10,6 @@ require 'zlib'
|
|||
require 'active_support/dependencies'
|
||||
require 'active_support/core_ext/array/wrap'
|
||||
require 'active_support/core_ext/object/blank'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'active_support/ordered_hash'
|
||||
require 'active_record/fixtures/file'
|
||||
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
## Rails 4.0.0 (unreleased) ##
|
||||
|
||||
* BufferedLogger is deprecated. Use ActiveSupport::Logger, or the logger
|
||||
from Ruby stdlib.
|
||||
|
||||
## Rails 3.2.0 (unreleased) ##
|
||||
|
||||
* Added Enumerable#pluck to wrap the common pattern of collect(&:method) *DHH*
|
||||
|
|
|
@ -38,6 +38,7 @@ end
|
|||
|
||||
require "active_support/dependencies/autoload"
|
||||
require "active_support/version"
|
||||
require "active_support/logger"
|
||||
|
||||
module ActiveSupport
|
||||
extend ActiveSupport::Autoload
|
||||
|
@ -53,7 +54,6 @@ module ActiveSupport
|
|||
autoload :Base64
|
||||
autoload :BasicObject
|
||||
autoload :Benchmarkable
|
||||
autoload :BufferedLogger
|
||||
autoload :Cache
|
||||
autoload :Callbacks
|
||||
autoload :Concern
|
||||
|
|
|
@ -1,125 +1,10 @@
|
|||
require 'thread'
|
||||
require 'logger'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/deprecation'
|
||||
require 'active_support/logger'
|
||||
require 'fileutils'
|
||||
|
||||
module ActiveSupport
|
||||
# Inspired by the buffered logger idea by Ezra
|
||||
class BufferedLogger
|
||||
module Severity
|
||||
DEBUG = 0
|
||||
INFO = 1
|
||||
WARN = 2
|
||||
ERROR = 3
|
||||
FATAL = 4
|
||||
UNKNOWN = 5
|
||||
end
|
||||
include Severity
|
||||
|
||||
MAX_BUFFER_SIZE = 1000
|
||||
|
||||
##
|
||||
# :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 = ERROR)
|
||||
if silencer
|
||||
begin
|
||||
logger = self.class.new @log_dest, temporary_level
|
||||
yield logger
|
||||
ensure
|
||||
logger.close
|
||||
end
|
||||
else
|
||||
yield self
|
||||
end
|
||||
end
|
||||
deprecate :silence
|
||||
|
||||
attr_reader :auto_flushing
|
||||
deprecate :auto_flushing
|
||||
|
||||
def initialize(log, level = DEBUG)
|
||||
@level = level
|
||||
@log_dest = log
|
||||
|
||||
unless log.respond_to?(:write)
|
||||
unless File.exist?(File.dirname(log))
|
||||
ActiveSupport::Deprecation.warn(<<-eowarn)
|
||||
Automatic directory creation for '#{log}' is deprecated. Please make sure the directory for your log file exists before creating the logger.
|
||||
eowarn
|
||||
FileUtils.mkdir_p(File.dirname(log))
|
||||
end
|
||||
end
|
||||
|
||||
@log = open_logfile log
|
||||
end
|
||||
|
||||
def open_log(log, mode)
|
||||
open(log, mode).tap do |open_log|
|
||||
open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding)
|
||||
open_log.sync = true
|
||||
end
|
||||
end
|
||||
deprecate :open_log
|
||||
|
||||
def level
|
||||
@log.level
|
||||
end
|
||||
|
||||
def level=(l)
|
||||
@log.level = l
|
||||
end
|
||||
|
||||
def add(severity, message = nil, progname = nil, &block)
|
||||
@log.add(severity, message, progname, &block)
|
||||
end
|
||||
|
||||
# Dynamically add methods such as:
|
||||
# def info
|
||||
# def warn
|
||||
# def debug
|
||||
Severity.constants.each do |severity|
|
||||
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
||||
def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
|
||||
add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
|
||||
end # end
|
||||
|
||||
def #{severity.downcase}? # def debug?
|
||||
#{severity} >= level # DEBUG >= @level
|
||||
end # end
|
||||
EOT
|
||||
end
|
||||
|
||||
# Set the auto-flush period. Set to true to flush after every log message,
|
||||
# to an integer to flush every N messages, or to false, nil, or zero to
|
||||
# never auto-flush. If you turn auto-flushing off, be sure to regularly
|
||||
# flush the log yourself -- it will eat up memory until you do.
|
||||
def auto_flushing=(period)
|
||||
end
|
||||
deprecate :auto_flushing=
|
||||
|
||||
def flush
|
||||
end
|
||||
deprecate :flush
|
||||
|
||||
def respond_to?(method, include_private = false)
|
||||
return false if method.to_s == "flush"
|
||||
super
|
||||
end
|
||||
|
||||
def close
|
||||
@log.close
|
||||
end
|
||||
|
||||
private
|
||||
def open_logfile(log)
|
||||
Logger.new log
|
||||
end
|
||||
end
|
||||
BufferedLogger = ActiveSupport::Deprecation::DeprecatedConstantProxy.new(
|
||||
'BufferedLogger', '::ActiveSupport::Logger')
|
||||
end
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
|
||||
next if File.basename(path, '.rb') == 'logger'
|
||||
require "active_support/core_ext/#{File.basename(path, '.rb')}"
|
||||
end
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/deprecation'
|
||||
|
||||
ActiveSupport::Deprecation.warn 'this file is deprecated and will be removed'
|
||||
|
||||
# Adds the 'around_level' method to Logger.
|
||||
class Logger #:nodoc:
|
||||
|
|
|
@ -50,7 +50,7 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
class MockLogger
|
||||
include ActiveSupport::BufferedLogger::Severity
|
||||
include ActiveSupport::Logger::Severity
|
||||
|
||||
attr_reader :flush_count
|
||||
attr_accessor :level
|
||||
|
@ -73,7 +73,7 @@ module ActiveSupport
|
|||
@flush_count += 1
|
||||
end
|
||||
|
||||
ActiveSupport::BufferedLogger::Severity.constants.each do |severity|
|
||||
ActiveSupport::Logger::Severity.constants.each do |severity|
|
||||
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
||||
def #{severity.downcase}?
|
||||
#{severity} >= @level
|
||||
|
|
18
activesupport/lib/active_support/logger.rb
Normal file
18
activesupport/lib/active_support/logger.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
require 'logger'
|
||||
|
||||
module ActiveSupport
|
||||
class Logger < ::Logger
|
||||
def initialize(*args)
|
||||
super
|
||||
@formatter = SimpleFormatter.new
|
||||
end
|
||||
|
||||
# Simple formatter which only displays the message.
|
||||
class SimpleFormatter < ::Logger::Formatter
|
||||
# This method is invoked when a log event occurs
|
||||
def call(severity, timestamp, progname, msg)
|
||||
"#{String === msg ? msg : msg.inspect}\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -19,7 +19,7 @@ class BenchmarkableTest < ActiveSupport::TestCase
|
|||
|
||||
def setup
|
||||
@buffer = Buffer.new
|
||||
@logger = ActiveSupport::BufferedLogger.new(@buffer)
|
||||
@logger = ActiveSupport::Logger.new(@buffer)
|
||||
end
|
||||
|
||||
def test_without_block
|
||||
|
@ -42,17 +42,17 @@ class BenchmarkableTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_within_level
|
||||
logger.level = ActiveSupport::BufferedLogger::DEBUG
|
||||
logger.level = ActiveSupport::Logger::DEBUG
|
||||
benchmark('included_debug_run', :level => :debug) { }
|
||||
assert_last_logged 'included_debug_run'
|
||||
end
|
||||
|
||||
def test_outside_level
|
||||
logger.level = ActiveSupport::BufferedLogger::ERROR
|
||||
logger.level = ActiveSupport::Logger::ERROR
|
||||
benchmark('skipped_debug_run', :level => :debug) { }
|
||||
assert_no_match(/skipped_debug_run/, buffer.last)
|
||||
ensure
|
||||
logger.level = ActiveSupport::BufferedLogger::DEBUG
|
||||
logger.level = ActiveSupport::Logger::DEBUG
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -10,7 +10,7 @@ class BufferedLoggerTest < Test::Unit::TestCase
|
|||
include MultibyteTestHelpers
|
||||
include ActiveSupport::Testing::Deprecation
|
||||
|
||||
Logger = ActiveSupport::BufferedLogger
|
||||
Logger = ActiveSupport::Logger
|
||||
|
||||
def setup
|
||||
@message = "A debug message"
|
||||
|
@ -113,21 +113,12 @@ class BufferedLoggerTest < Test::Unit::TestCase
|
|||
|
||||
def test_should_know_if_its_loglevel_is_below_a_given_level
|
||||
Logger::Severity.constants.each do |level|
|
||||
next if level.to_s == 'UNKNOWN'
|
||||
@logger.level = Logger::Severity.const_get(level) - 1
|
||||
assert @logger.send("#{level.downcase}?"), "didn't know if it was #{level.downcase}? or below"
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_create_the_log_directory_if_it_doesnt_exist
|
||||
tmp_directory = File.join(File.dirname(__FILE__), "tmp")
|
||||
log_file = File.join(tmp_directory, "development.log")
|
||||
FileUtils.rm_rf(tmp_directory)
|
||||
assert_deprecated do
|
||||
@logger = Logger.new(log_file)
|
||||
end
|
||||
assert File.exist?(tmp_directory)
|
||||
end
|
||||
|
||||
def test_buffer_multibyte
|
||||
@logger.info(UNICODE_STRING)
|
||||
@logger.info(BYTE_STRING)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
require 'abstract_unit'
|
||||
require 'stringio'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'active_support/logger'
|
||||
|
||||
class CleanLoggerTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@out = StringIO.new
|
||||
@logger = Logger.new(@out)
|
||||
@logger = ActiveSupport::Logger.new(@out)
|
||||
end
|
||||
|
||||
def test_format_message
|
||||
|
@ -13,40 +13,11 @@ class CleanLoggerTest < Test::Unit::TestCase
|
|||
assert_equal "error\n", @out.string
|
||||
end
|
||||
|
||||
def test_silence
|
||||
# Without yielding self.
|
||||
@logger.silence do
|
||||
@logger.debug 'debug'
|
||||
@logger.info 'info'
|
||||
@logger.warn 'warn'
|
||||
@logger.error 'error'
|
||||
@logger.fatal 'fatal'
|
||||
end
|
||||
|
||||
# Yielding self.
|
||||
@logger.silence do |logger|
|
||||
logger.debug 'debug'
|
||||
logger.info 'info'
|
||||
logger.warn 'warn'
|
||||
logger.error 'error'
|
||||
logger.fatal 'fatal'
|
||||
end
|
||||
|
||||
# Silencer off.
|
||||
Logger.silencer = false
|
||||
@logger.silence do |logger|
|
||||
logger.warn 'unsilenced'
|
||||
end
|
||||
Logger.silencer = true
|
||||
|
||||
assert_equal "error\nfatal\nerror\nfatal\nunsilenced\n", @out.string
|
||||
end
|
||||
|
||||
def test_datetime_format
|
||||
@logger.formatter = Logger::Formatter.new
|
||||
@logger.datetime_format = "%Y-%m-%d"
|
||||
@logger.formatter.datetime_format = "%Y-%m-%d"
|
||||
@logger.debug 'debug'
|
||||
assert_equal "%Y-%m-%d", @logger.datetime_format
|
||||
assert_equal "%Y-%m-%d", @logger.formatter.datetime_format
|
||||
assert_match(/D, \[\d\d\d\d-\d\d-\d\d#\d+\] DEBUG -- : debug/, @out.string)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
require 'abstract_unit'
|
||||
require 'active_support/core_ext/logger'
|
||||
require 'active_support/logger'
|
||||
require 'active_support/tagged_logging'
|
||||
|
||||
class TaggedLoggingTest < ActiveSupport::TestCase
|
||||
class MyLogger < ::Logger
|
||||
class MyLogger < ::ActiveSupport::Logger
|
||||
def flush(*)
|
||||
info "[FLUSHED]"
|
||||
end
|
||||
|
|
|
@ -5,7 +5,6 @@ require 'pathname'
|
|||
require 'active_support'
|
||||
require 'active_support/core_ext/kernel/reporting'
|
||||
require 'active_support/core_ext/array/extract_options'
|
||||
require 'active_support/core_ext/logger'
|
||||
|
||||
require 'rails/application'
|
||||
require 'rails/version'
|
||||
|
|
|
@ -33,13 +33,13 @@ module Rails
|
|||
f.sync = !Rails.env.production? # make sure every write flushes
|
||||
|
||||
logger = ActiveSupport::TaggedLogging.new(
|
||||
ActiveSupport::BufferedLogger.new(f)
|
||||
ActiveSupport::Logger.new(f)
|
||||
)
|
||||
logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
|
||||
logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase)
|
||||
logger
|
||||
rescue StandardError
|
||||
logger = ActiveSupport::TaggedLogging.new(ActiveSupport::BufferedLogger.new(STDERR))
|
||||
logger.level = ActiveSupport::BufferedLogger::WARN
|
||||
logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDERR))
|
||||
logger.level = ActiveSupport::Logger::WARN
|
||||
logger.warn(
|
||||
"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
|
||||
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
ActionController::Base.perform_caching = true
|
||||
ActiveSupport::Dependencies.mechanism = :require
|
||||
Rails.logger.level = ActiveSupport::BufferedLogger::INFO
|
||||
Rails.logger.level = ActiveSupport::Logger::INFO
|
||||
|
|
|
@ -5,7 +5,6 @@ require 'test/unit'
|
|||
require 'fileutils'
|
||||
|
||||
require 'active_support'
|
||||
require 'active_support/core_ext/logger'
|
||||
|
||||
require 'action_controller'
|
||||
require 'rails/all'
|
||||
|
|
Loading…
Reference in a new issue