effective_logging 1.0.0

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.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +280 -0
  4. data/Rakefile +20 -0
  5. data/app/assets/javascripts/effective_logging.js +1 -0
  6. data/app/assets/javascripts/effective_logging/effective_logger.js.coffee.erb +15 -0
  7. data/app/controllers/admin/logs_controller.rb +31 -0
  8. data/app/controllers/effective/logs_controller.rb +71 -0
  9. data/app/helpers/effective_logging_helper.rb +59 -0
  10. data/app/models/effective/access_denied.rb +17 -0
  11. data/app/models/effective/datatables/logs.rb +49 -0
  12. data/app/models/effective/log.rb +59 -0
  13. data/app/models/effective/user_logger.rb +7 -0
  14. data/app/models/effective_logger.rb +38 -0
  15. data/app/views/active_admin/effective_logging/logs/index.html.haml +8 -0
  16. data/app/views/active_admin/effective_logging/logs/show.html.haml +4 -0
  17. data/app/views/admin/logs/index.html.haml +7 -0
  18. data/app/views/admin/logs/show.html.haml +3 -0
  19. data/app/views/effective/logs/_log.html.haml +59 -0
  20. data/app/views/effective/logs/index.html.haml +7 -0
  21. data/app/views/effective/logs/show.html.haml +2 -0
  22. data/config/routes.rb +18 -0
  23. data/db/migrate/01_create_effective_logging.rb.erb +29 -0
  24. data/lib/effective_logging.rb +32 -0
  25. data/lib/effective_logging/email_logger.rb +28 -0
  26. data/lib/effective_logging/engine.rb +50 -0
  27. data/lib/effective_logging/log_page_views.rb +68 -0
  28. data/lib/effective_logging/version.rb +3 -0
  29. data/lib/generators/effective_logging/install_generator.rb +33 -0
  30. data/lib/generators/templates/README +1 -0
  31. data/lib/generators/templates/effective_logging.rb +46 -0
  32. data/spec/effective_logging_spec.rb +7 -0
  33. data/spec/spec_helper.rb +33 -0
  34. metadata +150 -0
