2012-09-20 60 views
2

當我使用這個腳本其做工精細紅寶石記錄文件的行爲

require 'logger' 

class Logging < Logger 
    log = Logger.new "sam.txt" 
    log.warn "samarth" 
end 

和日誌文件的輸出以這種方式

# Logfile created on Thu Sep 20 12:01:26 +0530 2012 by/
W, [2012-09-20T12:01:44.402915 #7553] WARN -- : asasa 

現在我想讓它繼承記錄器一類是即將到來和我使用該腳本在所有其他,但其生產

require 'date' 
require 'logger' 

class Logging < Logger 

    def initialize 
     @date = Time.now.strftime("20%y-%m-%d").to_s 
     @time = Time.now.strftime("%X") 
     @file_name = @date+"_"[email protected]+ " log.txt" 
     @file_name_path = '/cmf/samnew/build/Logs/'[email protected]_name 
    end 

    def createfile 
     $log1 = Logger.new(@file_name_path) 
     return $log1 
    end 

end 

我實現這SC一些不同的輸出RIPT在下面的腳本

require '/cmf/samnew/build/Controller/logging' 


class Controller 

    #Generating log file 

    log = Logging.new 
    log1 = log.createfile 
    log1.warn "samarth global" 

end 

和輸出是一樣

# Logfile created on Thu Sep 20 12:03:20 +0530 2012 by/
samarth global 

所以,我應該怎麼做是錯在此代碼或如何創建這樣的輸出上面

回答

1

也許你加載的寶石之一混淆全局格式化字符串。

試試這個定義你希望的格式

def createfile 
    $log1 = Logger.new(@file_name_path) 
    $log1.formatter = proc do |severity, datetime, progname, msg| 
    "#{severity[0]}, [#{datetime} \##{$$}] #{severity} --> #{msg} \n" 
    end 
    $log1 
end 
+0

好一個兄弟的感謝 – SSP