【问题标题】:Log file does not get written until server shutdown / does not write with Logger在服务器关闭之前不会写入日志文件/不使用 Logger 写入
【发布时间】:2013-02-13 09:25:40
【问题描述】:

我正在 Windows 上使用 Sinatra/Thin 开发一个简单的 Web 服务

在我的应用程序中,我有以下功能可以启用对文件的日志记录:

Dir.mkdir('log') unless File.exists?('log')
use Rack::CommonLogger, File.new('log/access.log', 'w')

当服务器启动时,文件被创建。但是,在我关闭服务器之前,不会将任何内容写入文件。

另一方面,当我使用 Logger 时,像这样:

logger = Logger.new('log/access.log', 10, 1024000)
use Rack::CommonLogger, logger

它甚至不将条目写入日志文件,除了一行写着:

# Logfile created on 2013-02-13 11:23:45 +0200 by logger.rb/31641

我需要的是:

  • 立即写入日志条目
  • 能够记录日志轮换功能

【问题讨论】:

    标签: logging sinatra rack thin


    【解决方案1】:

    我一直在我的 sinatra 应用程序中使用这个解决方案(继承 Sinatra::Base)before do
    env['rack.logger'] = Logger.new("log/#{App.environment}.log")
    logger.datetime_format = "%Y/%m/%d @ %H:%M:%S "
    logger.level = Logger::INFO if App.production?
    end

    【讨论】:

      【解决方案2】:

      使用 Sinatra 经典样式应用程序,将文件的属性同步设置为 true 以进行日志记录,对我有用。在生产和开发中:

      Logger.class_eval { alias :write :'<<' }
      flogger = File.open("log/app.log","a")
      flogger.sync = true # This is what makes to write always
      logger = Logger.new(flogger)
      use Rack::CommonLogger, logger
      

      来源:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 1970-01-01
        • 2019-01-08
        • 2012-04-27
        相关资源
        最近更新 更多