Class: ActiveSupport::ProxyLogger
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Logger::Severity,
LoggerThreadSafeLevel,
LoggerSilence
|
|
| Inherits: | Object |
| Defined in: | activesupport/lib/active_support/proxy_logger.rb |
Overview
The proxy logger, is a logger that forwards all received logs to another logger, but has its own independent severity level.
This is useful when you want some library you have no control over to use the same logger as the rest of your application, but to have a different severity level because it is logging too much:
SomeLibrary.logger = ActiveSupport::ProxyLogger.new(Rails.logger, :error)
Almost all of the standard Logger interface is supported.
Note that the proxy logger can only suppress some logs, if the proxy severity is lower than the severity of the proxied logger, the logs won't be emitted.
LoggerSilence - Attributes & Methods
Class Method Summary
Instance Attribute Summary
-
#debug? ⇒ Boolean
readonly
Returns
trueif the log level allows entries with severityLogger::DEBUGto be written,falseotherwise. -
#error? ⇒ Boolean
readonly
Returns
trueif the log level allows entries with severityLogger::ERRORto be written,falseotherwise. -
#fatal? ⇒ Boolean
readonly
Returns
trueif the log level allows entries with severityLogger::FATALto be written,falseotherwise. -
#info? ⇒ Boolean
readonly
Returns
trueif the log level allows entries with severityLogger::INFOto be written,falseotherwise. -
#level
rw
Logging severity threshold (e.g.
-
#level=(severity)
rw
Sets the log level; returns
severity. -
#warn? ⇒ Boolean
readonly
Returns
trueif the log level allows entries with severityLogger::WARNto be written,falseotherwise.
LoggerThreadSafeLevel - self
Instance Method Summary
-
#<<(msg)
Forward the given
msgto the underlying logger with no formatting returns the number of characters written, ornilif the underlying logger isnil: -
#add(severity)
(also: #log)
Creates a log entry, which may or may not be written to the log, depending on the entry's severity and on the log level.
-
#close
Closes the logger; returns
nil: Further logs won't be emitted. -
#debug(progname = nil, &block)
readonly
Equivalent to calling #add with severity
Logger::DEBUG. -
#debug!
Sets the log level to
Logger::DEBUG. -
#error(progname = nil, &block)
readonly
Equivalent to calling #add with severity
Logger::ERROR. -
#error!
Sets the log level to
Logger::ERROR. -
#fatal(progname = nil, &block)
readonly
Equivalent to calling #add with severity
Logger::FATAL. -
#fatal!
Sets the log level to
Logger::FATAL. -
#info(progname = nil, &block)
readonly
Equivalent to calling #add with severity
Logger::INFO. -
#info!
Sets the log level to
Logger::INFO. -
#log(severity)
Alias for #add.
-
#reopen(logger)
Change the underlying logger.
-
#unknown(progname = nil, &block)
Equivalent to calling #add with severity
Logger::UNKNOWN. -
#warn(progname = nil, &block)
readonly
Equivalent to calling #add with severity
Logger::WARN. -
#warn!
Sets the log level to
Logger::WARN.
LoggerThreadSafeLevel - self
| #initialize, #initialize_copy, #level, | |
| #log_at | Change the thread-local level for the duration of the given block. |
LoggerSilence - Included
| #silence | Silences the logger for the duration of the block. |
Constructor Details
.new(logger, level = ::Logger::DEBUG) ⇒ ProxyLogger
Class Attribute Details
.silencer (rw) Also known as: #silencer
[ GitHub ]# File 'activesupport/lib/active_support/logger_silence.rb', line 12
cattr_accessor :silencer, default: true
Instance Attribute Details
#debug? ⇒ Boolean (readonly)
Returns true if the log level allows entries with severity
Logger::DEBUG to be written, false otherwise.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 62
def debug?; level <= DEBUG; end
#error? ⇒ Boolean (readonly)
Returns true if the log level allows entries with severity
Logger::ERROR to be written, false otherwise.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 83
def error?; level <= ERROR; end
#fatal? ⇒ Boolean (readonly)
Returns true if the log level allows entries with severity
Logger::FATAL to be written, false otherwise.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 90
def fatal?; level <= FATAL; end
#info? ⇒ Boolean (readonly)
Returns true if the log level allows entries with severity
Logger::INFO to be written, false otherwise.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 69
def info?; level <= INFO; end
#level (rw)
Logging severity threshold (e.g. Logger::INFO).
# File 'activesupport/lib/active_support/proxy_logger.rb', line 33
def level local_level || @level end
#level=(severity) (rw)
# File 'activesupport/lib/active_support/proxy_logger.rb', line 45
def level=(severity) @level = ::Logger::Severity.coerce(severity) end
#silencer (rw)
[ GitHub ]# File 'activesupport/lib/active_support/logger_silence.rb', line 12
cattr_accessor :silencer, default: true
#warn? ⇒ Boolean (readonly)
Returns true if the log level allows entries with severity
Logger::WARN to be written, false otherwise.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 76
def warn?; level <= WARN; end
Instance Method Details
#<<(msg)
# File 'activesupport/lib/active_support/proxy_logger.rb', line 140
def <<(msg) if @logger @logger << msg end end
#add(severity) Also known as: #log
Creates a log entry, which may or may not be written to the log, depending on the entry's severity and on the log level.
Examples:
logger = ActiveSupport::ProxyLogger.new(Logger.new($stderr), :error)
logger.add(Logger::INFO, 'Will not show')
logger.add(Logger::ERROR, 'No good')
logger.add(Logger::ERROR, 'No good', 'gnum')
Output:
E, [2022-05-12T16:25:55.349414 #36328] ERROR -- mung: No good
E, [2022-05-12T16:26:35.841134 #36328] ERROR -- gnum: No good
These convenience methods have implicit severity:
# File 'activesupport/lib/active_support/proxy_logger.rb', line 119
def add(severity, ...) severity ||= UNKNOWN if @logger && severity >= level @logger.add(severity, ...) else true end end
#close
Closes the logger; returns nil:
Further logs won't be emitted.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 51
def close @logger = nil end
#debug(progname = nil, &block) (readonly)
Equivalent to calling #add with severity Logger::DEBUG.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 147
def debug(progname = nil, &block) add(DEBUG, nil, progname, &block) end
#debug!
Sets the log level to Logger::DEBUG.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 65
def debug!; self.level = DEBUG; end
#error(progname = nil, &block) (readonly)
Equivalent to calling #add with severity Logger::ERROR.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 164
def error(progname = nil, &block) add(ERROR, nil, progname, &block) end
#error!
Sets the log level to Logger::ERROR.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 86
def error!; self.level = ERROR; end
#fatal(progname = nil, &block) (readonly)
Equivalent to calling #add with severity Logger::FATAL.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 170
def fatal(progname = nil, &block) add(FATAL, nil, progname, &block) end
#fatal!
Sets the log level to Logger::FATAL.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 93
def fatal!; self.level = FATAL; end
#info(progname = nil, &block) (readonly)
Equivalent to calling #add with severity Logger::INFO.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 152
def info(progname = nil, &block) add(INFO, nil, progname, &block) end
#info!
Sets the log level to Logger::INFO.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 72
def info!; self.level = INFO; end
#log(severity)
Alias for #add.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 127
alias_method :log, :add
#reopen(logger)
Change the underlying logger.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 56
def reopen(logger) @logger = logger end
#unknown(progname = nil, &block)
Equivalent to calling #add with severity Logger::UNKNOWN.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 176
def unknown(progname = nil, &block) add(UNKNOWN, nil, progname, &block) end
#warn(progname = nil, &block) (readonly)
Equivalent to calling #add with severity Logger::WARN.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 158
def warn(progname = nil, &block) add(WARN, nil, progname, &block) end
#warn!
Sets the log level to Logger::WARN.
# File 'activesupport/lib/active_support/proxy_logger.rb', line 79
def warn!; self.level = WARN; end