Method: Console::Output::Serialized#call

Defined in:
lib/console/output/serialized.rb

#call(subject = nil, *arguments, severity: UNKNOWN, **options, &block) ⇒ Object

Output the given log message.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/console/output/serialized.rb', line 50

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
  record = {
    time: Time.now.iso8601,
    severity: severity,
    process_id: Process.pid,
    fiber_id: Fiber.current.object_id,
  }
  
  # For backwards compatibility:
  record[:pid] = record[:process_id]
  
  # We want to log just a brief subject:
  if subject.is_a?(String)
    record[:subject] = subject
  elsif subject.is_a?(Module)
    record[:subject] = subject.name
  else
    record[:subject] = subject.class.name
    record[:object_id] = subject.object_id
  end
  
  if annotation = Fiber.current.annotation
    record[:annotation] = annotation
  end
  
  message = arguments
  
  if block_given?
    if block.arity.zero?
      message << yield
    else
      buffer = StringIO.new
      yield buffer
      message << buffer.string
    end
  end
  
  if message.size == 1
    record[:message] = message.first
  elsif message.any?
    record[:message] = message
  end
  
  record.update(options)
  
  @stream.write(self.dump(record) << "\n")
end