2017-10-16 2 views
-1

以下は、ほんの数行で他のスクリプトでカスタムロガーを素早く作成するために書いたロギングモジュールです。ここでPython3カスタムロギングモジュール(再利用可能メソッド)

import logging 
def setup(filepath, filemode): 
    logging.basicConfig(level=logging.DEBUG, 
         format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 
         datefmt='%m-%d %H:%M', filename=filepath, filemode=filemode) 

    # A handler that writes to the console. def console(level,logger): 
    console = logging.StreamHandler() 
    console.setLevel(level=level) 
    # Sets a format which is simpler for console use. 
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 
    # Tell the handler to use this format. 
    console.setFormatter(formatter) 
    # Adds the handler to the root logger. 
    logging.getLogger(logger).addHandler(console) 

は、私が他のスクリプトからこのモジュールを呼び出す方法です:

import ddlog 
# Filepath, logger, and console handler 
logpath = os.getcwd() + '//logs/'+hostlist+'.log' 
logging = ddlog.setup(logpath, 'w') 
ddlog.console(INFO,logging) 

logging.error("[-] Failed to Authenticate.") 
logging.log(INFO,'[+] successfully connected to ' + host) 

私はこのスクリプトを実行しようとすると、これは、トレースに何が起こるかです:

File "login.py", line 23, in main 
logging.error("[-] Failed to Authenticate.", exc_info=True) 
AttributeError: 'NoneType' object has no attribute 'error' 

任意のアイデアや経験シンプルなロガーを作成することで?

答えて

0

セットアップ機能が何も表示されないようです。従ってlogging = ddlog.setup()Noneと同じです。

関連する問題