Class: Systemd::JournalEntry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/systemd/journal_entry.rb

Overview

Represents a single entry in the Journal.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, context = {}) ⇒ JournalEntry

Create a new JournalEntry from the given entry hash. You probably don’t need to construct this yourself; instead instances are returned from Systemd::Journal methods such as Systemd::Journal#current_entry.

Parameters:

  • entry (Hash)

    a hash containing all the key-value pairs associated with a given journal entry.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/systemd/journal_entry.rb', line 13

def initialize(entry, context = {})
  inspect = []
  @entry  = entry
  @ctx    = context
  @fields = entry.map do |key, value|
    name = key.downcase.to_sym
    define_singleton_method(name) { value } unless respond_to?(name)

    inspect.push("#{name}: '#{value}'")
    name
  end
  @inspect = inspect.join(', ')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



43
44
45
46
47
# File 'lib/systemd/journal_entry.rb', line 43

def method_missing(m, *args)
  # not all journal entries will have all fields.  don't raise an error
  # unless the user passed arguments.
  super(m, *args) unless args.empty?
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/systemd/journal_entry.rb', line 6

def fields
  @fields
end

Instance Method Details

#[](key) ⇒ String

Get the value of a given field in the entry, or nil if it doesn’t exist

Parameters:

  • the (String)

    field name for which to look up the value. Can be a symbol or string, case insensitive.

Returns:

  • (String)

    the value for the given field, or nil if not found.



53
54
55
# File 'lib/systemd/journal_entry.rb', line 53

def [](key)
  @entry[key] || @entry[key.to_s.upcase]
end

#catalog(opts = {}) ⇒ String

Returns the catalog message that this Journal Entry references, if any.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :replace (Boolean)

    set to false to not replace placeholder strings in the catalog with the associated values in this Journal Entry. defaults to true.

Returns:

  • (String)

    the catalog provided message for this Journal Entry, or nil if non exists.



71
72
73
74
75
76
77
78
79
80
# File 'lib/systemd/journal_entry.rb', line 71

def catalog(opts = {})
  return nil unless catalog?

  opts[:replace] = true unless opts.key?(:replace)

  cat = Systemd::Journal.catalog_for(self[:message_id])
  # catalog_for does not do field substitution for us, so we do it here
  # if requested
  opts[:replace] ? field_substitute(cat) : cat
end

#catalog?Boolean

Returns true if this Journal Entry has an associated catalog message.

Returns:

  • (Boolean)

    whether or not this entry has an associated catalog message.



85
86
87
# File 'lib/systemd/journal_entry.rb', line 85

def catalog?
  !self[:message_id].nil?
end

#eachEnumerator

Yields each field name and value pair to the provided block. If no block is given, returns an enumerator.

Returns:

  • (Enumerator)


60
61
62
63
# File 'lib/systemd/journal_entry.rb', line 60

def each
  return to_enum(:each) unless block_given?
  @entry.each { |key, value| yield [key, value] }
end

#inspectObject



96
97
98
# File 'lib/systemd/journal_entry.rb', line 96

def inspect
  format('#<%s:0x%016x %s>', self.class.name, object_id, @inspect)
end

#monotonic_timestampTime

Returns the monotonic time (time since boot) that this entry was received by the journal. This should be associated with a boot_id.

Returns:

  • (Time)


37
38
39
40
# File 'lib/systemd/journal_entry.rb', line 37

def monotonic_timestamp
  return nil unless @ctx.key?(:monotonic_ts)
  @monotonic_timestamp ||= Time.at(0, @ctx[:monotonic_ts].first)
end

#realtime_timestampTime

Returns the wall-clock time that this entry was received by the journal.

Returns:

  • (Time)


29
30
31
32
# File 'lib/systemd/journal_entry.rb', line 29

def realtime_timestamp
  return nil unless @ctx.key?(:realtime_ts)
  @realtime_timestamp ||= Time.at(0, @ctx[:realtime_ts])
end

#to_hHash

Convert this Entry into a hash.

Returns:

  • (Hash)

    the hash representation of this journal entry.



91
92
93
# File 'lib/systemd/journal_entry.rb', line 91

def to_h
  @entry.each_with_object({}) { |(k, v), h| h[k] = v.dup }
end