Module: Yarder::TaggedLogging

Defined in:
lib/yarder/tagged_logging.rb

Overview

Wraps any standard Logger object to provide tagging capabilities.

logger = Yarder::TaggedLogging.new(Logger.new(STDOUT)) logger.tagged(‘BCX’) { logger.info ‘Stuff’ } # Adds BCX to the @tags array and “Stuff” to the @message logger.tagged(‘BCX’, “Jason”) { logger.info ‘Stuff’ } # Adds ‘BCX’ and ‘Jason’ to the @tags array and “Stuff” to the @message logger.tagged(‘BCX’) { logger.tagged(‘Jason’) { logger.info ‘Stuff’ } } # Adds ‘BCX’ and ‘Jason’ to the @tags array and “Stuff” to the @message

This is used by the default Rails.logger when the Yarder gem is added to a rails application to make it easy to stamp JSON logs with subdomains, request ids, and anything else to aid debugging of multi-user production applications.

Defined Under Namespace

Modules: Formatter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(logger) ⇒ Object



85
86
87
88
89
90
# File 'lib/yarder/tagged_logging.rb', line 85

def self.new(logger)
  # Ensure we set a default formatter so we aren't extending nil!
  logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
  logger.formatter.extend Formatter
  logger.extend(self)
end

Instance Method Details

#flushObject



98
99
100
101
# File 'lib/yarder/tagged_logging.rb', line 98

def flush
  clear_tags!
  super if defined?(super)
end

#tagged(*tags) ⇒ Object



94
95
96
# File 'lib/yarder/tagged_logging.rb', line 94

def tagged(*tags)
  formatter.tagged(*tags) { yield self }
end