technologic 0.3.1 → 0.4.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c488d3241dd9a49c25f589abeb1ad2881bbd49111b6e89bd4b591595723cc36
4
- data.tar.gz: 97a061057d7d1be868593ae093084c89f17044fe07cfffc954b8a377cdc2cd27
3
+ metadata.gz: 4d1d248bef1496c9ff561e4c50643d464b9a76e02f089d4c54917a5ce9a22552
4
+ data.tar.gz: a3292dcbda07946d9c4b3c8d86fa8354c79ff8b347250c2b0edcdb0f6c1c6bf5
5
5
  SHA512:
6
- metadata.gz: 9fdc6a17fe71ad4489fdf3a89bd50d88aa59469adcdc57a170362c7179a40952c542aded610e1590c4d0e1364632977beec491c02f8c7cf6f584139f08c4ce0c
7
- data.tar.gz: 64e6e5a9262c53481c713a99bc28b7b57a3208bc8872cdbb7f683ea8b05d5e4810c2a862b0ea54ee570d0c7d033d3197eda90fe4939fe50463e8cee9ccb0af4d
6
+ metadata.gz: b5830d5eaee4562a7dfdb97a19e8d0b46fd2bfcc2c4c550b996f6051ce321fc540bf4abf7a54798379a02435a13c4d20b3fd2be5a910fe21dfdc4f7736fd4058
7
+ data.tar.gz: fb6c695b74b5bd4519ca20ea331c5b01158be4529d71d2d3b0b0bd0af648f981d7ac6273da3cf7a5517a60c989c3749e4d9d289737840503793fee99ea23e0fa
data/lib/technologic.rb CHANGED
@@ -1,7 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support"
4
+ require "active_support/callbacks"
5
+ require "active_support/inflector"
6
+ require "active_support/core_ext/hash/keys"
7
+ require "active_support/core_ext/module/delegation"
8
+ require "active_support/core_ext/string/inflections"
9
+
10
+ require "short_circu_it"
11
+
3
12
  require "technologic/version"
13
+ require "technologic/event"
14
+ require "technologic/subscriber/base"
15
+ require "technologic/fatal_subscriber"
16
+ require "technologic/error_subscriber"
17
+ require "technologic/warn_subscriber"
18
+ require "technologic/info_subscriber"
19
+ require "technologic/debug_subscriber"
20
+ require "technologic/logger"
21
+ require "technologic/config_options"
22
+ require "technologic/setup"
4
23
 
5
24
  module Technologic
6
- # Your code goes here...
25
+ extend ActiveSupport::Concern
26
+
27
+ SEVERITIES = %i[debug info warn error fatal].freeze
28
+ EXCEPTION_SEVERITIES = %i[error fatal].freeze
29
+
30
+ included do
31
+ delegate :instrument, :surveil, to: :class
32
+ protected :instrument, :surveil
33
+
34
+ SEVERITIES.each do |severity|
35
+ delegate severity, to: :class
36
+ protected severity # rubocop:disable Style/AccessModifierDeclarations
37
+ end
38
+
39
+ EXCEPTION_SEVERITIES.each do |severity|
40
+ method_name = "#{severity}!"
41
+
42
+ delegate method_name, to: :class
43
+ protected method_name # rubocop:disable Style/AccessModifierDeclarations
44
+ end
45
+ end
46
+
47
+ class_methods do
48
+ def instrument(severity, event, **data, &block)
49
+ ActiveSupport::Notifications.instrument("#{event}.#{name}.#{severity}", data, &block).tap do
50
+ # If a block was defined, :instrument will return the value of the block.
51
+ # Otherwise, :instrument will return nil, since it didn't do anything.
52
+ # Returning true here allows us to do fun things like `info :subscription_created and return subscription`
53
+ return true unless block_given?
54
+ end
55
+ end
56
+
57
+ def surveil(event, severity: :info, **data, &block)
58
+ raise LocalJumpError unless block_given?
59
+
60
+ instrument(severity, "#{event}_started", **data)
61
+ instrument(severity, "#{event}_finished", &block)
62
+ end
63
+
64
+ SEVERITIES.each do |severity|
65
+ define_method(severity) { |event, **data, &block| instrument(severity, event, **data, &block) }
66
+ end
67
+
68
+ EXCEPTION_SEVERITIES.each do |severity|
69
+ define_method("#{severity}!") do |error_class = StandardError, message = nil, **data, &block|
70
+ instrument severity, error_class.name.demodulize, **data, &block
71
+ raise error_class, message
72
+ end
73
+ end
74
+ end
7
75
  end
