Class: LogNotify
- Inherits:
-
Object
- Object
- LogNotify
- Defined in:
- lib/lognotify.rb
Instance Method Summary collapse
-
#initialize(logfile, sps_address: nil, sps_port: 59000, sps_topic: 'lognotify') ⇒ LogNotify
constructor
A new instance of LogNotify.
- #on_update(entry) ⇒ Object
- #start ⇒ Object
- #watch ⇒ Object
Constructor Details
#initialize(logfile, sps_address: nil, sps_port: 59000, sps_topic: 'lognotify') ⇒ LogNotify
Returns a new instance of LogNotify.
10 11 12 13 14 15 16 17 |
# File 'lib/lognotify.rb', line 10 def initialize(logfile, sps_address: nil, sps_port: 59000, sps_topic: 'lognotify') @sps = sps_address ? SPSPub.new(address: sps_address, port: sps_port) : nil @sps_topic = sps_topic @command = 'tail -n 1 -F ' + logfile end |
Instance Method Details
#on_update(entry) ⇒ Object
19 20 21 22 23 |
# File 'lib/lognotify.rb', line 19 def on_update(entry) # custom defined end |
#start ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lognotify.rb', line 25 def start() t = Time.now # using the time we can ignore existing entries IO.popen(@command).each_line do |x| # anything after 5 seconds from start is new if Time.now > t + 5 then raw_log_entry = x.lines.last #@sps.notice(@sps_topic + ': ' + json) if @sps on_update(raw_log_entry) end end end |
#watch ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lognotify.rb', line 43 def watch() return unless block_given? t = Time.now # using the time we can ignore existing entries IO.popen(@command).each_line do |x| # anything after 5 seconds from start is new if Time.now > t + 5 then raw_log_entry = x.lines.last #@sps.notice(@sps_topic + ': ' + json) if @sps yield raw_log_entry end end end |