activesupport-operation_logger 0.1.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/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/active_support-operation_logger.gemspec +25 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_support/operation_logger.rb +88 -0
- data/lib/active_support/operation_logger/version.rb +5 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0c293665f8c0a6fed23c6464851489ad4fce3b0d
|
4
|
+
data.tar.gz: be66a0e2e7adb67aab8f5b2d7f2fb3f96b1625fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02a4375c25a234b0651b2000dc57a9a142caacddacf006402a4af5723083a31069cba389f3cd77edc85bc19da6de84ff8f2cf6307263aaf65616b21764a9c115
|
7
|
+
data.tar.gz: 7d5be6981943762b1c7cd0f641cc3b6de940d7c36be7e54b5810838735577a35791379fb91605d8046ab415f1076d46a97aa33a0f8886594e754534ea563b131
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ActiveSupport::OperationLogger
|
2
|
+
|
3
|
+
adds ActiveRecord-like logging to anything you want
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'activesupport-operation_logger'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install activesupport-operation_logger
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```rb
|
24
|
+
require 'active_support/operation_logger'
|
25
|
+
|
26
|
+
ActiveSupport::OperationLogger.log_calls_on! MyKlass
|
27
|
+
ActiveSupport::OperationLogger.log_calls_on! Redis::Client, event_namespace: 'redis', only: :call
|
28
|
+
```
|
29
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_support/operation_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activesupport-operation_logger"
|
8
|
+
spec.version = ActiveSupport::OperationLogger::VERSION
|
9
|
+
spec.authors = ["Josh Bodah"]
|
10
|
+
spec.email = ["jb3689@yahoo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{adds ActiveRecord-like logging to anything you want}
|
13
|
+
spec.homepage = "https://github.com/jbodah/active_support-operation_logger"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'activesupport'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_support/operation_logger"
|
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,88 @@
|
|
1
|
+
require "active_support/operation_logger/version"
|
2
|
+
require "active_support/notifications"
|
3
|
+
require "active_support/log_subscriber"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
module OperationLogger
|
7
|
+
# Provides methods that should probably be part of Ruby's stdlib
|
8
|
+
module MethodHelper
|
9
|
+
def self.public_owned_instance_methods(klass)
|
10
|
+
klass.public_instance_methods.select do |m|
|
11
|
+
klass.instance_method(m).owner == klass
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# A generic factory responsible for creating modules that can be used to
|
17
|
+
# decorate classes with ActiveSupport::Notifications
|
18
|
+
module EventInstrumenterFactory
|
19
|
+
# @param [Class] klass
|
20
|
+
# @param [Array<Symbol>] methods
|
21
|
+
# @param [Symbol] event_namespace
|
22
|
+
# @return [Module] a module for prepending
|
23
|
+
def self.instrumenter_for(klass, methods, event_namespace)
|
24
|
+
Module.new do
|
25
|
+
methods.each do |m|
|
26
|
+
define_method m do |*args, &block|
|
27
|
+
command_str = if args.size == 1 && args.first.is_a?(Array)
|
28
|
+
args.first.map(&:inspect).join(', ')
|
29
|
+
else
|
30
|
+
args.map(&:inspect).join(' ')
|
31
|
+
end
|
32
|
+
ActiveSupport::Notifications.instrument("call.#{event_namespace}",
|
33
|
+
name: "#{event_namespace.to_s.classify} #{m}",
|
34
|
+
command: command_str) do
|
35
|
+
super *args, &block
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# A generic factory responsible for creating classes that can be used to
|
44
|
+
# create LogSubscribers that integrate with ActiveSupport::Notifications
|
45
|
+
# and mirror the functionality of ActiveRecord::LogSubscriber
|
46
|
+
module EventSubscriberFactory
|
47
|
+
def self.subscriber_for(klass)
|
48
|
+
Class.new(ActiveSupport::LogSubscriber) do
|
49
|
+
def initialize
|
50
|
+
super
|
51
|
+
@odd = false
|
52
|
+
end
|
53
|
+
|
54
|
+
def odd?
|
55
|
+
@odd = !@odd
|
56
|
+
end
|
57
|
+
|
58
|
+
def call(event)
|
59
|
+
name = "#{event.payload[:name]} (#{event.duration.round(1)}ms)"
|
60
|
+
command = event.payload[:command]
|
61
|
+
if odd?
|
62
|
+
name = color(name, ActiveSupport::LogSubscriber::CYAN, true)
|
63
|
+
command = color(command, nil, true)
|
64
|
+
else
|
65
|
+
name = color(name, ActiveSupport::LogSubscriber::MAGENTA, true)
|
66
|
+
end
|
67
|
+
|
68
|
+
debug " #{name} #{command}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Encapsulates the act of decorating classes with ActiveSupport::Notifications
|
75
|
+
# instrumenters and ActiveSupport::LogSubscribers
|
76
|
+
#
|
77
|
+
# TODO: @jbodah 2016-04-30: just use one shared event subscriber
|
78
|
+
def self.log_calls_on!(klass, only: nil, event_namespace: nil)
|
79
|
+
methods = only || MethodHelper.public_owned_instance_methods(klass)
|
80
|
+
event_namespace ||= klass.name.demodulize.underscore
|
81
|
+
event_namespace = event_namespace.to_sym
|
82
|
+
|
83
|
+
klass.prepend EventInstrumenterFactory.instrumenter_for(klass, methods, event_namespace)
|
84
|
+
|
85
|
+
EventSubscriberFactory.subscriber_for(klass).attach_to(event_namespace)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activesupport-operation_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Bodah
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
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
|
+
description:
|
70
|
+
email:
|
71
|
+
- jb3689@yahoo.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- active_support-operation_logger.gemspec
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/active_support/operation_logger.rb
|
85
|
+
- lib/active_support/operation_logger/version.rb
|
86
|
+
homepage: https://github.com/jbodah/active_support-operation_logger
|
87
|
+
licenses: []
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.8
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: adds ActiveRecord-like logging to anything you want
|
109
|
+
test_files: []
|