Class: ManageIQ::Loggers::Journald

Inherits:
Base
  • Object
show all
Defined in:
lib/manageiq/loggers/journald.rb

Defined Under Namespace

Classes: Formatter

Constant Summary

Constants inherited from Base

Base::MAX_LOG_LINE_LENGTH

Instance Attribute Summary

Attributes inherited from Base

#logdev

Instance Method Summary collapse

Methods inherited from Base

#level, #log_backtrace, log_hashes, #log_hashes, log_hashes_filter, #log_hashes_filter, log_hashes_filter=, #log_hashes_filter=, #silence

Constructor Details

#initialize(_logdev = nil, *_, **_) ⇒ Journald

Create and return a new ManageIQ::Loggers::Journald instance. The arguments to the initializer can be ignored unless you’re multicasting.

Internally we set our own formatter, and automatically set the progname option to ‘manageiq’ if not specified.



10
11
12
13
14
15
# File 'lib/manageiq/loggers/journald.rb', line 10

def initialize(_logdev = nil, *_, **_)
  require "systemd-journal"
  super
  @formatter = Formatter.new
  @progname ||= 'manageiq'
end

Instance Method Details

#add(severity, message = nil, progname = nil) ⇒ Object

Redefine this method from the core Logger class. Internally this is the method used when .info, .warn, etc are called.



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/manageiq/loggers/journald.rb', line 27

def add(severity, message = nil, progname = nil)
  severity ||= Logger::UNKNOWN
  return true if severity < @level

  progname ||= @progname

  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end

  message = formatter.call(format_severity(severity), progname, message)
  caller_object = caller_locations(3, 1).first

  Systemd::Journal.message(
    :message           => message,
    :priority          => log_level_map[severity],
    :syslog_identifier => progname,
    :code_line         => caller_object.lineno,
    :code_file         => caller_object.absolute_path,
    :code_func         => caller_object.label
  )
end

#filenameObject

Comply with the VMDB::Logger interface. For a filename we simply use the string ‘journald’ since we’re not using a backing file directly.



20
21
22
# File 'lib/manageiq/loggers/journald.rb', line 20

def filename
  "journald"
end