Class: ActiveSupport::TaggedLogging
- Defined in:
- activesupport/lib/active_support/tagged_logging.rb
Overview
Wraps any standard Logger class to provide tagging capabilities. Examples:
Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
Logger.tagged("BCX") { Logger.info "Stuff" } # Logs "[BCX] Stuff"
Logger.tagged("BCX", "Jason") { Logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines with subdomains, request ids, and anything else to aid debugging of multi-user production applications.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #flush ⇒ Object
-
#initialize(logger) ⇒ TaggedLogging
constructor
A new instance of TaggedLogging.
- #method_missing(method, *args) ⇒ Object
- #silence(temporary_level = Logger::ERROR, &block) ⇒ Object
- #tagged(*new_tags) ⇒ Object
Constructor Details
#initialize(logger) ⇒ TaggedLogging
Returns a new instance of TaggedLogging.
16 17 18 19 |
# File 'activesupport/lib/active_support/tagged_logging.rb', line 16 def initialize(logger) @logger = logger @tags = Hash.new { |h,k| h[k] = [] } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
53 54 55 |
# File 'activesupport/lib/active_support/tagged_logging.rb', line 53 def method_missing(method, *args) @logger.send(method, *args) end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
35 36 37 38 |
# File 'activesupport/lib/active_support/tagged_logging.rb', line 35 def add(severity, = nil, progname = nil, &block) = (block_given? ? block.call : progname) if .nil? @logger.add(severity, "#{}#{}", progname) end |
#flush ⇒ Object
48 49 50 51 |
# File 'activesupport/lib/active_support/tagged_logging.rb', line 48 def flush @tags.delete(Thread.current) @logger.flush if @logger.respond_to?(:flush) end |