Class: Anoubis::LogService

Inherits:
Object
  • Object
show all
Defined in:
app/services/anoubis/log_service.rb

Overview

Graylog logging service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogService

Setups basic initialization parameters.



50
51
52
53
54
55
# File 'app/services/anoubis/log_service.rb', line 50

def initialize
  self.logger = GELF::Notifier.new(url, port, 'WAN', { facility: facility })
  logger.collect_file_and_line = false
  logger.rescue_network_errors = true
  self.perm = {}
end

Instance Attribute Details

#loggerObject

GELF::Notifier service



5
6
7
# File 'app/services/anoubis/log_service.rb', line 5

def logger
  @logger
end

#permObject

Hash of permanent parameters that added before sending data to Graylog server



7
8
9
# File 'app/services/anoubis/log_service.rb', line 7

def perm
  @perm
end

Instance Method Details

#facilityString

Returns Graylog facility identifier in input source

Returns:

  • (String)

    Graylog facility identifier



38
39
40
41
42
43
44
45
46
# File 'app/services/anoubis/log_service.rb', line 38

def facility
  begin
    value = Rails.configuration.graylog_facility
  rescue StandardError
    value = 'Graylog'
  end

  value
end

#level(type) ⇒ GELF::Levels

Returns GELF::Levels according by type

Parameters:

  • type (String)

    Log level (‘debug’, ‘error’, ‘info’, ‘warn’)

Returns:

  • (GELF::Levels)

    GELF::Levels



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/services/anoubis/log_service.rb', line 83

def level(type)
  case type
  when 'debug'
    return GELF::DEBUG
  when 'info'
    return GELF::INFO
  when 'warn'
    return GELF::WARN
  when 'error'
    return GELF::ERROR
  end

  GELF::UNKNOWN
end

#log(text, type = 'info', object = nil) ⇒ Object

Send data to Graylog server

Parameters:

  • text (String)

    Logged text data

  • type (String) (defaults to: 'info')

    Log level (‘debug’, ‘error’, ‘info’, ‘warn’)

  • object (Hash) (defaults to: nil)

    Additional parameters for logged data



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/anoubis/log_service.rb', line 62

def log(text, type = 'info', object = nil)
  type = 'info' unless %w[info warn error].include? type.downcase
  loc = caller_locations(1, 1).first

  data = {
    short_message: text,
    level: level(type),
    line: loc.lineno,
    file: loc.path
  }

  data.merge!(object) if object
  data.merge!(self.perm)
  logger.notify data
  nil
end

#portString

Returns Graylog server port

Returns:

  • (String)

    Graylog server port



25
26
27
28
29
30
31
32
33
# File 'app/services/anoubis/log_service.rb', line 25

def port
  begin
    value = Rails.configuration.graylog_port
  rescue StandardError
    value = 12201
  end

  value
end

#urlString

Returns Graylog server URL

Returns:

  • (String)

    Graylog server URL



12
13
14
15
16
17
18
19
20
# File 'app/services/anoubis/log_service.rb', line 12

def url
  begin
    value = Rails.configuration.graylog_server
  rescue StandardError
    value = '127.0.0.1'
  end

  value
end