mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
05ad44eb89
- Following the Rails guide which state that a logger needs to include the `ActiveSupport::LoggerSilence` as well as `ActiveSupport::LoggerThreadSafe` modules isn't enough and won't work. Here is a test cases with 3 tests that all fails https://gist.github.com/Edouard-chin/4a72930c2b1eafbbd72a80c66f102010 The problems are the following: 1) The logger needs to call `after_initialize` in order to setup some instance variables. 2) The silence doesn't actually work because the bare ruby Logger `add` method checks for the instance variable `@logger`. We need to override the `add` (like we used to in the ActiveSupport::Logger class). 3) Calling `debug?` `info?` etc... doesn't work as the bare ruby methods will check for the instance variable. Again we need to override this methods (like we used to in the ActiveSupport::Logger class) The LoggerSilence won't work without LoggerThreadSafe, but the later is not public API, the user shouldn't have to include it so I modified to include it automatically. Same for the `after_initialize` method. I find unuintitive to have to call it directly. I modified to instance the variables when the module get included.
93 lines
2.4 KiB
Ruby
93 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "active_support/logger_silence"
|
|
require "active_support/logger_thread_safe_level"
|
|
require "logger"
|
|
|
|
module ActiveSupport
|
|
class Logger < ::Logger
|
|
include LoggerSilence
|
|
|
|
# Returns true if the logger destination matches one of the sources
|
|
#
|
|
# logger = Logger.new(STDOUT)
|
|
# ActiveSupport::Logger.logger_outputs_to?(logger, STDOUT)
|
|
# # => true
|
|
def self.logger_outputs_to?(logger, *sources)
|
|
logdev = logger.instance_variable_get("@logdev")
|
|
logger_source = logdev.dev if logdev.respond_to?(:dev)
|
|
sources.any? { |source| source == logger_source }
|
|
end
|
|
|
|
# Broadcasts logs to multiple loggers.
|
|
def self.broadcast(logger) # :nodoc:
|
|
Module.new do
|
|
define_method(:add) do |*args, &block|
|
|
logger.add(*args, &block)
|
|
super(*args, &block)
|
|
end
|
|
|
|
define_method(:<<) do |x|
|
|
logger << x
|
|
super(x)
|
|
end
|
|
|
|
define_method(:close) do
|
|
logger.close
|
|
super()
|
|
end
|
|
|
|
define_method(:progname=) do |name|
|
|
logger.progname = name
|
|
super(name)
|
|
end
|
|
|
|
define_method(:formatter=) do |formatter|
|
|
logger.formatter = formatter
|
|
super(formatter)
|
|
end
|
|
|
|
define_method(:level=) do |level|
|
|
logger.level = level
|
|
super(level)
|
|
end
|
|
|
|
define_method(:local_level=) do |level|
|
|
logger.local_level = level if logger.respond_to?(:local_level=)
|
|
super(level) if respond_to?(:local_level=)
|
|
end
|
|
|
|
define_method(:silence) do |level = Logger::ERROR, &block|
|
|
if logger.respond_to?(:silence)
|
|
logger.silence(level) do
|
|
if defined?(super)
|
|
super(level, &block)
|
|
else
|
|
block.call(self)
|
|
end
|
|
end
|
|
else
|
|
if defined?(super)
|
|
super(level, &block)
|
|
else
|
|
block.call(self)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
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
|