Sinatra Logger for Web Service Errors

夙愿已清 提交于 2019-12-12 04:58:26

问题


I am using Sinatra 1.3 and it's a Sinatra::Application.
I have a method that fetches a web service.
I want to log when this service has succeed and what it has failed as it runs in the background (cron job)

def fetch_some_web_service  
  begin  
    #if successful  
    log.info "Success"  
  rescue SocketError => e  
    log.info "Failed"
  end   
end

I can't seem to use the Sinatra logger instance. It's generating errors for me and I'm assuming it's doing this because I'm logging in a method and not within a route?

What is the best way to capture the errors and success in some log file using Sinatra::Application


回答1:


I use the following code in Sinatra for logging

raise "Log File not specified" if log_file_location == nil
log_file = File.new(log_file_location, "a")

$stdout.reopen(log_file)
$stderr.reopen(log_file)

$stdout.sync=true
$stderr.sync=true

Then use logger to log.

logger.info("it works !!!!")


来源:https://stackoverflow.com/questions/8716595/sinatra-logger-for-web-service-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!