Class: FnordMetric::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Logger

Returns a new instance of Logger.



23
24
25
26
27
28
# File 'lib/fnordmetric/logger.rb', line 23

def initialize(opts)
  @opts = opts
  opts.fetch(:file)

  FnordMetric.register(self)
end

Class Method Details

.import(logfile_path) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fnordmetric/logger.rb', line 3

def self.import(logfile_path)
  expire = FnordMetric.options[:event_queue_ttl]
  redis  = Redis.new
 
  @opts[:channels] ||= []
  @opts[:channels] = @opts[:channels].map(&:to_s) 

  dump_file = File.open(logfile_path, 'r')
  num_lines = %x{wc -l #{logfile_path}}.to_i
  puts "importing #{num_lines} events..."

  dump_file.each_with_log(num_lines) do |line, ind|
    (8**64).to_s(36).tap do |uuid|
      redis.set    "fnordmetric-event-#{uuid}", line
      redis.lpush  "fnordmetric-queue"        , uuid
      redis.expire "fnordmetric-event-#{uuid}", expire
    end
  end
end

Instance Method Details

#initializedObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fnordmetric/logger.rb', line 30

def initialized 
  logfile_path = @opts[:file]

  events = Queue.new
  dump_file = File.open(logfile_path, 'a+')

  fetcher = Thread.new do
    loop do
      event = events.pop

      dump_file.write(event.to_json+"\n")
      dump_file.flush
    end
  end

  listener = Thread.new do  
    backend = FnordMetric.backend
    backend.subscribe do |event|
      events << event if log_channel?(event["_channel"])
    end
  end

  FnordMetric.log "logging to #{logfile_path}"
end