2012-01-03 32 views
0

我正在使用Sinatra 1.3,它是一個Sinatra::Application
我有一個方法,提取一個Web服務。
我想登錄時該服務有成功,它已經失敗,因爲它在後臺(cron作業)運行用於Web服務錯誤的Sinatra記錄器

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

我似乎無法使用Sinatralogger instance。它爲我生成錯誤,我假設它是這樣做的,因爲我登錄的方法,而不是在一個路線內?

什麼是使用Sinatra::Application

回答

1

我用下面的代碼放在一個Sinatra用於記錄

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 

然後用記錄儀記錄到捕捉一些日誌文件中的errorssuccess的最佳途徑。

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

是前6行代碼,我會放在我的'helpers'或'do do'中嗎? – alenm 2012-01-04 17:16:07

+0

沒有此代碼將作爲文件的一部分,在任何方法之外。如果您正在使用模塊化的Sinatra風格,請將其作爲課程的一部分。 – randomuser 2012-01-05 08:46:00