class StructuredEventLogger
Constants
- VERSION
Attributes
default_context[R]
endpoints[R]
error_handler[RW]
only[RW]
Public Class Methods
new(endpoints = {})
click to toggle source
# File lib/structured_event_logger.rb, line 20 def initialize(endpoints = {}) @endpoints = endpoints @thread_contexts = {} @default_context = {} @error_handler = lambda { |exception| raise(exception) } end
Public Instance Methods
context()
click to toggle source
# File lib/structured_event_logger.rb, line 36 def context @thread_contexts[thread_key] ||= {} end
event(scope, event, content = {})
click to toggle source
# File lib/structured_event_logger.rb, line 29 def event(scope, event, content = {}) return unless @only.nil? || @only.call(scope, event, content) log_event scope, event, flatten_hash(content) rescue EventHandlingException => e error_handler.call(e) end
Private Instance Methods
flatten_hash(hash, keys = nil, separator = "_")
click to toggle source
# File lib/structured_event_logger.rb, line 42 def flatten_hash(hash, keys = nil, separator = "_") flat_hash = {} hash.each_pair do |key, val| conc_key = keys.nil? ? key : "#{keys}#{separator}#{key}" if val.is_a?(Hash) flat_hash.merge!(flatten_hash(val, conc_key)) else flat_hash[conc_key] = val end end flat_hash end
log_event(scope, event, hash)
click to toggle source
# File lib/structured_event_logger.rb, line 55 def log_event(scope, event, hash) record = @default_context.merge(context) record.update(event_name: event, event_scope: scope, event_uuid: SecureRandom.uuid, event_timestamp: Time.now.utc) record.update(hash) exceptions = {} endpoints.each do |name, endpoint| begin endpoint.call(scope, event, hash, record) rescue => e exceptions[name] = e end end raise EventHandlingException.new(scope, event, exceptions) unless exceptions.empty? record end
thread_key()
click to toggle source
# File lib/structured_event_logger.rb, line 73 def thread_key Thread.current end