Class: God::Logger

Inherits:
SimpleLogger show all
Defined in:
lib/god/logger.rb

Constant Summary collapse

SYSLOG_EQUIVALENTS =
{:fatal => :crit,
:error => :err,
:warn => :debug,
:info => :debug,
:debug => :debug}

Constants inherited from SimpleLogger

SimpleLogger::DEBUG, SimpleLogger::ERROR, SimpleLogger::FATAL, SimpleLogger::INFO, SimpleLogger::SEV_LABEL, SimpleLogger::WARN

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from SimpleLogger

#datetime_format, #level

Instance Method Summary collapse

Methods inherited from SimpleLogger

#debug, #error, #fatal, #info, #output, #warn

Constructor Details

#initializeLogger

Instantiate a new Logger object



19
20
21
22
23
24
25
26
27
28
# File 'lib/god/logger.rb', line 19

def initialize
  super($stdout)
  self.logs = {}
  @mutex = Mutex.new
  @capture = nil
  @templogio = StringIO.new
  @templog = SimpleLogger.new(@templogio)
  @templog.level = Logger::INFO
  load_syslog
end

Class Attribute Details

.syslogObject

Returns the value of attribute syslog.



13
14
15
# File 'lib/god/logger.rb', line 13

def syslog
  @syslog
end

Instance Attribute Details

#logsObject

Returns the value of attribute logs.



10
11
12
# File 'lib/god/logger.rb', line 10

def logs
  @logs
end

Instance Method Details

#finish_captureObject

Disable capturing of log and return what was captured since capturing was enabled with Logger#start_capture

Returns String



111
112
113
114
115
116
117
# File 'lib/god/logger.rb', line 111

def finish_capture
  @mutex.synchronize do
    cap = @capture.string
    @capture = nil
    cap
  end
end

#load_syslogObject

If Logger.syslog is true then attempt to load the syslog bindings. If syslog cannot be loaded, then set Logger.syslog to false and continue.

Returns nothing



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/god/logger.rb', line 34

def load_syslog
  return unless Logger.syslog
  
  begin
    require 'syslog'
    
    # Ensure that Syslog is open
    begin
      Syslog.open('god')
    rescue RuntimeError
      Syslog.reopen('god')
    end
  rescue Exception
    Logger.syslog = false
  end
end

#log(watch, level, text) ⇒ Object

Log a message

+watch+ is the String name of the Watch (may be nil if not Watch is applicable)
+level+ is the log level [:debug|:info|:warn|:error|:fatal]
+text+ is the String message

Returns nothing



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/god/logger.rb', line 57

def log(watch, level, text)
  # initialize watch log if necessary
  self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT) if watch
  
  # push onto capture and timeline for the given watch
  @templogio.truncate(0)
  @templogio.rewind
  @templog.send(level, text % [])
  @mutex.synchronize do
    @capture.puts(@templogio.string.dup) if @capture
    self.logs[watch.name] << [Time.now, @templogio.string.dup] if watch
  end
  
  # send to regular logger
  self.send(level, text % [])
  
  # send to syslog
  Syslog.send(SYSLOG_EQUIVALENTS[level], text) if Logger.syslog
end

#start_captureObject

Enable capturing of log

Returns nothing



101
102
103
104
105
# File 'lib/god/logger.rb', line 101

def start_capture
  @mutex.synchronize do
    @capture = StringIO.new
  end
end

#watch_log_since(watch_name, since) ⇒ Object

Get all log output for a given Watch since a certain Time.

+watch_name+ is the String name of the Watch
+since+ is the Time since which to fetch log lines

Returns String



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/god/logger.rb', line 82

def watch_log_since(watch_name, since)
  # initialize watch log if necessary
  self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
  
  # get and join lines since given time
  @mutex.synchronize do
    self.logs[watch_name].select do |x|
      x.first > since
    end.map do |x|
      x[1]
    end.join
  end
end