开发者

Can the standard Ruby logger be configured to flush after every message?

开发者 https://www.devze.com 2023-02-18 15:08 出处:网络
Is it possible for the standard Ruby logger (i.e. ::Logger) to auto-flush after each write? Update: I am setting a custom log formatter, as per How to get timestamps in your ruby on rails logs:

Is it possible for the standard Ruby logger (i.e. ::Logger) to auto-flush after each write?

Update: I am setting a custom log formatter, as per How to get timestamps in your ruby on rails logs:

class Foo
  def initialize(params = {})
    @logger = Logger.new $stdout
    @logger.formatter = LogFormatter.new
    @logger.level = params.include?(:log)开发者_C百科 ? params[:log] : Logger::INFO
    # ...
  end

  class LogFormatter < Logger::Formatter
    def call(severity, time, progname, message)
      timestamp = time.iso8601
      log_msg = "#{timestamp}#{" [#{severity}]" if %w{ERROR WARN}.include? severity} "
      if message.is_a? Exception
        log_msg += "#{message.message}"
        log_msg += ": #{message.backtrace.join "\n#{timestamp}  "}" unless message.backtrace.blank?
      else
        log_msg += message
      end

      "#{log_msg}\n"
    end
  end
end

I tried to use the suggestion from idlefingers as follows:

  def initialize(params = {})
    Rails.logger.auto_flushing = 1
    @logger = Logger.new $stdout
    @logger.formatter = LogFormatter.new
    @logger.level = params.include?(:log) ? params[:log] : Logger::INFO
  end

But no dice. Neither does @logger.auto_flushing = 1 work for me. What am I doing wrong?

Update: It turns out that ActiveSupport's ::Logger simply wraps the standard Ruby ::Logger. So my question turns out to be about Ruby and not Rails, after all.


Turns out it was just a matter of turning on the sync attribute of the underlying IO object:

  def initialize(params = {})
    $stdout.sync = true unless RAILS_ENV == 'production'
    @logger = Logger.new $stdout
    @logger.formatter = LogFormatter.new
    @logger.level = params.include?(:log) ? params[:log] : Logger::INFO
  end


Set ActiveSupport::BufferedLogger.auto_flushing in your environment like so:

Rails.logger.auto_flushing = 1

This should make it flush after every message.


Sets the “sync mode'' to true or false. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered internally. Returns the new state. See also #fsync.

https://ruby-doc.org/core-2.7.1/IO.html#method-i-sync

I had this issue where logs were buffered and only flushed every hour.

require 'logger'

STDOUT.sync = true
logger = Logger.new(STDOUT, level: :info)
logger.info "Hello World"
0

精彩评论

暂无评论...
验证码 换一张
取 消