mongo_logs_on_roids 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/mongo_logs_on_roids/colorize_command_log.rb +69 -0
- data/lib/mongo_logs_on_roids/railtie.rb +17 -0
- data/lib/mongo_logs_on_roids/runtime_subscriber.rb +75 -0
- data/lib/mongo_logs_on_roids/version.rb +3 -0
- data/lib/mongo_logs_on_roids.rb +11 -0
- data/mongo_logs_on_roids.gemspec +36 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d2dc3e286069213c9fd659853527b025e790c549
|
4
|
+
data.tar.gz: e7f1c201e0a22fed17675e183435ee0839730c2a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf7e529c572148a86d9dcb8998edb6a8bfb80b6c576a2abc5f69aae3cfd6d8d6e15f5a14e81dacc4218921c694d902ab569f82e300195f9d4546b4d12382264e
|
7
|
+
data.tar.gz: df0da61a49b27c6d790f30f59ccf5eb7a25f1630891e3192ca753b3be375ced585bf6cfae1c873c39b8574ad7915a453e9de4df4fe6b6826ad730c3586f0118c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 thomas morgan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# MongoLogsOnRoids
|
2
|
+
|
3
|
+
Better logging for mongodb on Ruby.
|
4
|
+
|
5
|
+
Compatible with just the `mongo` gem (v2.1+). Auto-detects Mongoid 5 and Rails and enables appropriate features.
|
6
|
+
|
7
|
+
Makes mongo logs more awesome with:
|
8
|
+
|
9
|
+
* Colorized logging
|
10
|
+
* Calculating and displaying total time in mongo (Rails):
|
11
|
+
`Completed 200 OK in 13ms (Views: 0.2ms | Mongo: 3.2ms)`
|
12
|
+
* Showing mongo logs in console, for faster debugging
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Install like any other gem. Add to your Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'mongo_logs_on_roids'
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Under normal circumstances, should activate itself automatically.
|
27
|
+
|
28
|
+
<!-- ## Development
|
29
|
+
|
30
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
31
|
+
|
32
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). -->
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zarqman/mongo_logs_on_roids.
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
42
|
+
|
43
|
+
Inspired by [mongoid_colored_logger](https://github.com/romanbsd/mongoid_colored_logger), by Roman Shterenzon (thanks!).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mongo_logs_on_roids"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module MongoLogsOnRoids
|
2
|
+
module ColorizeCommandLog
|
3
|
+
|
4
|
+
CLEAR = "\e[0m"
|
5
|
+
BOLD = "\e[1m"
|
6
|
+
BLACK = "\e[30m"
|
7
|
+
RED = "\e[31m"
|
8
|
+
GREEN = "\e[32m"
|
9
|
+
YELLOW = "\e[33m"
|
10
|
+
BLUE = "\e[34m"
|
11
|
+
MAGENTA = "\e[35m"
|
12
|
+
CYAN = "\e[36m"
|
13
|
+
WHITE = "\e[37m"
|
14
|
+
|
15
|
+
|
16
|
+
def started(event)
|
17
|
+
if logger.debug?
|
18
|
+
log_debug("#{prefix(event)} | #{MAGENTA}STARTED#{BLUE} | #{format_command(event.command)}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def succeeded(event)
|
23
|
+
if logger.debug?
|
24
|
+
log_debug("#{prefix(event)} | #{BOLD}#{GREEN}SUCCEEDED#{CLEAR}#{BLUE} | #{'%.3f' % (event.duration*1000)}ms")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def failed(event)
|
29
|
+
if logger.debug?
|
30
|
+
log_debug("#{prefix(event)} | #{RED}FAILED#{BLUE} | #{YELLOW}#{event.message}#{BLUE} | #{'%.3f' % (event.duration*1000)}ms")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def format_command(args)
|
38
|
+
message = super
|
39
|
+
WHITE+message.gsub(/=>/){|m| MAGENTA+m+WHITE }+BLUE
|
40
|
+
# message.gsub(/\[?\{.+?\}\]?(\s|$)/) do |m|
|
41
|
+
# m.gsub!(/=>/) {|m2| color(m2, MAGENTA) + WHITE }
|
42
|
+
# color(m, WHITE)+BLUE
|
43
|
+
# end
|
44
|
+
end
|
45
|
+
|
46
|
+
PREFIX = Mongo::Loggable::PREFIX
|
47
|
+
|
48
|
+
def format_message(message)
|
49
|
+
format("#{BLUE}%s | %s#{CLEAR}".freeze, PREFIX, message)
|
50
|
+
end
|
51
|
+
|
52
|
+
def prefix(event)
|
53
|
+
"#{event.address.to_s} | #{CYAN}#{event.database_name}#{BLUE}.#{YELLOW}#{event.command_name}#{BLUE}"
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# def color(text, color, bold=false)
|
58
|
+
# # return text unless colorize_logging
|
59
|
+
# color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
|
60
|
+
# bold = bold ? BOLD : ""
|
61
|
+
# "#{bold}#{color}#{text}#{CLEAR}"
|
62
|
+
# end
|
63
|
+
# def color(text, color, bold=false)
|
64
|
+
# color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
65
|
+
# "#{BOLD if bold}#{color}#{text}#{CLEAR}"
|
66
|
+
# end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module MongoLogsOnRoids
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
ActiveSupport.on_load(:action_controller) do
|
5
|
+
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, MongoLogsOnRoids::RuntimeSubscriber.new)
|
6
|
+
include MongoLogsOnRoids::ControllerRuntime
|
7
|
+
end
|
8
|
+
|
9
|
+
console do |app|
|
10
|
+
Mongo::Logger.logger = Logger.new($stdout)
|
11
|
+
if defined?(Mongoid)
|
12
|
+
config.mongoid.logger = Mongo::Logger.logger
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'active_support/log_subscriber'
|
2
|
+
|
3
|
+
module MongoLogsOnRoids
|
4
|
+
class RuntimeSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def runtime=(value)
|
8
|
+
Thread.current['mongo_runtime'] = value
|
9
|
+
end
|
10
|
+
|
11
|
+
def runtime
|
12
|
+
Thread.current['mongo_runtime'] ||= 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset_runtime
|
16
|
+
rt, self.runtime = runtime, 0
|
17
|
+
rt
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def started(event)
|
23
|
+
# noop
|
24
|
+
end
|
25
|
+
def succeeded(event)
|
26
|
+
self.class.runtime += event.duration*1000
|
27
|
+
end
|
28
|
+
def failed(event)
|
29
|
+
self.class.runtime += event.duration*1000
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Extends ActionController's logging system so as to include
|
34
|
+
# cumulative runtime at the end of each action's log entry.
|
35
|
+
# See ActiveRecord::Railties::ControllerRuntime for reference
|
36
|
+
module ControllerRuntime
|
37
|
+
extend ActiveSupport::Concern
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
attr_internal :runtime
|
42
|
+
|
43
|
+
def process_action(action, *args)
|
44
|
+
# We also need to reset the runtime before each action
|
45
|
+
# because of queries in middleware or in cases we are streaming
|
46
|
+
# and it won't be cleaned up by the method below.
|
47
|
+
MongoLogsOnRoids::RuntimeSubscriber.reset_runtime
|
48
|
+
super
|
49
|
+
end
|
50
|
+
|
51
|
+
def cleanup_view_runtime
|
52
|
+
rt_before_render = MongoLogsOnRoids::RuntimeSubscriber.reset_runtime
|
53
|
+
self.runtime = (runtime || 0) + rt_before_render
|
54
|
+
runtime = super
|
55
|
+
rt_after_render = MongoLogsOnRoids::RuntimeSubscriber.reset_runtime
|
56
|
+
self.runtime += rt_after_render
|
57
|
+
runtime - rt_after_render
|
58
|
+
end
|
59
|
+
|
60
|
+
def append_info_to_payload(payload)
|
61
|
+
super
|
62
|
+
payload[:mongo_runtime] = (runtime || 0) + MongoLogsOnRoids::RuntimeSubscriber.reset_runtime
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
module ClassMethods
|
67
|
+
def log_process_action(payload)
|
68
|
+
messages, rt = super, payload[:mongo_runtime]
|
69
|
+
messages << ("Mongo: %.1fms" % rt.to_f) if rt && rt > 0
|
70
|
+
messages
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
|
3
|
+
require 'mongo_logs_on_roids/version'
|
4
|
+
require 'mongo_logs_on_roids/colorize_command_log'
|
5
|
+
|
6
|
+
Mongo::Monitoring::CommandLogSubscriber.prepend MongoLogsOnRoids::ColorizeCommandLog
|
7
|
+
|
8
|
+
if defined?(Rails)
|
9
|
+
require 'mongo_logs_on_roids/runtime_subscriber'
|
10
|
+
require 'mongo_logs_on_roids/railtie'
|
11
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongo_logs_on_roids/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongo_logs_on_roids"
|
8
|
+
spec.version = MongoLogsOnRoids::VERSION
|
9
|
+
spec.authors = ["thomas morgan"]
|
10
|
+
spec.email = ["tm@iprog.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Better logging for mongodb. Compatible w/Mongoid 5, Rails, etc.}
|
13
|
+
spec.description = %q{Add color, DB time (Rails), etc to your mongo logs. Requires just mongo gem; compatible with Mongoid 5, Rails, etc.}
|
14
|
+
spec.homepage = "https://github.com/zarqman/mongo_logs_on_roids"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# # delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "minitest"
|
33
|
+
spec.add_development_dependency "mongoid", "~> 5.0"
|
34
|
+
spec.add_development_dependency "rails", "~> 4.2"
|
35
|
+
spec.add_dependency "mongo", "~> 2.1"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_logs_on_roids
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- thomas morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mongoid
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mongo
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.1'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.1'
|
97
|
+
description: Add color, DB time (Rails), etc to your mongo logs. Requires just mongo
|
98
|
+
gem; compatible with Mongoid 5, Rails, etc.
|
99
|
+
email:
|
100
|
+
- tm@iprog.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/mongo_logs_on_roids.rb
|
113
|
+
- lib/mongo_logs_on_roids/colorize_command_log.rb
|
114
|
+
- lib/mongo_logs_on_roids/railtie.rb
|
115
|
+
- lib/mongo_logs_on_roids/runtime_subscriber.rb
|
116
|
+
- lib/mongo_logs_on_roids/version.rb
|
117
|
+
- mongo_logs_on_roids.gemspec
|
118
|
+
homepage: https://github.com/zarqman/mongo_logs_on_roids
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.4.3
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Better logging for mongodb. Compatible w/Mongoid 5, Rails, etc.
|
142
|
+
test_files: []
|