Class: Currentsh::LogOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/currentsh/log_output.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout) ⇒ LogOutput

Returns a new instance of LogOutput.



3
4
5
# File 'lib/currentsh/log_output.rb', line 3

def initialize(io=$stdout)
  @io = io
end

Class Method Details

.with_context(msg) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/currentsh/log_output.rb', line 15

def self.with_context(msg)
  Thread.current[:currrentsh_sidekiq_context] ||= []
  Thread.current[:currrentsh_sidekiq_context] << msg
  yield
ensure
  Thread.current[:currrentsh_sidekiq_context].pop
end

Instance Method Details

#<<(data) ⇒ Object



11
12
13
# File 'lib/currentsh/log_output.rb', line 11

def <<(data)
  @io << format(data)
end

#call(severity, time, program_name, message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/currentsh/log_output.rb', line 23

def call(severity, time, program_name, message)
  data = {
    time: time,
    process: ::Process.pid,
    thread: Thread.current.object_id.to_s(36),
    message: message
  }

  if ctx = context
    data.merge! ctx
  end

  format data
end

#contextObject



38
39
40
# File 'lib/currentsh/log_output.rb', line 38

def context
  Thread.current[:currrentsh_sidekiq_context]
end

#format(data) ⇒ Object



7
8
9
# File 'lib/currentsh/log_output.rb', line 7

def format(data)
  "@current: #{data.to_json}\n"
end