Skip to content

Commit

Permalink
Log thread name for gem development debugging (#1085)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon authored Sep 26, 2023
1 parent 379997e commit 98d022f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spec/test_app/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require_relative 'boot'

require 'rails/all'
require "good_job"
require "good_job/engine"

Bundler.require(*Rails.groups)
require "good_job"
require_relative "../lib/thread_name_formatter"

module TestApp
class Application < Rails::Application
Expand All @@ -16,6 +17,7 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
#
config.log_formatter = ThreadNameFormatter.new

config.active_job.queue_adapter = :good_job

Expand Down
32 changes: 32 additions & 0 deletions spec/test_app/lib/thread_name_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
class ThreadNameFormatter < ActiveSupport::Logger::SimpleFormatter
def call(severity, timestamp, _progname, message)
prefix = [emoji_hash(Thread.current.name), Thread.current.name, emoji_hash(Thread.current.name)].compact.join(" ")
"#{ActiveSupport::LogSubscriber.new.send(:color, "[#{prefix}]", :magenta)} #{super}"
end

def emoji_hash(str)
# Hash the input string using SHA256
digest = Digest::SHA256.hexdigest(str || "")

# Take the first few characters from the hash
partial_digest = digest[0..4].to_i(16)

# Define the ranges for the emojis
ranges = [
(0x1F345..0x1F35E), # Vegetables and some other food items
(0x1F400..0x1F43E) # Animals
]

# Combine all ranges into a single array of code points
all_emojis = ranges.flat_map { |r| r.to_a }

# Compute an index within the all_emojis array
index = partial_digest % all_emojis.length

# Convert the code point to a character (emoji)
emoji = [all_emojis[index]].pack('U*')

emoji
end
end

0 comments on commit 98d022f

Please sign in to comment.