123456789_123456789_123456789_123456789_123456789_

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)

It can also ignore individual messages matching given patterns, when a library is only noisy on some logs your application doesn't care about:

SomeLibrary.logger = ActiveSupport::ProxyLogger.new(Rails.logger).ignore(/Noisy/)

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 true if the log level allows entries with severity Logger::DEBUG to be written, false otherwise.

  • #error? ⇒ Boolean readonly

    Returns true if the log level allows entries with severity Logger::ERROR to be written, false otherwise.

  • #fatal? ⇒ Boolean readonly

    Returns true if the log level allows entries with severity Logger::FATAL to be written, false otherwise.

  • #info? ⇒ Boolean readonly

    Returns true if the log level allows entries with severity Logger::INFO to be written, false otherwise.

  • #level rw

    Logging severity threshold (e.g.

  • #level=(severity) rw

    Sets the log level; returns severity.

  • #warn? ⇒ Boolean readonly

    Returns true if the log level allows entries with severity Logger::WARN to be written, false otherwise.

LoggerThreadSafeLevel - self

Instance Method Summary

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 31

def initialize(logger, level = ::Logger::DEBUG)
  super()
  @logger = logger
  @level = ::Logger::Severity.coerce(level)
  @ignored_patterns = []
  @ignored = nil
end

Class Attribute Details

.silencer (rw) Also known as: #silencer

[ GitHub ]

  
# File 'activesupport/lib/active_support/logger_silence.rb', line 13

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 89

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 110

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 117

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 96

def info?; level <= INFO; end

#level (rw)

Logging severity threshold (e.g. Logger::INFO).

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 60

def level
  local_level || @level
end

#level=(severity) (rw)

Sets the log level; returns severity.

Argument severity may be an integer, a string, or a symbol:

logger.level = Logger::ERROR # => 3
logger.level = 3             # => 3
logger.level = 'error'       # => "error"
logger.level = :error        # => :error
[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 72

def level=(severity)
  @level = ::Logger::Severity.coerce(severity)
end

#silencer (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/logger_silence.rb', line 13

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 103

def warn?; level <= WARN; end

Instance Method Details

#<<(msg)

Forward the given msg to the underlying logger with no formatting returns the number of characters written, or nil if the underlying logger is nil:

logger = ProxyLogger.new(Logger.new($stderr))
logger << 'My message.' # => 10

Output:

My message.
[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 180

def <<(msg)
  if @logger
    @logger << msg
  end
end

#add(severity, message = nil, progname = nil, &block) 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:

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 146

def add(severity, message = nil, progname = nil, &block)
  severity ||= UNKNOWN
  return true unless @logger && severity >= level

  if @ignored
    if message.nil?
      if block_given?
        message = yield
        block = nil
      else
        message = progname
        progname = nil
      end
    end

    text = message.to_s
    return true if text.valid_encoding? && @ignored.match?(text)
  end

  @logger.add(severity, message, progname, &block)
end

#close

Closes the logger; returns nil: Further logs won't be emitted.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 78

def close
  @logger = nil
end

#debug(progname = nil, &block) (readonly)

Equivalent to calling #add with severity Logger::DEBUG.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 187

def debug(progname = nil, &block)
  add(DEBUG, nil, progname, &block)
end

#debug!

Sets the log level to Logger::DEBUG.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 92

def debug!; self.level = DEBUG; end

#error(progname = nil, &block) (readonly)

Equivalent to calling #add with severity Logger::ERROR.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 204

def error(progname = nil, &block)
  add(ERROR, nil, progname, &block)
end

#error!

Sets the log level to Logger::ERROR.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 113

def error!; self.level = ERROR; end

#fatal(progname = nil, &block) (readonly)

Equivalent to calling #add with severity Logger::FATAL.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 210

def fatal(progname = nil, &block)
  add(FATAL, nil, progname, &block)
end

#fatal!

Sets the log level to Logger::FATAL.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 120

def fatal!; self.level = FATAL; end

#ignore(*patterns)

Registers patterns of messages to ignore. Ignored messages aren't forwarded to the underlying logger, whatever their severity. Returns self, so it can be chained on the constructor.

Regexps are matched against the message, strings are matched literally:

logger.ignore(/Noisy/, 'Also noisy')
logger.error('Noisy message') # not forwarded
logger.error('Also noisy')    # not forwarded

Patterns are matched against message.to_s, so lazily generated messages are evaluated even when they end up ignored.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 51

def ignore(*patterns)
  return self if patterns.empty?

  @ignored_patterns.concat(patterns)
  @ignored = Regexp.union(@ignored_patterns)
  self
end

#info(progname = nil, &block) (readonly)

Equivalent to calling #add with severity Logger::INFO.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 192

def info(progname = nil, &block)
  add(INFO, nil, progname, &block)
end

#info!

Sets the log level to Logger::INFO.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 99

def info!; self.level = INFO; end

#log(severity, message = nil, progname = nil, &block)

Alias for #add.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 167

alias_method :log, :add

#reopen(logger)

Change the underlying logger.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 83

def reopen(logger)
  @logger = logger
end

#unknown(progname = nil, &block)

Equivalent to calling #add with severity Logger::UNKNOWN.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 216

def unknown(progname = nil, &block)
  add(UNKNOWN, nil, progname, &block)
end

#warn(progname = nil, &block) (readonly)

Equivalent to calling #add with severity Logger::WARN.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 198

def warn(progname = nil, &block)
  add(WARN, nil, progname, &block)
end

#warn!

Sets the log level to Logger::WARN.

[ GitHub ]

  
# File 'activesupport/lib/active_support/proxy_logger.rb', line 106

def warn!; self.level = WARN; end