Module: RailsLogstasher::TaggedLogging

Defined in:
lib/rails-logstasher/tagged_logging.rb

Overview

Wraps any standard Logger object to provide tagging capabilities.

logger = RailsLogstasher::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 RailsLogstasher 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, log_type, log_entry_processor = nil) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/rails-logstasher/tagged_logging.rb', line 101

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

Instance Method Details

#flushObject



116
117
118
119
# File 'lib/rails-logstasher/tagged_logging.rb', line 116

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

#tagged(*tags) ⇒ Object



112
113
114
# File 'lib/rails-logstasher/tagged_logging.rb', line 112

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