Is Ruby's stdlib Logger class thread-safe?

后端 未结 4 722
长情又很酷
长情又很酷 2021-01-03 18:31

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 fe

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 18:57

    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. It's often easy to answer questions like this for yourself by reading the code :-)

提交回复
热议问题