Class: QuietLogger
- Inherits:
-
Logger
- Object
- Logger
- QuietLogger
- Defined in:
- lib/quiet_logger.rb
Overview
Wrap a Logger in a quieter instance that can have a higher severity level filter.
Usage:
logger = Logger.new(STDOUT)
quiet_logger = QuietLogger.new(logger)
logger.debug("Stuff") # This statement is logged
quiet_logger.debug("More stuff") # This statement is not logged
quiet_logger.warn("Other stuff") # This statement logged
By default the quiet log level is "warn", but you can change to an even higher level:
QuietLogger.new(logger, level: :error)
The log level on the QuietLogger can only be made more restrictive than the level on the
underlying logger. So, if the underlying logger has a level of "info", you cannot set the
QuietLogger to "debug".
The severity level is the only thing a QuietLogger controls. Messages are written by the
underlying logger, so the output device and the formatter used to render messages are the ones
on that logger. Reading or setting formatter therefore reads or sets it on the wrapped logger.
The progname and datetime format used for output are the wrapped logger's as well, but setting
them on the QuietLogger has no effect on the output.
If the wrapped logger supports tagged logging (i.e. ActiveSupport::TaggedLogging), then the
tagging methods are available on the QuietLogger as well. Tags are stored on the wrapped
logger, so they apply to messages logged with either logger.
quiet_logger = QuietLogger.new(Rails.logger)
quiet_logger.tagged("ActionCable") { quiet_logger.warn("Other stuff") }
Defined Under Namespace
Modules: Tagging
Instance Method Summary collapse
- #<<(msg) ⇒ Object private
- #add(severity, message = nil, progname = nil, &block) ⇒ Object (also: #log) private
- #close ⇒ Object private
-
#formatter ⇒ Logger::Formatter
A
QuietLoggernever formats messages itself; the wrapped logger does. -
#formatter=(value) ⇒ Object
Set the formatter on the wrapped logger since that is the logger that formats the messages.
-
#initialize(logger, level: Logger::WARN) ⇒ QuietLogger
constructor
Wrap a logger to have a higher log level.
-
#level ⇒ Integer
(also: #sev_threshold)
Returns the log level.
- #reopen(logdev = nil) ⇒ Object private
-
#silence(severity = Logger::ERROR) ⇒ Object
Silence the logger for the duration of the block by temporarily raising the severity level.
Constructor Details
#initialize(logger, level: Logger::WARN) ⇒ QuietLogger
Wrap a logger to have a higher log level.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quiet_logger.rb', line 76 def initialize(logger, level: Logger::WARN) unless logger.respond_to?(:add) && logger.respond_to?(:level) raise ArgumentError, "logger must respond to :add and :level; got #{logger.inspect}" end # Passing a nil log device sets up the rest of the Logger internals without opening one of # our own; messages are always written by the wrapped logger. The wrapped logger is not # set until after this call so that the attributes the constructor initializes cannot be # written through to it. super(nil, level: level) @logger = logger extend(Tagging) if defined?(ActiveSupport::TaggedLogging) && logger.is_a?(ActiveSupport::TaggedLogging) end |
Instance Method Details
#<<(msg) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
136 137 138 |
# File 'lib/quiet_logger.rb', line 136 def <<(msg) @logger << msg end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
126 127 128 129 130 131 132 |
# File 'lib/quiet_logger.rb', line 126 def add(severity, = nil, progname = nil, &block) severity ||= UNKNOWN if severity < level return true end @logger.add(severity, , progname, &block) end |
#close ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
141 142 143 |
# File 'lib/quiet_logger.rb', line 141 def close # no-op end |
#formatter ⇒ Logger::Formatter
A QuietLogger never formats messages itself; the wrapped logger does. The tagged logging
API is built on top of the formatter, so this has to report the wrapped logger's formatter.
104 105 106 |
# File 'lib/quiet_logger.rb', line 104 def formatter @logger.formatter end |
#formatter=(value) ⇒ Object
Set the formatter on the wrapped logger since that is the logger that formats the messages.
109 110 111 112 |
# File 'lib/quiet_logger.rb', line 109 def formatter=(value) # The wrapped logger is not set yet when this is called from the Logger constructor. @logger.formatter = value if @logger end |
#level ⇒ Integer Also known as: sev_threshold
Returns the log level
92 93 94 |
# File 'lib/quiet_logger.rb', line 92 def level [super, @logger.level].max end |
#reopen(logdev = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
146 147 148 |
# File 'lib/quiet_logger.rb', line 146 def reopen(logdev = nil) # no-op end |
#silence(severity = Logger::ERROR) ⇒ Object
Silence the logger for the duration of the block by temporarily raising the severity level. Provided for compatibility with ActiveSupport loggers. The level is still constrained by the level of the wrapped logger.
Only defined if the logger gem supports with_level (logger >= 1.5).
120 121 122 |
# File 'lib/quiet_logger.rb', line 120 def silence(severity = Logger::ERROR) with_level(severity) { yield self } end |