Last active
September 11, 2022 10:33
-
-
Save keithrbennett/006c10128ee3a2f37da98b7f013d3951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Illustrates cycling through the SemanticLogger log levels with signals. | |
# For reference by https://github.com/reidmorrison/semantic_logger/issues/231. | |
require 'semantic_logger' | |
# Redefine add_signal_handler to output to STDERR (omit TTIN behavior for brevity): | |
module SemanticLogger | |
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 | |
# This line added: | |
STDERR.print "Original level: #{default_level.to_s.ljust(5)}. " | |
index = default_level == :trace ? LEVELS.find_index(:error) : LEVELS.find_index(default_level) | |
new_level = LEVELS[index - 1] | |
# This line disabled: | |
#self["SemanticLogger"].warn "Changed global default log level to #{new_level.inspect}" | |
self.default_level = new_level | |
# This line added: | |
STDERR.puts "New level is #{default_level}. " | |
end | |
end | |
end | |
end | |
SemanticLogger.add_signal_handler('USR1') | |
7.times do | |
print 'Sending signal...' | |
Process.kill('USR1', Process.pid) | |
sleep 0.01 | |
end | |
=begin | |
Output is: | |
Sending signal...Original level: info . New level is debug. | |
Sending signal...Original level: debug. New level is trace. | |
Sending signal...Original level: trace. New level is warn. | |
Sending signal...Original level: warn . New level is info. | |
Sending signal...Original level: info . New level is debug. | |
Sending signal...Original level: debug. New level is trace. | |
Sending signal...Original level: trace. New level is warn. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment