问题
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