Class: Notifu::Logger

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

Constant Summary collapse

LEVELS =
[ "debug",
"info",
"notice",
"warning",
"error",
"critical",
"alert",
"emergency" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Logger

Returns a new instance of Logger.


18
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
# File 'lib/notifu/logger.rb', line 18

def initialize (mod)
  @syslog_enabled = Notifu::CONFIG[:logging][:syslog][:enabled]
  @elasticsearch_enabled = Notifu::CONFIG[:logging][:elasticsearch][:enabled]

  @logger = Log4r::Logger.new 'notifu'

  if self.syslog_enabled
    begin
      @logger.outputters = Log4r::SyslogOutputter.new "notifu", ident: "notifu-#{mod}"
      log "info", "Syslog socket opened"
    rescue
      @logger.outputters = Log4r::Outputter.stdout
      log "error", "Failed to open local syslog socket, using STDOUT"
    end
  else
    log "info", "Syslog disabled"
    @logger.outputters = Log4r::Outputter.stdout
  end

  if self.elasticsearch_enabled
    begin
      @es = Elasticsearch::Client.new hosts: Notifu::CONFIG[:logging][:elasticsearch][:conn], retry_on_failure: false, transport_options: { request: { timeout: Notifu::CONFIG[:logging][:elasticsearch][:timeout] || 10 } }
      log "info", "Action log output to ElasticSearch - " + Notifu::CONFIG[:logging][:elasticsearch][:conn].to_json
    rescue
      @es = false
      log "error", "Failed to connect to ElasticSearch"
      exit 1
    end
  else
    log "info", "ElasticSearch action logging disabled"
    @es = false
  end

end

Instance Attribute Details

#elasticsearch_enabledObject (readonly)

Returns the value of attribute elasticsearch_enabled


5
6
7
# File 'lib/notifu/logger.rb', line 5

def elasticsearch_enabled
  @elasticsearch_enabled
end

#esObject (readonly)

Returns the value of attribute es


6
7
8
# File 'lib/notifu/logger.rb', line 6

def es
  @es
end

#syslogObject (readonly)

Returns the value of attribute syslog


7
8
9
# File 'lib/notifu/logger.rb', line 7

def syslog
  @syslog
end

#syslog_enabledObject (readonly)

Returns the value of attribute syslog_enabled


4
5
6
# File 'lib/notifu/logger.rb', line 4

def syslog_enabled
  @syslog_enabled
end

Instance Method Details

#action_log(type, event) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/notifu/logger.rb', line 53

def action_log (type, event)
  if self.elasticsearch_enabled && self.es
    index_name = "notifu-" + Time.now.strftime("%Y.%m.%d").to_s
    begin
      self.es.index index: index_name, type: type, body: event
    rescue Faraday::TimeoutError
      log "error", "Action log action failed: ElasticSearch timeout"
      log "info", "Action log: (#{type}) #{event.to_json}"
    end
  else
    log "debug", "Action log: #{type}"
    log "debug", "Action log: (#{type}) #{event.to_json}"
  end
end

#log(prio, msg) ⇒ Object


68
69
70
# File 'lib/notifu/logger.rb', line 68

def log (prio, msg)
  @logger.send prio.to_sym, msg
end