mongoid_logger 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53e985ab2b9d5543a4ff59616696ed9d17d246af
4
+ data.tar.gz: fd26deee9c54db16d7c583281c2f9eecaaa2da95
5
+ SHA512:
6
+ metadata.gz: 4b1cb3c5b734214492e77f915c1dc7c89fde634af00c20db2e5fe2a13a3c5b04e897f9c29bf70d79e4eae1489f01339dec1904f9ef40e87fda00bf9dcf7ea880
7
+ data.tar.gz: 0dcf2bf161432d41147a19f86db211e03d2cec2c48ac989cbda6043cc3b6ebf65ceadbcab09316f98a6a5aed445017a3578d574cff661bf96dc5e5f84822ff3f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 akima
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MongoidLogger
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid_logger'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid_logger
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require "mongoid_logger/version"
4
+
5
+ require "active_support/buffered_logger"
6
+ require "mongoid"
7
+
8
+ module MongoidLogger
9
+
10
+ LOG_LEVEL_SYM = [:debug, :info, :warn, :error, :fatal, :unknown]
11
+
12
+ LEVEL_NAMES = LOG_LEVEL_SYM.each_with_object({}) do |name, d|
13
+ value = Logger.const_get(name.to_s.upcase)
14
+ d[value] = name.to_s
15
+ end
16
+
17
+ autoload :Base , "mongoid_logger/base"
18
+ autoload :Filter , "mongoid_logger/filter"
19
+ autoload :LogCollection, "mongoid_logger/log_collection"
20
+ end
@@ -0,0 +1,131 @@
1
+ require 'mongoid_logger'
2
+
3
+ module MongoidLogger
4
+
5
+ class Base < ActiveSupport::BufferedLogger
6
+
7
+ class << self
8
+ def collection_names
9
+ @collection_names ||= []
10
+ end
11
+ end
12
+
13
+ def initialize(path, options={})
14
+ @path = path
15
+ @level = options[:level] || DEBUG
16
+ configure(options)
17
+ super(@path, @level)
18
+ end
19
+
20
+ def add(severity, message=nil, progname=nil, &blk)
21
+ if @level <= severity and message.present? and @record.present?
22
+ @record[:messages] ||= []
23
+ @record[:messages] << [severity, message]
24
+ end
25
+ super
26
+ end
27
+
28
+ def add_metadata(options={})
29
+ if @record
30
+ options.each_pair do |key, value|
31
+ @record[key] = value
32
+ end
33
+ end
34
+ end
35
+
36
+ def mongoize(controller, options={})
37
+ @record = options.merge({
38
+ :messages => [],
39
+ :request_time => Time.now.getutc,
40
+ :application_name => @application_name,
41
+ :host => Socket.gethostname,
42
+ :pid => Process.pid,
43
+ })
44
+ st = Time.now
45
+ yield
46
+ ed = Time.now
47
+ @record["runtime"] = (ed - st).to_f
48
+ @record["status"] = controller ? controller.response.status : 200
49
+ rescue Exception
50
+ if st
51
+ @record["runtime"] = (Time.now - st).to_f
52
+ end
53
+ @record["status"] = 500
54
+ add(3, $!.message + "\n " + $!.backtrace.join("\n ")) #rescue nil
55
+ raise
56
+ ensure
57
+ begin
58
+ insert_document(@record)
59
+ rescue
60
+ end
61
+ end
62
+
63
+ def create_collection(name)
64
+ @session.command(create: name, capped: true, size: @db_configuration["capsize"] || 64.megabyte)
65
+ rescue Exception => e
66
+ if e.message =~ /collection already exists/
67
+ internal_log(:warn, "Ignore #{name} creation failure. See following message of #{e.class.name}:\n" << e.message)
68
+ else
69
+ raise e
70
+ end
71
+ end
72
+
73
+ def confirm_collection
74
+ mongo_collection_name_array.each do |name|
75
+ unless @session.collections.find{|col| col.name == name }
76
+ create_collection(name)
77
+ end
78
+ end
79
+ end
80
+
81
+ def reset_collection
82
+ mongo_collection_name_array.each do |name|
83
+ @session.command(drop: name)
84
+ create_collection(name)
85
+ end
86
+ end
87
+
88
+ attr_reader :mongo_collection_names
89
+
90
+ def mongo_collection_name_array
91
+ (@mongo_collection_names.values + [@mongo_collection_names.default]).compact.uniq
92
+ end
93
+
94
+ private
95
+
96
+ def configure(options={})
97
+ @db_configuration = {
98
+ "capsize" => 512.megabytes,
99
+ }.merge(resolve_config)
100
+ base_name = options[:collection_name] || @db_configuration["log_collection"]
101
+ @mongo_collection_names = (options[:isolated_methods] || []).each_with_object({}){|name,d | d[name] = base_name.sub(/_logs\Z/){ "_#{name}_logs" } }
102
+ @mongo_collection_names.default = base_name
103
+
104
+ @application_name = @db_configuration["application_name"]
105
+ @session = Mongoid.default_session.with(safe: true)
106
+ confirm_collection
107
+
108
+ @mongo_collections = @mongo_collection_names.each_with_object({}){|(k, col_name), d| d[k] = @session[col_name] }
109
+ @mongo_collections.default = @session[ @mongo_collection_names.default ]
110
+
111
+ @ignore_block = options[:ignore]
112
+ end
113
+
114
+ def resolve_config
115
+ {}
116
+ end
117
+
118
+ def insert_document(doc)
119
+ doc[:level] = doc[:messages].map(&:first).min || 0
120
+ return if @ignore_block && @ignore_block.call(doc)
121
+ col = @mongo_collections[ doc[:request_method].downcase.to_sym ]
122
+ col.insert(doc)
123
+ end
124
+
125
+ def internal_log(log_level, msg)
126
+ $stderr.puts("#{log_level} #{msg}")
127
+ end
128
+
129
+ end
130
+
131
+ end
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'mongoid_logger'
3
+
4
+ module MongoidLogger
5
+
6
+ # ApplicationController に include して around_filter でログ挿入するためのモジュール
7
+ module Filter
8
+ def self.included(klass)
9
+ klass.class_eval { around_filter :enable_mongoid_logger }
10
+ end
11
+
12
+ def enable_mongoid_logger
13
+ return yield unless logger.respond_to?(:mongoize)
14
+
15
+ f_params = case
16
+ when request.respond_to?(:filtered_parameters)
17
+ request.filtered_parameters
18
+ when respond_to?(:filter_parameters)
19
+ filter_parameters(params)
20
+ else
21
+ params
22
+ end
23
+ # controllerのloggerに対して処理を行います
24
+ logger.mongoize(self, {
25
+ :request_method => request.request_method,
26
+ :path => request.path,
27
+ :url => request.url,
28
+ :params => f_params,
29
+ :remote_ip => request.remote_ip,
30
+ }) { yield }
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'mongoid_logger'
3
+
4
+ module MongoidLogger
5
+
6
+ # ログコレクションのためのモジュール
7
+ module LogCollection
8
+ def self.included(klass)
9
+ klass.class_eval do
10
+ include Mongoid::Document
11
+ field :request_time, :type => ActiveSupport::TimeWithZone
12
+ field :application_name, :type => String
13
+ field :level, :type => Integer
14
+ field :host, :type => String
15
+ field :pid, :type => Integer
16
+ field :request_method, :type => String
17
+ field :path, :type => String
18
+ field :url, :type => String
19
+ field :params, :type => Object
20
+ field :remote_ip, :type => String
21
+ field :messages, :type => Array
22
+ field :runtime, :type => Float
23
+ field :status, :type => Integer
24
+
25
+ def level_name
26
+ LOG_LEVEL_SYM[level]
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidLogger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid_logger"
8
+ spec.version = MongoidLogger::VERSION
9
+ spec.authors = ["akima", "nagachika"]
10
+ spec.email = ["akm2000@gmail.com"]
11
+ spec.description = %q{Log into both log file and mongodb log collection}
12
+ spec.summary = %q{Log into both log file and mongodb log collection}
13
+ spec.homepage = "https://github.com/groovenauts/mongoid_logger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activesupport", "~> 3.2.11"
22
+ spec.add_runtime_dependency "mongoid", "~> 3.1.3"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoidLogger do
4
+ it 'should have a version number' do
5
+ MongoidLogger::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should do something useful' do
9
+ false.should be_true
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'mongoid_logger'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - akima
8
+ - nagachika
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 3.2.11
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.11
28
+ - !ruby/object:Gem::Dependency
29
+ name: mongoid
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 3.1.3
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: 3.1.3
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '1.3'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.3'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Log into both log file and mongodb log collection
85
+ email:
86
+ - akm2000@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/mongoid_logger.rb
99
+ - lib/mongoid_logger/base.rb
100
+ - lib/mongoid_logger/filter.rb
101
+ - lib/mongoid_logger/log_collection.rb
102
+ - lib/mongoid_logger/version.rb
103
+ - mongoid_logger.gemspec
104
+ - spec/mongoid_logger_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: https://github.com/groovenauts/mongoid_logger
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Log into both log file and mongodb log collection
130
+ test_files:
131
+ - spec/mongoid_logger_spec.rb
132
+ - spec/spec_helper.rb
133
+ has_rdoc: