From 9ef35d38327794ea63d371650680d7eca6bb2d3d Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 6 Mar 2024 15:06:03 +1300 Subject: [PATCH] Scope the shoryuken context to the current fiber. --- lib/shoryuken/logging.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/shoryuken/logging.rb b/lib/shoryuken/logging.rb index df618ee0..9cff3877 100644 --- a/lib/shoryuken/logging.rb +++ b/lib/shoryuken/logging.rb @@ -1,6 +1,10 @@ require 'time' require 'logger' +class Fiber + attr_accessor :shoryuken_context +end + module Shoryuken module Logging class Pretty < Logger::Formatter @@ -10,16 +14,15 @@ def call(severity, time, _program_name, message) end def context - c = Thread.current[:shoryuken_context] - c ? " #{c}" : '' + Fiber.current.shoryuken_context&.then{|context| " #{context}"} end end def self.with_context(msg) - Thread.current[:shoryuken_context] = msg + Fiber.current.shoryuken_context = msg yield ensure - Thread.current[:shoryuken_context] = nil + Fiber.current.shoryuken_context = nil end def self.initialize_logger(log_target = STDOUT)