76
+
77
+ require "technologic/railtie" if defined?(Rails)
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class ConfigOptions
5
+ class_attribute :enabled, default: true
6
+
7
+ class_attribute :subscribe_to_fatal, default: true
8
+ class_attribute :subscribe_to_error, default: true
9
+ class_attribute :subscribe_to_warn, default: true
10
+ class_attribute :subscribe_to_info, default: true
11
+ class_attribute :subscribe_to_debug, default: true
12
+
13
+ class_attribute :log_fatal_events, default: true
14
+ class_attribute :log_error_events, default: true
15
+ class_attribute :log_warn_events, default: true
16
+ class_attribute :log_info_events, default: true
17
+ class_attribute :log_debug_events, default: true
18
+
19
+ class_attribute :include_in_classes, default: %w[ApplicationController]
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class DebugSubscriber < Subscriber::Base; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class ErrorSubscriber < Subscriber::Base; end
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class Event
5
+ include ShortCircuIt
6
+
7
+ attr_reader :name, :duration
8
+
9
+ def initialize(name, started, finished, payload)
10
+ @name = name
11
+ @duration = finished - started
12
+ @payload = payload
13
+ end
14
+
15
+ def data
16
+ {}.tap do |hash|
17
+ hash.merge!(@payload)
18
+ hash[:event] = name
19
+ hash[:duration] = duration if duration.round > 0
20
+ end
21
+ end
22
+ memoize :data
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class FatalSubscriber < Subscriber::Base; end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class InfoSubscriber < Subscriber::Base; end
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class Logger
5
+ class << self
6
+ def log(severity, event)
7
+ return unless defined?(Rails)
8
+
9
+ Rails.logger.public_send(severity) do
10
+ event.data.transform_values { |value| format_value_for_log(value) }
11
+ end
12
+ end
13
+
14
+ def format_value_for_log(value)
15
+ # `#to_log_string` is idiomatic to Technologic and is the most explicit way to specify how an object is logged.
16
+ return value.to_log_string if value.respond_to?(:to_log_string)
17
+
18
+ return value if value.is_a?(Numeric)
19
+ return value.id if value.respond_to?(:id)
20
+ return value.map { |mappable_value| format_value_for_log(mappable_value) } if value.respond_to?(:map)
21
+
22
+ value.to_s
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module Technologic
6
+ class Railtie < Rails::Railtie
7
+ config.technologic = Technologic::ConfigOptions
8
+
9
+ config.after_initialize do |application|
10
+ Technologic::Setup.for(application) if application.config.technologic.enabled
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class Setup
5
+ class << self
6
+ def for(application)
7
+ technologic_config = application.config.technologic
8
+
9
+ setup_subscribers(technologic_config)
10
+ setup_loggers(technologic_config)
11
+ setup_includes(technologic_config)
12
+ end
13
+
14
+ private
15
+
16
+ def setup_subscribers(config)
17
+ ActiveSupport::Notifications.subscribe(%r{\.fatal$}, Technologic::FatalSubscriber) if config.subscribe_to_fatal
18
+ ActiveSupport::Notifications.subscribe(%r{\.error$}, Technologic::ErrorSubscriber) if config.subscribe_to_error
19
+ ActiveSupport::Notifications.subscribe(%r{\.warn$}, Technologic::WarnSubscriber) if config.subscribe_to_warn
20
+ ActiveSupport::Notifications.subscribe(%r{\.info$}, Technologic::InfoSubscriber) if config.subscribe_to_info
21
+ ActiveSupport::Notifications.subscribe(%r{\.debug$}, Technologic::DebugSubscriber) if config.subscribe_to_debug
22
+ end
23
+
24
+ def setup_loggers(config)
25
+ Technologic::FatalSubscriber.on_event { |e| Technologic::Logger.log(:fatal, e) } if config.log_fatal_events
26
+ Technologic::ErrorSubscriber.on_event { |e| Technologic::Logger.log(:error, e) } if config.log_error_events
27
+ Technologic::WarnSubscriber.on_event { |e| Technologic::Logger.log(:warn, e) } if config.log_warn_events
28
+ Technologic::InfoSubscriber.on_event { |e| Technologic::Logger.log(:info, e) } if config.log_info_events
29
+ Technologic::DebugSubscriber.on_event { |e| Technologic::Logger.log(:debug, e) } if config.log_debug_events
30
+ end
31
+
32
+ def setup_includes(config)
33
+ config.include_in_classes.each { |class_name| class_name.constantize.include(Technologic) }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "core"
4
+ require_relative "event_handling"
5
+
6
+ module Technologic
7
+ module Subscriber
8
+ class Base
9
+ include Core
10
+ include EventHandling
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ module Subscriber
5
+ module Core
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def call(name, started, finished, _unique_id, payload)
10
+ trigger Technologic::Event.new(name.chomp(".#{severity}"), started, finished, payload)
11
+ end
12
+
13
+ def severity
14
+ @severity ||= name.demodulize.chomp("Subscriber").downcase.to_sym
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ module Subscriber
5
+ module EventHandling
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :_event_handlers, instance_writer: false, default: []
10
+ end
11
+
12
+ class_methods do
13
+ def on_event(&block)
14
+ _event_handlers << block
15
+ end
16
+
17
+ def trigger(event)
18
+ _event_handlers.each { |handler| handler.call(event) }
19
+ end
20
+
21
+ def inherited(base)
22
+ base._event_handlers = _event_handlers.dup
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Technologic
4
4
  # This constant is managed by spicerack
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.3"
6
6
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Technologic
4
+ class WarnSubscriber < Subscriber::Base; end
5
+ end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: technologic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Garside
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2018-10-10 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: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 5.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: short_circu_it
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.3
13
55
  description: An opinionated philosophy on logging for Ruby on Rails applications.
14
56
  email:
15
57
  - garside@gmail.com
@@ -20,7 +62,20 @@ files:
20
62
  - LICENSE.txt
21
63
  - README.md
22
64
  - lib/technologic.rb
65
+ - lib/technologic/config_options.rb
66
+ - lib/technologic/debug_subscriber.rb
67
+ - lib/technologic/error_subscriber.rb
68
+ - lib/technologic/event.rb
69
+ - lib/technologic/fatal_subscriber.rb
70
+ - lib/technologic/info_subscriber.rb
71
+ - lib/technologic/logger.rb
72
+ - lib/technologic/railtie.rb
73
+ - lib/technologic/setup.rb
74
+ - lib/technologic/subscriber/base.rb
75
+ - lib/technologic/subscriber/core.rb
76
+ - lib/technologic/subscriber/event_handling.rb
23
77
  - lib/technologic/version.rb
78
+ - lib/technologic/warn_subscriber.rb
24
79
  homepage: https://www.freshly.com
25
80
  licenses:
26
81
  - MIT