如何在较旧的 Sinatra API 应用程序中进行日志记录和调试?
How can I log and debug in an older Sinatra API application?
我有这个古老而简单的 Sinatra API。它的版本是 1.4.8。很简单,又要重写了。在短期内,我需要向它添加一些调试语句以修复某些问题。我发现只需将它们发送到 STDOUT 即可在屏幕上实时观看它们。
我看到 1.3 版中包含了一个记录器,但我似乎找不到关于如何实现它的好的教程页面。我不需要复杂的日志记录 class。我最想做的就是记录到一个文件。有没有一种无需大量时间投资即可完成其中任一操作的简单方法?
我发现 this page 很好地概述了过程。
此代码块包含我的更改:
::Logger.class_eval { alias :write :'<<' }
access_log = ::File.join(::File.dirname(::File.expand_path(__FILE__)),'log','access.log')
$access_logger = ::Logger.new(access_log)
$error_logger = ::File.new(::File.join(::File.dirname(::File.expand_path(__FILE__)),'log','error.log'),"a+")
$error_logger.sync = true
configure :development do
$access_logger.level = Logger::DEBUG
use ::Rack::CommonLogger, $access_logger
end
configure :production do
$access_logger.level = Logger::WARN
use ::Rack::CommonLogger, $access_logger
end
我刚刚更改了开发和生产的日志记录级别。我还将记录器变成了可以从代码的其他部分访问的变量。
所以对于我的主要 API 模块,我可以输出变量的内容或其他任何内容:
$access_logger.debug "subscription_id is #{subscription_id}."
效果很好!
我有这个古老而简单的 Sinatra API。它的版本是 1.4.8。很简单,又要重写了。在短期内,我需要向它添加一些调试语句以修复某些问题。我发现只需将它们发送到 STDOUT 即可在屏幕上实时观看它们。
我看到 1.3 版中包含了一个记录器,但我似乎找不到关于如何实现它的好的教程页面。我不需要复杂的日志记录 class。我最想做的就是记录到一个文件。有没有一种无需大量时间投资即可完成其中任一操作的简单方法?
我发现 this page 很好地概述了过程。
此代码块包含我的更改:
::Logger.class_eval { alias :write :'<<' }
access_log = ::File.join(::File.dirname(::File.expand_path(__FILE__)),'log','access.log')
$access_logger = ::Logger.new(access_log)
$error_logger = ::File.new(::File.join(::File.dirname(::File.expand_path(__FILE__)),'log','error.log'),"a+")
$error_logger.sync = true
configure :development do
$access_logger.level = Logger::DEBUG
use ::Rack::CommonLogger, $access_logger
end
configure :production do
$access_logger.level = Logger::WARN
use ::Rack::CommonLogger, $access_logger
end
我刚刚更改了开发和生产的日志记录级别。我还将记录器变成了可以从代码的其他部分访问的变量。
所以对于我的主要 API 模块,我可以输出变量的内容或其他任何内容:
$access_logger.debug "subscription_id is #{subscription_id}."
效果很好!