Sha256: 6e1094384f6d414cbd5e0faf1fec2ef6df9befe6f11e596a174a076181ab395b
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
require 'securerandom' class StructuredEventLogger class Error < ::StandardError; end class EndpointException < StructuredEventLogger::Error attr_reader :name, :wrapped_exception def initialize(name, wrapped_exception) @name, @wrapped_exception = name, wrapped_exception super("Endpoint #{name} failed - #{wrapped_exception.class.name}: #{wrapped_exception.message}") end end attr_reader :endpoints, :default_context 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 = {}) log_event scope, event, flatten_hash(content) rescue EndpointException => 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) endpoints.each do |name, endpoint| begin endpoint.call(scope, event, hash, record) rescue => e raise EndpointException.new(name, e) end end 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
structured-event-logger-0.1.0 | lib/structured_event_logger.rb |