Class: Sensu::Logger::Stream

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStream

Initialize a log stream, redirect STDERR to STDOUT, create log level methods, and setup the reactor log event writer.


16
17
18
19
20
21
22
23
24
# File 'lib/sensu/logger/stream.rb', line 16

def initialize
  @stream = []
  @stream_callbacks = []
  @level = :info
  STDOUT.sync = true
  STDERR.reopen(STDOUT)
  self.class.create_level_methods
  setup_writer
end

Instance Attribute Details

#levelObject


12
13
14
# File 'lib/sensu/logger/stream.rb', line 12

def level
  @level
end

Class Method Details

.create_level_methodsObject

Create a method for each of the log levels, they call add() to add log events to the log stream.


28
29
30
31
32
33
34
# File 'lib/sensu/logger/stream.rb', line 28

def self.create_level_methods
  LEVELS.each do |level|
    define_method(level) do |*args|
      add(level, *args)
    end
  end
end

Instance Method Details

#add(level, *args) ⇒ TrueClass, FalseClass

Add a log event to the log stream.

Parameters:

  • level (Symbol)

    log event level.

  • args (Array)

    to pass to create_log_event().

Returns:

  • (TrueClass, FalseClass)

    if the log event was added.


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sensu/logger/stream.rb', line 49

def add(level, *args)
  unless level_filtered?(level)
    event = create_log_event(level, *args)
    if EM.reactor_running?
      schedule_write(event)
    else
      safe_write(event)
    end
    true
  else
    false
  end
end

#level_filtered?(level) ⇒ TrueClass, FalseClass

Check to see if a log level is currently being filtered.

Parameters:

  • level (Symbol)

    log event level.

Returns:

  • (TrueClass, FalseClass)

40
41
42
# File 'lib/sensu/logger/stream.rb', line 40

def level_filtered?(level)
  LEVELS.index(level) < LEVELS.index(@level)
end

#reopen(target) ⇒ Object

Reopen the log stream output, write log events to a file.

Parameters:

  • target (IO, String)

    IO stream or file path.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sensu/logger/stream.rb', line 66

def reopen(target)
  @reopen = target
  case target
  when IO
    STDOUT.reopen(target)
    STDOUT.sync = true
    STDERR.reopen(STDOUT)
  when String
    if File.writable?(target) || !File.exist?(target) && File.writable?(File.dirname(target))
      STDOUT.reopen(target, "a")
      STDOUT.sync = true
      STDERR.reopen(STDOUT)
    else
      error("log file is not writable", {
        :log_file => target
      })
    end
  end
end

#setup_signal_trapsObject

Setup signal traps for the log stream. Signals:

TRAP: toggle debug logging.
USR2: reopen the log file.

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sensu/logger/stream.rb', line 90

def setup_signal_traps
  if Signal.list.include?("TRAP")
    Signal.trap("TRAP") do
      @level = case @level
      when :debug
        @previous_level || :info
      else
        @previous_level = @level
        :debug
      end
    end
  end
  if Signal.list.include?("USR2")
    Signal.trap("USR2") do
      if @reopen
        reopen(@reopen)
      end
    end
  end
end