Ruby的stdlib Logger类是线程安全的吗? [英] Is Ruby's stdlib Logger class thread-safe?
问题描述
简而言之,是Ruby线程安全的标准库 Logger 类? Google出现的唯一有用信息是论坛上的某人说它似乎"是线程安全的.而且,我不想花时间测试记录器以弄清楚是否是记录器.
In short, is the standard library Logger class in Ruby thread-safe? Only useful info Google turned up was someone on a forum saying it "seems" thread-safe. And I don't feel like spending time testing a logger to try to figure out if it is or not.
我暂时使用的是 log4r ,它是线程安全的,但是如果使用标准库已经做到了.
For the time being I'm using log4r which is thread-safe, but it's overkill if the standard library already does it.
推荐答案
快速浏览logger.rb会显示如下代码:
A quick look at logger.rb reveals code such as the following:
def write(message)
@mutex.synchronize do
if @shift_age and @dev.respond_to?(:stat)
begin
check_shift_log
rescue
raise Logger::ShiftingError.new("Shifting failed. #{$!}")
end
end
@dev.write(message)
end
end
因此,尽管我不能保证它是否具有线程安全性正确,但我可以确认它正在共同努力,以正确地做到这一点!
So while I can't vouch for whether it gets thread-safety correct, I can confirm that it is making a concerted effort to do it right!
P.S.通过阅读代码,通常很容易为自己回答这样的问题:-)
P.S. It's often easy to answer questions like this for yourself by reading the code :-)
这篇关于Ruby的stdlib Logger类是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!