2017-07-27 33 views
1

私は現在、コンソール用のログ設定があります。使用して、私の設定を呼び出すPython - ログをファイルに書き込む

イム:

import logging 
import logging.config 

logging.config.fileConfig('logging.conf') 
logger = logging.getLogger('osPatch') 

私の設定は、これは私に完全に罰金コンソールレベルのログを提供します::

[loggers] 
keys=root,osPatch 

[handlers] 
keys=consoleHandler 

[formatters] 
keys=osPatch 

[logger_root] 
level=DEBUG 
handlers=consoleHandler 

[logger_osPatch] 
level=DEBUG 
handlers=consoleHandler 
qualname=osPatch 
propagate=0 

[handler_consoleHandler] 
class=StreamHandler 
level=DEBUG 
formatter=osPatch 
args=(sys.stdout,) 

[formatter_osPatch] 
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s 
datefmt= 

です。

今、同じログをファイルに書きたいと思っています。

自分の設定ファイルを編集して、fileHandlerを使用しています。私はここで間違って何をやっている

Traceback (most recent call last): 
    File "apply_errata.py", line 1, in <module> 
    import satellite_utils 
    File "/root/config-3.1.25/automated-os-patching/satellite_utils.py", line 3, in <module> 
    import system_utils 
    File "/root/config-3.1.25/automated-os-patching/system_utils.py", line 4, in <module> 
    import processing_utils 
    File "/root/config-3.1.25/automated-os-patching/processing_utils.py", line 7, in <module> 
    logging.config.fileConfig('logging.conf') 
    File "/usr/lib64/python3.4/logging/config.py", line 85, in fileConfig 
    _install_loggers(cp, handlers, disable_existing_loggers) 
    File "/usr/lib64/python3.4/logging/config.py", line 253, in _install_loggers 
    logger.addHandler(handlers[hand]) 
KeyError: 'FileHandler' 

:これは私にエラーになります

[loggers] 
keys=root,osPatch 

[handlers] 
keys=consoleHandler,FileHandler 

[handler_FileHandler] 
filename=example.log 
level=DEBUG 
formatter=osPatch 

だからイムに私の設定ファイルを編集しますか?

+1

は、以下の答えを参照してください:

は参考のために、これは私はあなたが作成することを目指す考えて設定ファイルの試み、作業バージョンですむしろ漠然とした "私はこれで始まり、ここでこれを編集した"、あなたの実際のファイルがどのように見えるかを本当に知るのは難しい。 – bgse

答えて

1

あなたの完全な設定ファイルがどのように見えるのかわかりませんが、私の経験上のトレースバックはハンドラキーがハンドラセクションで正しく宣言されていないことを示しています。それは多分に役立つのではなく、あなたの完全な設定ファイルを投稿していない場合は、

[loggers] 
keys=root,osPatch 

[handlers] 
keys=consoleHandler,FileHandler 

[formatters] 
keys=osPatch 

[logger_root] 
level=DEBUG 
handlers=consoleHandler,FileHandler 

[logger_osPatch] 
level=DEBUG 
handlers=consoleHandler,FileHandler 
qualname=osPatch 
propagate=0 

[handler_consoleHandler] 
class=StreamHandler 
level=DEBUG 
formatter=osPatch 
args=(sys.stdout,) 

[handler_FileHandler] 
class=FileHandler 
level=DEBUG 
formatter=osPatch 
args=('example.log',) 

[formatter_osPatch] 
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s 
datefmt= 
関連する問題