Class: Rollout::Logging::Logger
- Inherits:
-
Object
- Object
- Rollout::Logging::Logger
- Defined in:
- lib/rollout/logging.rb
Constant Summary collapse
- CONTEXT_THREAD_KEY =
:rollout_logging_context- WITHOUT_THREAD_KEY =
:rollout_logging_disabled
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#global ⇒ Object
readonly
Returns the value of attribute global.
-
#history_length ⇒ Object
readonly
Returns the value of attribute history_length.
Instance Method Summary collapse
- #current_context ⇒ Object
- #delete(feature_name) ⇒ Object
- #event_for(before, after) ⇒ Object
- #events(feature_name, limit: nil) ⇒ Object
- #global_events(limit: nil) ⇒ Object
-
#initialize(adapter:, history_length: 50, global: false) ⇒ Logger
constructor
A new instance of Logger.
- #last_event(feature_name) ⇒ Object
- #logging_enabled? ⇒ Boolean
- #updated_at(feature_name) ⇒ Object
- #with_context(context) ⇒ Object
- #without ⇒ Object
Constructor Details
#initialize(adapter:, history_length: 50, global: false) ⇒ Logger
Returns a new instance of Logger.
51 52 53 54 55 |
# File 'lib/rollout/logging.rb', line 51 def initialize(adapter:, history_length: 50, global: false) @adapter = adapter @history_length = history_length @global = global end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
49 50 51 |
# File 'lib/rollout/logging.rb', line 49 def adapter @adapter end |
#global ⇒ Object (readonly)
Returns the value of attribute global.
49 50 51 |
# File 'lib/rollout/logging.rb', line 49 def global @global end |
#history_length ⇒ Object (readonly)
Returns the value of attribute history_length.
49 50 51 |
# File 'lib/rollout/logging.rb', line 49 def history_length @history_length end |
Instance Method Details
#current_context ⇒ Object
126 127 128 |
# File 'lib/rollout/logging.rb', line 126 def current_context Thread.current[CONTEXT_THREAD_KEY] || {} end |
#delete(feature_name) ⇒ Object
73 74 75 |
# File 'lib/rollout/logging.rb', line 73 def delete(feature_name) @adapter.delete_feature_events(feature_name) end |
#event_for(before, after) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rollout/logging.rb', line 77 def event_for(before, after) return unless logging_enabled? before_hash = before.to_hash before_hash.delete(:data).each do |k, v| before_hash["data.#{k}"] = v end after_hash = after.to_hash after_hash.delete(:data).each do |k, v| after_hash["data.#{k}"] = v end keys = before_hash.keys | after_hash.keys change = { before: {}, after: {} } changed_count = 0 keys.each do |key| next if before_hash[key] == after_hash[key] change[:before][key] = before_hash[key] change[:after][key] = after_hash[key] changed_count += 1 end return if changed_count == 0 Event.new( feature: after.name, name: :update, data: change, context: current_context, created_at: Time.now, ) end |
#events(feature_name, limit: nil) ⇒ Object
65 66 67 |
# File 'lib/rollout/logging.rb', line 65 def events(feature_name, limit: nil) @adapter.feature_events(feature_name, limit: limit) end |
#global_events(limit: nil) ⇒ Object
69 70 71 |
# File 'lib/rollout/logging.rb', line 69 def global_events(limit: nil) @adapter.global_events(limit: limit) end |
#last_event(feature_name) ⇒ Object
61 62 63 |
# File 'lib/rollout/logging.rb', line 61 def last_event(feature_name) events(feature_name, limit: 1).last end |
#logging_enabled? ⇒ Boolean
138 139 140 |
# File 'lib/rollout/logging.rb', line 138 def logging_enabled? !Thread.current[WITHOUT_THREAD_KEY] end |
#updated_at(feature_name) ⇒ Object
57 58 59 |
# File 'lib/rollout/logging.rb', line 57 def updated_at(feature_name) @adapter.feature_updated_at(feature_name) end |
#with_context(context) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/rollout/logging.rb', line 116 def with_context(context) raise ArgumentError, "context must be a Hash" unless context.is_a?(Hash) raise ArgumentError, "block is required" unless block_given? Thread.current[CONTEXT_THREAD_KEY] = context yield ensure Thread.current[CONTEXT_THREAD_KEY] = nil end |
#without ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/rollout/logging.rb', line 130 def without previous = Thread.current[WITHOUT_THREAD_KEY] Thread.current[WITHOUT_THREAD_KEY] = true yield ensure Thread.current[WITHOUT_THREAD_KEY] = previous end |