Permalink
Browse files

initial import of the MongoDBLogger plugin

git-svn-id: https://svn.vitalbook.com/svn/plugins/trunk/mongo_db_logger@27836 882a0cfe-dafd-0310-a90d-f8c45283ee93
  • Loading branch information...
0 parents commit aaaed6f382f690b51dbe23ddea3c13314ef92141 phil committed Sep 24, 2009
Showing with 145 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +13 −0 README
  3. +23 −0 Rakefile
  4. +2 −0 init.rb
  5. 0 install.rb
  6. +12 −0 lib/mongo_db_logger.rb
  7. +59 −0 lib/mongo_logger_modifications.rb
  8. +4 −0 tasks/mongo_db_logger_tasks.rake
  9. +8 −0 test/mongo_db_logger_test.rb
  10. +3 −0 test/test_helper.rb
  11. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+MongoDbLogger
+=============
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the mongo_db_logger plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the mongo_db_logger plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'MongoDbLogger'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+# Include hook code here
+require 'mongo_db_logger'
No changes.
@@ -0,0 +1,12 @@
+require 'mongo'
+module MongoDBLogger
+ def self.included(base)
+ ActiveSupport::BufferedLogger.send(:include, MongoLoggerModifications)
+ base.class_eval { around_filter :use_mongodb_logger }
+ end
+
+ def use_mongodb_logger
+ # return yield if (Rails.env == 'development')
+ Rails.logger.mongoize(:action => action_name, :controller => controller_name, :params => params, :ip => request.remote_ip) { yield }
+ end
+end
@@ -0,0 +1,59 @@
+module MongoLoggerModifications
+ def self.included(base)
+ base.class_eval {
+ alias_method :old_add, :add
+ alias_method :new_add, :add
+
+ db_configuration = {
+ 'host' => 'localhost',
+ 'port' => 27017,
+ 'capsize' => 100000}.merge( YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config/database.yml'))).result)[Rails.env]['mongo'] )
+
+ @@mongo_collection_name = "#{Rails.env}_log"
+ @@mongo_connection ||= Mongo::Connection.new(db_configuration['host'], db_configuration['port'], :auto_reconnect => true).db(db_configuration['database'])
+
+ # setup the capped collection if it doesn't already exist
+ unless @@mongo_connection.collection_names.include?(@@mongo_collection_name)
+ @@mongo_connection.create_collection(@@mongo_collection_name, {:capped => true, :size => db_configuration['capsize']})
+ end
+
+ cattr_reader :mongo_connection, :mongo_collection_name
+ }
+ end
+
+ def mongoize(options={})
+ @mongo_record = options.merge({
+ :messages => Hash.new { |hash, key| hash[key] = Array.new },
+ :request_time => Time.now.utc
+ })
+ runtime = Benchmark.ms do
+ yield
+ end
+ @mongo_record[:runtime] = runtime.ceil
+ self.class.mongo_connection[self.class.mongo_collection_name].insert(@mongo_record) rescue nil
+ end
+
+ def add_metadata(options={})
+ options.each_pair do |key, value|
+ unless [:messages, :request_time, :ip, :runtime].include?(key.to_sym)
+ info("adding #{key} => #{value}")
+ @mongo_record[key] = value
+ else
+ raise ArgumentError, ":#{key} is a reserved key for the mongo logger. Please choose a different key"
+ end
+ end
+ end
+
+ def new_add(severity, message = nil, progname = nil, &block)
+ unless @level > severity
+ if ActiveRecord::Base.colorize_logging
+ # remove colorization done by rails and just save the actual message
+ @mongo_record[:messages][level_to_sym(severity)] << message.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip rescue nil
+ else
+ @mongo_record[:messages][level_to_sym(severity)] << message
+ end
+ end
+
+ old_add(severity, message, progname, &block)
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :mongo_db_logger do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class MongoDbLoggerTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit aaaed6f

Please sign in to comment.