lib/syslog-sd/notifier.rb in syslog-sd-1.2.4 vs lib/syslog-sd/notifier.rb in syslog-sd-1.3.1

- old
+ new

@@ -1,9 +1,9 @@ module SyslogSD # syslog notifier. class Notifier - attr_accessor :enabled, :collect_file_and_line, :timestamp_as_float + attr_accessor :enabled, :collect_file_and_line, :rescue_network_errors, :timestamp_as_float attr_reader :level, :default_options, :level_mapping # +host+ and +port+ are host/ip and port of syslog server. # +default_options+ is used in notify! def initialize(host = 'localhost', port = 514, default_options = {}) @@ -11,10 +11,11 @@ @collect_file_and_line = true @sd_id = "_@37797" self.level = SyslogSD::DEBUG self.timestamp_as_float = false + self.rescue_network_errors = false self.default_options = default_options self.default_options['host'] ||= Socket.gethostname self.default_options['level'] ||= SyslogSD::UNKNOWN self.default_options['facility'] ||= 'syslog-sd-rb' @@ -108,12 +109,15 @@ end private def notify_with_level(message_level, *args) notify_with_level!(message_level, *args) + rescue SocketError + raise unless self.rescue_network_errors rescue Exception => exception notify_with_level!(SyslogSD::UNKNOWN, exception) + exception end def notify_with_level!(message_level, *args) return unless @enabled extract_hash(*args) @@ -137,10 +141,11 @@ end @hash = default_options.merge(self.class.stringify_keys(args.merge(primary_data))) convert_hoptoad_keys_to_graylog2 set_file_and_line if @collect_file_and_line + set_timestamp check_presence_of_mandatory_attributes @hash end def self.extract_hash_from_exception(exception) @@ -166,9 +171,13 @@ frame = stack.shift end while frame.include?(LIB_PATTERN) match = CALLER_REGEXP.match(frame) @hash['file'] = match[1] @hash['line'] = match[2].to_i + end + + def set_timestamp + @hash['timestamp'] = Time.now.utc.to_f if @hash['timestamp'].nil? end def check_presence_of_mandatory_attributes %w(short_message host).each do |attribute| if @hash[attribute].to_s.empty?