module SemanticLogger

def self.add_signal_handler(log_level_signal = "USR2", thread_dump_signal = "TTIN", gc_log_microseconds = 100_000)

Set gc_log_microseconds to nil to not enable JRuby Garbage collections
To only register one of the signal handlers, set the other to nil
Note:

Currently only supported when running JRuby
that exceed the time threshold will be logged. Default: 100 ms
Also adds JRuby Garbage collection logging so that any garbage collections

Thread.current.name = 'My Worker'
calling the following from within the thread itself:
It is recommended to name any threads you create in the application, by

Java thread dump which includes system threads and Java stack traces.
For JRuby users this thread dump differs form the standard QUIT triggered

of threads to the log file, along with their back-traces when available
When the signal is raised on this process, Semantic Logger will write the list

2. Logging a Ruby thread dump

If the current level is :trace it wraps around back to :fatal

:fatal, :error, :warn, :info, :debug, :trace
from the current global default level:
rotates through the following log levels in the following order, starting
When the log_level_signal is raised on this process, the global default log level

log_level_signal, which by default is 'USR2'
The log level can be changed without restarting the process by sending the

1. Changing the log_level:

Two signal handlers will be registered by default:

Add signal handlers for Semantic Logger
def self.add_signal_handler(log_level_signal = "USR2", thread_dump_signal = "TTIN", gc_log_microseconds = 100_000)
  if log_level_signal
    Signal.trap(log_level_signal) do
      current_level_index = LEVELS.find_index(default_level)
      new_level_index = current_level_index == 0 ? LEVELS.size - 1 : current_level_index - 1
      new_level = LEVELS[new_level_index]
      self.default_level = new_level
      self["SemanticLogger"].warn "Changed global default log level to #{new_level.inspect}"
    end
  end
  if thread_dump_signal
    Signal.trap(thread_dump_signal) do
      logger = SemanticLogger["Thread Dump"]
      Thread.list.each do |thread|
        # MRI re-uses the main thread for signals, JRuby uses `SIGTTIN handler` thread.
        next if defined?(JRuby) && (thread == Thread.current)
        logger.backtrace(thread: thread)
      end
    end
  end
  if gc_log_microseconds && defined?(JRuby)
    listener = SemanticLogger::JRuby::GarbageCollectionLogger.new(gc_log_microseconds)
    Java::JavaLangManagement::ManagementFactory.getGarbageCollectorMXBeans.each do |gcbean|
      gcbean.add_notification_listener(listener, nil, nil)
    end
  end
  true
end