Class: RocketJob::Subscribers::Logger

Inherits:
Object
  • Object
show all
Includes:
RocketJob::Subscriber
Defined in:
lib/rocket_job/subscribers/logger.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from RocketJob::Subscriber

#process_action, #process_event, test_mode!, test_mode?

Class Attribute Details

.host_nameObject



8
9
10
# File 'lib/rocket_job/subscribers/logger.rb', line 8

def self.host_name
  @host_name ||= Socket.gethostname
end

Instance Method Details

#set(level: :info, class_name: nil, host_name: nil, pid: nil) ⇒ Object

Change the log level

Examples:

# Change the global log level to :trace on all servers.
RocketJob::Subscribers::Logger.publish(:set, level: :trace)

# Change the global log level to :trace on one server.
RocketJob::Subscribers::Logger.publish(:set, level: :trace, host_name: 'server1.company.com')

# Change the global log level to :trace for a specific process id.
RocketJob::Subscribers::Logger.publish(:set, level: :trace, host_name: 'server1.company.com', pid: 34567)

# Change the log level for a specific class to :trace.
RocketJob::Subscribers::Logger.publish(:set, level: :trace, class_name: 'RocketJob::Supervisor')


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rocket_job/subscribers/logger.rb', line 30

def set(level: :info, class_name: nil, host_name: nil, pid: nil)
  return unless for_me?(host_name, pid)

  if class_name
    class_name.constantize.logger.level = level
    logger.info "Changed log level to #{level} for #{class_name}"
  else
    SemanticLogger.default_level = level
    logger.info "Changed global log level to #{level}"
  end
end

#thread_dump(host_name: nil, pid: nil) ⇒ Object

Dump all backtraces to the log file.

Examples:

# Thread dump on all servers:
RocketJob::Subscribers::Logger.publish(:thread_dump)

# Change the global log level to :trace on one server.
RocketJob::Subscribers::Logger.publish(:thread_dump, host_name: 'server1.company.com')

# Change the global log level to :trace for a specific process id.
RocketJob::Subscribers::Logger.publish(:thread_dump, host_name: 'server1.company.com', pid: 34567)


53
54
55
56
57
58
59
60
61
# File 'lib/rocket_job/subscribers/logger.rb', line 53

def thread_dump(host_name: nil, pid: nil)
  return unless for_me?(host_name, pid)

  Thread.list.each do |thread|
    next if thread == Thread.current

    logger.backtrace(thread: thread)
  end
end