Class: SignalTrapLogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/signal_trap_logger.rb

Overview

This class is used to log messages to a specified logger from within a ‘Signal.trap` context. Most loggers rely on methods that are prohibited within a `Signal.trap` context, so this class is used to queue up log messages and then log them from a separate thread outside of the `Signal.trap` context.

Example:

Signal.trap("USR1") do
  SignalTrapLogger.instance.log(Rails.logger, "Received USR1 signal")
end

Do note that you need to call ‘SignalTrapLogger.instance.after_fork` after forking a new process to ensure that the logging thread is running in the new process.

Instance Method Summary collapse

Constructor Details

#initializeSignalTrapLogger

Returns a new instance of SignalTrapLogger.



17
18
19
20
# File 'lib/signal_trap_logger.rb', line 17

def initialize
  @queue = Queue.new
  ensure_logging_thread_running
end

Instance Method Details

#after_forkObject



26
27
28
# File 'lib/signal_trap_logger.rb', line 26

def after_fork
  ensure_logging_thread_running
end

#log(logger, message, level: :info) ⇒ Object



22
23
24
# File 'lib/signal_trap_logger.rb', line 22

def log(logger, message, level: :info)
  @queue << { logger:, message:, level: }
end