2014-01-30 219 views
0
logger = logging.getLogger('application') 
logger.setLevel(logging.DEBUG) 
fh = logging.FileHandler('app.log') 
fh.setLevel(logging.DEBUG) 
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 
fh.setFormatter(formatter) 
logger.addHandler(fh) 

class blah() 

    def blah1(self) 
     .... 
     .... 
     self.logger.DEBUG("You dont have root privileges") 

    def blah1(self) 
     .... 
     .... 
     self.logger.DEBUG("You dont have root privileges2") 


if __name__ == "__main__": 
    b= blah() 
    b.blah1() 

夥計們即時嘗試讓我的每個函數報告到我的Python腳本中的日誌文件。但它沒有記錄。我哪裏出錯了?Python日誌記錄功能

感謝 威廉

+0

self.logger.DEBUG(「你沒有root權限」),你能顯示你在類中定義記錄器的行嗎 – duck

回答

2

找到答案簡化

import logging 
logger = logging.getLogger('application') 
logger.setLevel(logging.DEBUG) 
fh = logging.FileHandler('app.log') 
fh.setLevel(logging.DEBUG) 
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 
fh.setFormatter(formatter) 
logger.addHandler(fh) 

class blah: 
    def blah1(self): 
     logger.debug("You dont have root privileges") 


if __name__ == "__main__": 
    b= blah() 
    b.blah1() 

注:調試應該是小case了,請使用全局記錄。當你在全局範圍內初始化它時

1

嘗試

class blah(): 
    logger = logger 

    def blah1(self) 
     .... 
     .... 
     self.logger.DEBUG("You dont have root privileges") 

    def blah1(self) 
     .... 
     .... 
     self.logger.DEBUG("You dont have root privileges2")