Class: SweetLogger::Logstash

Inherits:
Object
  • Object
show all
Defined in:
lib/sweet_logger/logstash.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.silence(routes) ⇒ Object



11
12
13
# File 'lib/sweet_logger/logstash.rb', line 11

def self.silence(routes)
  @silencer_re = /#{ routes.map { |e| Regexp.escape(e) }.join("|") }/
end

.silencer_reObject



15
16
17
# File 'lib/sweet_logger/logstash.rb', line 15

def self.silencer_re
  @silencer_re
end

Instance Method Details

#flush(entries) ⇒ Object



55
56
57
58
59
# File 'lib/sweet_logger/logstash.rb', line 55

def flush(entries)
  entries.each do |logger, args, block|
    logger.add_before_sweet_logger *args, &block
  end
end

#shall_be_silenced?(path) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sweet_logger/logstash.rb', line 61

def shall_be_silenced?(path)
  path =~ self.class.silencer_re
end

#wrap(path, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
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/sweet_logger/logstash.rb', line 19

def wrap(path, &block)
  silenced = shall_be_silenced?(path)
  we_started_this = ! Thread.main[:sweet_logger_stash]

  MUTEX.synchronize do
    if we_started_this && ! silenced
      Thread.main[:sweet_logger_stash] = []
    else
      Thread.current[:sweet_logger_stash] = []
    end
  end

  yield

ensure
  MUTEX.synchronize do
    if silenced || ! Thread.current[:sweet_logger_stash]
      Thread.current[:sweet_logger_stash] = []
    end

    if Thread.main[:sweet_logger_stash]
      if we_started_this
        flush Thread.main[:sweet_logger_stash]
        Thread.main[:sweet_logger_stash] = nil
      else
        Thread.main[:sweet_logger_stash] += Thread.current[:sweet_logger_stash]
      end

    elsif
      flush Thread.current[:sweet_logger_stash]
    end

    Thread.current[:sweet_logger_stash] = nil
  end
end