@@ -0,0 +1,50 @@
1
+ module EffectiveLogging
2
+ class Engine < ::Rails::Engine
3
+ engine_name 'effective_logging'
4
+
5
+ # Set up our default configuration options.
6
+ initializer "effective_logging.defaults", :before => :load_config_initializers do |app|
7
+ eval File.read("#{config.root}/lib/generators/templates/effective_logging.rb")
8
+ end
9
+
10
+ # ActiveAdmin (optional)
11
+ # This prepends the load path so someone can override the assets.rb if they want.
12
+ initializer 'effective_logging.active_admin' do
13
+ if defined?(ActiveAdmin) && EffectiveLogging.use_active_admin == true
14
+ ActiveAdmin.application.load_paths.unshift Dir["#{config.root}/active_admin"]
15
+ end
16
+ end
17
+
18
+ # Automatically Log Emails
19
+ initializer 'effective_logging.emails' do |app|
20
+ if EffectiveLogging.emails_enabled == true
21
+ require 'effective_logging/email_logger'
22
+ ActionMailer::Base.register_observer(EffectiveLogging::EmailLogger)
23
+ end
24
+ end
25
+
26
+ # Register the log_page_views concern so that it can be called in ActionController or elsewhere
27
+ initializer 'effective_logging.action_controller' do |app|
28
+ ActiveSupport.on_load :action_controller do
29
+ ActionController::Base.extend(EffectiveLogging::LogPageViews::ActionController)
30
+ end
31
+ end
32
+
33
+ # This has to be run after initialization or User hasn't been loaded yet
34
+ config.after_initialize do
35
+ if EffectiveLogging.user_logins_enabled == true
36
+ ActiveSupport.on_load :active_record do
37
+ if defined?(Devise)
38
+ User.instance_eval do
39
+ alias_method :original_after_database_authentication, :after_database_authentication
40
+ define_method(:after_database_authentication) { Effective::UserLogger.successful_login(self) ; original_after_database_authentication() }
41
+ end
42
+ else
43
+ raise ArgumentError.new("EffectiveLogging.user_logins_enabled only works with Devise and a user class defined as User")
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,68 @@
1
+ module EffectiveLogging
2
+ module LogPageViews
3
+
4
+ module ActionController
5
+ def log_page_views(options = nil)
6
+ @log_page_view_options = options || {}
7
+
8
+ include EffectiveLogging::LogPageViews::InstanceMethods
9
+ extend EffectiveLogging::LogPageViews::ClassMethods
10
+
11
+ # Break up the options
12
+ logging_options = {} ; filter_options = {}
13
+ (@log_page_view_options || {}).each do |k, v|
14
+ [:details, :skip_namespace].include?(k) ? (logging_options[k] = v) : (filter_options[k] = v)
15
+ end
16
+
17
+ cattr_accessor :log_page_views_opts
18
+ self.log_page_views_opts = logging_options
19
+
20
+ # Set up the after_filter to do page logging
21
+ after_filter :effective_logging_log_page_view, filter_options
22
+ end
23
+
24
+ def skip_log_page_views(options = {})
25
+ Rails.logger.info("WARNING EffectiveLogging: skip_log_page_views called without first having called log_page_views. Please add 'log_page_views' to your ApplicationController or this controller before using skip_log_page_views") unless options[:quiet]
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def skip_log_page_views(options = {})
31
+ before_filter :skip_log_page_view, options
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+ def effective_logging_log_page_view
37
+ return if @_effective_logging_skip_log_page_view == true
38
+ return if (self.class.log_page_views_opts[:skip_namespace] || []).include?(self.class.parent)
39
+
40
+ user = (current_user rescue nil)
41
+
42
+ if self.class.log_page_views_opts[:details] == false
43
+ EffectiveLogger.info("page view: #{request.request_method} #{request.path}", :user => user)
44
+ else
45
+ EffectiveLogger.info(
46
+ "page view: #{request.request_method} #{request.path}",
47
+ :user => user,
48
+ :params => request.params.reject { |k, v| (k == 'controller' || k == 'action') },
49
+ :format => (request.format.to_s == 'text/html' ? nil : request.format.to_s),
50
+ :referrer => request.referrer,
51
+ :user_agent => request.user_agent
52
+ )
53
+ end
54
+ end
55
+
56
+ def skip_log_page_view
57
+ @_effective_logging_skip_log_page_view = true
58
+ end
59
+
60
+ def skip_log_page_views
61
+ @_effective_logging_skip_log_page_view = true
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
68
+
@@ -0,0 +1,3 @@
1
+ module EffectiveLogging
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,33 @@
1
+ module EffectiveLogging
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ desc "Creates an EffectiveLogging initializer in your application."
7
+
8
+ source_root File.expand_path("../../templates", __FILE__)
9
+
10
+ def self.next_migration_number(dirname)
11
+ if not ActiveRecord::Base.timestamped_migrations
12
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
13
+ else
14
+ "%.3d" % (current_migration_number(dirname) + 1)
15
+ end
16
+ end
17
+
18
+ def copy_initializer
19
+ template "effective_logging.rb", "config/initializers/effective_logging.rb"
20
+ end
21
+
22
+ def create_migration_file
23
+ @logs_table_name = ':' + EffectiveLogging.logs_table_name.to_s
24
+
25
+ migration_template '../../../db/migrate/01_create_effective_logging.rb.erb', 'db/migrate/create_effective_logging.rb'
26
+ end
27
+
28
+ def show_readme
29
+ readme "README" if behavior == :invoke
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ Thanks for using EffectiveLogging
@@ -0,0 +1,46 @@
1
+ EffectiveLogging.setup do |config|
2
+ # Configure Database Tables
3
+ config.logs_table_name = :logs
4
+
5
+ # Authorization Method
6
+ #
7
+ # This method is called by all controller actions with the appropriate action and resource
8
+ # If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
9
+ #
10
+ # Use via Proc (and with CanCan):
11
+ # config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
12
+ #
13
+ # Use via custom method:
14
+ # config.authorization_method = :my_authorization_method
15
+ #
16
+ # And then in your application_controller.rb:
17
+ #
18
+ # def my_authorization_method(action, resource)
19
+ # current_user.is?(:admin)
20
+ # end
21
+ #
22
+ # Or disable the check completely:
23
+ # config.authorization_method = false
24
+ config.authorization_method = Proc.new { |controller, action, resource| true } # All users can see every screen
25
+
26
+ # Register Effective::Logs with ActiveAdmin if ActiveAdmin is present
27
+ config.use_active_admin = true
28
+
29
+ # Admin Screens Layout Settings
30
+ config.layout = 'application' # All EffectiveLogging controllers will use this layout
31
+
32
+ # All statuses defined here, as well as 'info', 'success', and 'error' (hardcoded) will be created as
33
+ # EffectiveLogger.info('my message') macros
34
+ config.additional_statuses = []
35
+
36
+ #########################################
37
+ #### Automatic Logging Functionality ####
38
+ #########################################
39
+
40
+ # Log all emails sent
41
+ config.emails_enabled = true
42
+
43
+ # Log all successful user login attempts
44
+ config.user_logins_enabled = true
45
+
46
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe EffectiveLogging do
4
+ it 'should be a module' do
5
+ assert_kind_of Module, EffectiveLogging
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ require File.expand_path("../dummy/config/environment", __FILE__)
4
+
5
+ require 'rspec/rails'
6
+ require 'rspec/autorun'
7
+
8
+ # Requires supporting ruby files with custom matchers and macros, etc,
9
+ # in spec/support/ and its subdirectories.
10
+ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
14
+
15
+ Rails.logger.level = 4 # Output only minimal stuff to test.log
16
+
17
+ config.use_transactional_fixtures = true # Make this false to once again use DatabaseCleaner
18
+ config.infer_base_class_for_anonymous_controllers = false
19
+ config.order = 'random'
20
+ end
21
+
22
+ class ActiveRecord::Base
23
+ mattr_accessor :shared_connection
24
+ @@shared_connection = nil
25
+
26
+ def self.connection
27
+ @@shared_connection || retrieve_connection
28
+ end
29
+ end
30
+
31
+ # Forces all threads to share the same connection. This works on
32
+ # Capybara because it starts the web server in a thread.
33
+ ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: effective_logging
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Code and Effect
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: coffee-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: devise
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: haml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: migrant
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Automatically log all sent emails, user logins, and page views. This
84
+ also will log custom events from Ruby and JavaScript.
85
+ email:
86
+ - info@codeandeffect.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - MIT-LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - app/assets/javascripts/effective_logging.js
95
+ - app/assets/javascripts/effective_logging/effective_logger.js.coffee.erb
96
+ - app/controllers/admin/logs_controller.rb
97
+ - app/controllers/effective/logs_controller.rb
98
+ - app/helpers/effective_logging_helper.rb
99
+ - app/models/effective/access_denied.rb
100
+ - app/models/effective/datatables/logs.rb
101
+ - app/models/effective/log.rb
102
+ - app/models/effective/user_logger.rb
103
+ - app/models/effective_logger.rb
104
+ - app/views/active_admin/effective_logging/logs/index.html.haml
105
+ - app/views/active_admin/effective_logging/logs/show.html.haml
106
+ - app/views/admin/logs/index.html.haml
107
+ - app/views/admin/logs/show.html.haml
108
+ - app/views/effective/logs/_log.html.haml
109
+ - app/views/effective/logs/index.html.haml
110
+ - app/views/effective/logs/show.html.haml
111
+ - config/routes.rb
112
+ - db/migrate/01_create_effective_logging.rb.erb
113
+ - lib/effective_logging.rb
114
+ - lib/effective_logging/email_logger.rb
115
+ - lib/effective_logging/engine.rb
116
+ - lib/effective_logging/log_page_views.rb
117
+ - lib/effective_logging/version.rb
118
+ - lib/generators/effective_logging/install_generator.rb
119
+ - lib/generators/templates/README
120
+ - lib/generators/templates/effective_logging.rb
121
+ - spec/effective_logging_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: https://github.com/code-and-effect/effective_logging
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Automatically log all sent emails, user logins, and page views. This also
147
+ will log custom events from Ruby and JavaScript.
148
+ test_files:
149
+ - spec/effective_logging_spec.rb
150
+ - spec/spec_helper.rb