Skip to content

Instantly share code, notes, and snippets.

@simonoff
Created December 10, 2011 01:11
Show Gist options
  • Save simonoff/1454135 to your computer and use it in GitHub Desktop.
Save simonoff/1454135 to your computer and use it in GitHub Desktop.
--- central_logger/lib/central_logger/replica_set_helper.rb 2011-12-09 23:39:36.000000000 +0200
+++ mongodb_logger/lib/mongodb_logger/replica_set_helper.rb 2011-12-09 23:39:55.000000000 +0200
@@ -1,4 +1,4 @@
-module CentralLogger
+module MongodbLogger
module ReplicaSetHelper
# Use retry alg from mongodb to gobble up connection failures during replica set master vote
# Defaults to a 10 second wait
@@ -16,4 +16,4 @@
end
end
end
-end
+end
\ No newline at end of file
--- central_logger/lib/central_logger/initializer_mixin.rb 2011-12-09 23:39:36.000000000 +0200
+++ mongodb_logger/lib/mongodb_logger/initializer_mixin.rb 2011-12-09 23:39:55.000000000 +0200
@@ -1,20 +1,26 @@
-module CentralLogger
+module MongodbLogger
module InitializerMixin
- # initialization common to Rails 2.3.8 and 3.0
- def create_logger(config, path)
+
+ def rails30?
+ 3 == Rails::VERSION::MAJOR && 0 == Rails::VERSION::MINOR
+ end
+
+ def create_logger(config)
+ path = rails30? ? config.paths.log.to_a.first : config.paths['log'].first
level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
- logger = MongoLogger.new(:path => path, :level => level)
+ logger = MongodbLogger::Logger.new(:path => path, :level => level)
logger.auto_flushing = false if Rails.env.production?
logger
rescue StandardError => e
logger = ActiveSupport::BufferedLogger.new(STDERR)
logger.level = ActiveSupport::BufferedLogger::WARN
logger.warn(
- "CentralLogger Initializer Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
+ "MongodbLogger Initializer Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + "\n" +
e.message + "\n" + e.backtrace.join("\n")
)
logger
end
+
end
-end
+end
\ No newline at end of file
--- central_logger/lib/central_logger/mongo_logger.rb 2011-12-09 23:39:36.000000000 +0200
+++ mongodb_logger/lib/mongodb_logger/logger.rb 2011-12-09 23:39:55.000000000 +0200
@@ -2,16 +2,15 @@
require 'mongo'
require 'active_support'
require 'active_support/core_ext'
-require 'central_logger/replica_set_helper'
+require 'mongodb_logger/replica_set_helper'
-module CentralLogger
- class MongoLogger < ActiveSupport::BufferedLogger
+module MongodbLogger
+ class Logger < ActiveSupport::BufferedLogger
include ReplicaSetHelper
- PRODUCTION_COLLECTION_SIZE = 250.megabytes
- DEFAULT_COLLECTION_SIZE = 100.megabytes
+ DEFAULT_COLLECTION_SIZE = 250.megabytes
# Looks for configuration files in this order
- CONFIGURATION_FILES = ["central_logger.yml", "mongoid.yml", "database.yml"]
+ CONFIGURATION_FILES = ["mongodb_logger.yml", "mongoid.yml", "database.yml"]
LOG_LEVEL_SYM = [:debug, :info, :warn, :error, :fatal, :unknown]
attr_reader :db_configuration, :mongo_connection, :mongo_collection_name, :mongo_collection
@@ -20,43 +19,46 @@
path = options[:path] || File.join(Rails.root, "log/#{Rails.env}.log")
level = options[:level] || DEBUG
internal_initialize
+ rescue => e
+ # should use a config block for this
+ Rails.env.production? ? (raise e) : (puts "MongodbLogger WARNING: Using BufferedLogger due to exception: " + e.message)
+ ensure
if disable_file_logging?
- @level = level
- @buffer = {}
- @auto_flushing = 1
- @guard = Mutex.new
+ @level = level
+ @buffer = {}
+ @auto_flushing = 1
+ @guard = Mutex.new
else
super(path, level)
end
- rescue => e
- # should use a config block for this
- Rails.env.production? ? (raise e) : (puts "Using BufferedLogger due to exception: " + e.message)
end
def add_metadata(options={})
- options.each_pair do |key, value|
- unless [:messages, :request_time, :ip, :runtime, :application_name].include?(key.to_sym)
+ options.each do |key, value|
+ unless [:messages, :request_time, :ip, :runtime, :application_name, :is_exception, :params, :method].include?(key.to_sym)
@mongo_record[key] = value
else
- raise ArgumentError, ":#{key} is a reserved key for the central logger. Please choose a different key"
+ raise ArgumentError, ":#{key} is a reserved key for the mongodb logger. Please choose a different key"
end
end
end
def add(severity, message = nil, progname = nil, &block)
- if @level <= severity && message.present? && @mongo_record.present?
+ if @level && @level <= severity && message.present? && @mongo_record.present?
# do not modify the original message used by the buffered logger
msg = logging_colorized? ? message.to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip : message
@mongo_record[:messages][LOG_LEVEL_SYM[severity]] << msg
end
# may modify the original message
- disable_file_logging? ? message : super
+ disable_file_logging? ? message : (@level ? super : message)
end
# Drop the capped_collection and recreate it
def reset_collection
- @mongo_collection.drop
- create_collection
+ if @mongo_connection && @mongo_collection
+ @mongo_collection.drop
+ create_collection
+ end
end
def mongoize(options={})
@@ -69,6 +71,7 @@
runtime = Benchmark.measure{ yield }.real if block_given?
rescue Exception => e
add(3, e.message + "\n" + e.backtrace.join("\n"))
+ @mongo_record[:is_exception] = true
# Reraise the exception for anyone else who cares
raise e
ensure
@@ -94,19 +97,19 @@
connect
check_for_collection
end
-
+
def disable_file_logging?
@db_configuration.fetch('disable_file_logging', false)
end
def configure
- default_capsize = Rails.env.production? ? PRODUCTION_COLLECTION_SIZE : DEFAULT_COLLECTION_SIZE
- @mongo_collection_name = "#{Rails.env}_log"
+ default_capsize = DEFAULT_COLLECTION_SIZE
@authenticated = false
@db_configuration = {
'host' => 'localhost',
'port' => 27017,
'capsize' => default_capsize}.merge(resolve_config)
+ @mongo_collection_name = @db_configuration[:collection] || "#{Rails.env}_log"
@application_name = resolve_application_name
@safe_insert = @db_configuration['safe_insert'] || false
@@ -118,13 +121,8 @@
def resolve_application_name
if @db_configuration.has_key?('application_name')
@db_configuration['application_name']
- elsif Rails::VERSION::MAJOR >= 3
- Rails.application.class.to_s.split("::").first
else
- # rails 2 requires detective work if it's been deployed by capistrano
- # if last entry is a timestamp, go back 2 dirs (ex. /app_name/releases/20110304132847)
- path = Rails.root.to_s.split('/')
- path.length >= 4 && path.last =~ /^\d/ ? path.last(3)[0] : path.last
+ Rails.application.class.to_s.split("::").first
end
end
@@ -134,8 +132,8 @@
config_file = Rails.root.join("config", filename)
if config_file.file?
config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
- config = config['mongo'] if config.has_key?('mongo')
- break
+ config = config['mongodb_logger'] if config && config.has_key?('mongodb_logger')
+ break unless config.blank?
end
end
config
@@ -144,7 +142,8 @@
def connect
@mongo_connection ||= Mongo::Connection.new(@db_configuration['host'],
@db_configuration['port'],
- :auto_reconnect => true).db(@db_configuration['database'])
+ :auto_reconnect => true,
+ :pool_timeout => 6).db(@db_configuration['database'])
if @db_configuration['username'] && @db_configuration['password']
# the driver stores credentials in case reconnection is required
@@ -155,27 +154,22 @@
def create_collection
@mongo_connection.create_collection(@mongo_collection_name,
- {:capped => true, :size => @db_configuration['capsize'].to_i})
+ {:capped => true, :size => @db_configuration['capsize']})
end
def check_for_collection
# setup the capped collection if it doesn't already exist
- unless @mongo_connection.collection_names.include?(@mongo_collection_name)
- create_collection
- end
+ create_collection unless @mongo_connection.collection_names.include?(@mongo_collection_name)
@mongo_collection = @mongo_connection[@mongo_collection_name]
end
- def insert_log_record(safe=false)
+ def insert_log_record(safe = false)
@mongo_collection.insert(@mongo_record, :safe => safe)
end
def logging_colorized?
# Cache it since these ActiveRecord attributes are assigned after logger initialization occurs in Rails boot
- @colorized ||= Object.const_defined?(:ActiveRecord) &&
- (Rails::VERSION::MAJOR >= 3 ?
- ActiveRecord::LogSubscriber.colorize_logging :
- ActiveRecord::Base.colorize_logging)
+ @colorized ||= Object.const_defined?(:ActiveRecord) && ActiveRecord::LogSubscriber.colorize_logging
end
# force the data in the db by inspecting each top level array and hash element
@@ -190,5 +184,5 @@
pms.each { |i, j| pms[i] = j.inspect }
end
end
- end # class MongoLogger
-end
+ end
+end
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment