Skip to content

Commit aaaed6f

Browse files
author
phil
committedSep 24, 2009
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
0 parents  commit aaaed6f

11 files changed

+145
-0
lines changed
 

‎MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 [name of plugin creator]
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
MongoDbLogger
2+
=============
3+
4+
Introduction goes here.
5+
6+
7+
Example
8+
=======
9+
10+
Example goes here.
11+
12+
13+
Copyright (c) 2009 [name of plugin creator], released under the MIT license

‎Rakefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'rake'
2+
require 'rake/testtask'
3+
require 'rake/rdoctask'
4+
5+
desc 'Default: run unit tests.'
6+
task :default => :test
7+
8+
desc 'Test the mongo_db_logger plugin.'
9+
Rake::TestTask.new(:test) do |t|
10+
t.libs << 'lib'
11+
t.libs << 'test'
12+
t.pattern = 'test/**/*_test.rb'
13+
t.verbose = true
14+
end
15+
16+
desc 'Generate documentation for the mongo_db_logger plugin.'
17+
Rake::RDocTask.new(:rdoc) do |rdoc|
18+
rdoc.rdoc_dir = 'rdoc'
19+
rdoc.title = 'MongoDbLogger'
20+
rdoc.options << '--line-numbers' << '--inline-source'
21+
rdoc.rdoc_files.include('README')
22+
rdoc.rdoc_files.include('lib/**/*.rb')
23+
end

‎init.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Include hook code here
2+
require 'mongo_db_logger'

‎install.rb

Whitespace-only changes.

‎lib/mongo_db_logger.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'mongo'
2+
module MongoDBLogger
3+
def self.included(base)
4+
ActiveSupport::BufferedLogger.send(:include, MongoLoggerModifications)
5+
base.class_eval { around_filter :use_mongodb_logger }
6+
end
7+
8+
def use_mongodb_logger
9+
# return yield if (Rails.env == 'development')
10+
Rails.logger.mongoize(:action => action_name, :controller => controller_name, :params => params, :ip => request.remote_ip) { yield }
11+
end
12+
end

‎lib/mongo_logger_modifications.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module MongoLoggerModifications
2+
def self.included(base)
3+
base.class_eval {
4+
alias_method :old_add, :add
5+
alias_method :new_add, :add
6+
7+
db_configuration = {
8+
'host' => 'localhost',
9+
'port' => 27017,
10+
'capsize' => 100000}.merge( YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config/database.yml'))).result)[Rails.env]['mongo'] )
11+
12+
@@mongo_collection_name = "#{Rails.env}_log"
13+
@@mongo_connection ||= Mongo::Connection.new(db_configuration['host'], db_configuration['port'], :auto_reconnect => true).db(db_configuration['database'])
14+
15+
# setup the capped collection if it doesn't already exist
16+
unless @@mongo_connection.collection_names.include?(@@mongo_collection_name)
17+
@@mongo_connection.create_collection(@@mongo_collection_name, {:capped => true, :size => db_configuration['capsize']})
18+
end
19+
20+
cattr_reader :mongo_connection, :mongo_collection_name
21+
}
22+
end
23+
24+
def mongoize(options={})
25+
@mongo_record = options.merge({
26+
:messages => Hash.new { |hash, key| hash[key] = Array.new },
27+
:request_time => Time.now.utc
28+
})
29+
runtime = Benchmark.ms do
30+
yield
31+
end
32+
@mongo_record[:runtime] = runtime.ceil
33+
self.class.mongo_connection[self.class.mongo_collection_name].insert(@mongo_record) rescue nil
34+
end
35+
36+
def add_metadata(options={})
37+
options.each_pair do |key, value|
38+
unless [:messages, :request_time, :ip, :runtime].include?(key.to_sym)
39+
info("adding #{key} => #{value}")
40+
@mongo_record[key] = value
41+
else
42+
raise ArgumentError, ":#{key} is a reserved key for the mongo logger. Please choose a different key"
43+
end
44+
end
45+
end
46+
47+
def new_add(severity, message = nil, progname = nil, &block)
48+
unless @level > severity
49+
if ActiveRecord::Base.colorize_logging
50+
# remove colorization done by rails and just save the actual message
51+
@mongo_record[:messages][level_to_sym(severity)] << message.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip rescue nil
52+
else
53+
@mongo_record[:messages][level_to_sym(severity)] << message
54+
end
55+
end
56+
57+
old_add(severity, message, progname, &block)
58+
end
59+
end

‎tasks/mongo_db_logger_tasks.rake

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# desc "Explaining what the task does"
2+
# task :mongo_db_logger do
3+
# # Task goes here
4+
# end

‎test/mongo_db_logger_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'test_helper'
2+
3+
class MongoDbLoggerTest < ActiveSupport::TestCase
4+
# Replace this with your real tests.
5+
test "the truth" do
6+
assert true
7+
end
8+
end

‎test/test_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rubygems'
2+
require 'active_support'
3+
require 'active_support/test_case'

‎uninstall.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Uninstall hook code here

0 commit comments

Comments
 (0)
Please sign in to comment.