Sha256: a91ec143798f3d32e7c45e3ddb8ba67ac803e7fc6f149910f97f5474f6904f93
Contents?: true
Size: 1.99 KB
Versions: 3
Compression:
Stored size: 1.99 KB
Contents
require 'securerandom' class StructuredEventLogger class Error < ::StandardError; end class EventHandlingException < StructuredEventLogger::Error attr_reader :exceptions def initialize(scope, name, exceptions) @scope, @name, @exceptions = scope, name, exceptions super("Failed to submit the #{scope}/#{name} event to the following endpoints: #{exceptions.keys.join(", ")}") end end attr_reader :endpoints, :default_context attr_accessor :only attr_accessor :error_handler def initialize(endpoints = {}) @endpoints = endpoints @thread_contexts = {} @default_context = {} @error_handler = lambda { |exception| raise(exception) } end 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 def context @thread_contexts[thread_key] ||= {} end private 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 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 def thread_key Thread.current end end require 'structured_event_logger/syslogger' require 'structured_event_logger/json_writer' require 'structured_event_logger/human_readable_logger'
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
structured-event-logger-0.1.5 | lib/structured_event_logger.rb |
structured-event-logger-0.1.4 | lib/structured_event_logger.rb |
structured-event-logger-0.1.3 | lib/structured_event_logger.rb |