Sinatra Logger for Web Service Errors

2019-09-05 15:19发布

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条回答
ら.Afraid
2楼-- · 2019-09-05 15:35

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 !!!!")
查看更多
登录 后发表回答