2013-07-19 70 views
39

我工作的一個Rails 4項目,我似乎無法做任何事情出現在我的開發日誌當我打電話Rails.logger.debug "test"我試着在網上搜索,但我覺得我的天堂」牛逼取得了很大的進展。我如何去建立一個記錄器,並告訴它在我的開發日誌文件中寫?Rails中使用記錄儀4

任何幫助將不勝感激。

回答

58

我想你應該在你的方法這樣使用它。結帳section 2.3這裏

def your_method 
    logger.debug "This is from debug" 
    logger.info "This is from info" 
end 
0

如果你想在一個普通老式Ruby對象或ServiceObject使用Rails.logger,那麼你將需要手動包括它:

class MyService 
    def initialize(foo) 
    @logger = Logger.new(STDOUT) 
    @foo = foo 
    end 

    def call 
    do_something 
    end 

    private 

    def do_something 
    @logger.debug { "do_something called! With #{@foo.class} class" } 
    end 
end 

結果:

MyService.new(Bar.new).call 
# [2018-01-27T00:17:22.167974 #57866] DEBUG -- : do_something called! With Bar