Class: Loggun::Modifiers::Sidekiq::LoggunFormatter

Inherits:
Sidekiq::Logger::Formatters::Base
  • Object
show all
Defined in:
lib/loggun/modifiers/sidekiq/sidekiq4.rb,
lib/loggun/modifiers/sidekiq/sidekiq6.rb

Instance Method Summary collapse

Instance Method Details

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



5
6
7
8
# File 'lib/loggun/modifiers/sidekiq/sidekiq4.rb', line 5

def call(severity, time, _program_name, message)
  message, loggun_type = prepared_message(message)
  Loggun::Formatter.new.call(severity, time.utc, nil, message, loggun_type: loggun_type)
end

#prepared_message(message) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/loggun/modifiers/sidekiq/sidekiq4.rb', line 10

def prepared_message(message)
  if %w[start].include?(message) || message[/^(done|fail):\s(.*)\ssec$/]
    message, elapsed = split_msg_and_time(message)
    loggun_type = "sidekiq.job.#{message}"
    message = { tid: "#{Thread.current.object_id.to_s(36)}", context: context.strip }
    message[:elapsed] = elapsed if elapsed
  else
    loggun_type = 'app.sidekiq.control'
    message = { tid: Thread.current.object_id.to_s(36), message: message }
    message.merge!(context: context) if context
  end

  [message, loggun_type]
end

#split_msg_and_time(message) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/loggun/modifiers/sidekiq/sidekiq4.rb', line 25

def split_msg_and_time(message)
  unless message[/^done:\s(.*)\ssec$/] || message[/^fail:\s(.*)\ssec$/]
    return [message, nil]
  end

  msg_type = message[/^done:\s(.*)\ssec$/] ? 'done' : 'fail'

  msg = message[/#{msg_type}:\s(.*)\ssec/] ? msg_type : message
  elapsed = message.gsub(/#{msg_type}:\s/, '').gsub('sec', '').strip
  [msg, elapsed]
end