私はさまざまな Web サイト用にいくつかの異なるパーサーを持っています。また、特別な解析用のlxml関数を含むshared.pyというファイルと、データベース (保存など) を処理するbase.pyファイルもあります。何かが失敗した (画像が見つからない) または成功した (画像が見つかった) 場合、これをログ ファイルに記録する必要があります。これには、標準のログ モジュールを使用しています。
Log クラスを作成するlogger.pyファイルを作成したので、パーサーまたはbase.py/shared.pyでこれを呼び出すことができます。ファイルは次のようになります。
import logging
import os.path
__metaclass__ = type
class Log:
def __init__(self, filename, loggername="fetchers", path="logs/", formater="%(asctime)s %(levelname)s %(message)s"):
self.logger = logging.getLogger(loggername)
self.hdlr = logging.FileHandler(path + os.path.splitext(filename)[0] + ".log")
self.formater = logging.Formatter(formater)
self.hdlr.setFormatter(self.formater)
self.logger.addHandler(self.hdlr)
self.logger.setLevel(logging.INFO)
def info(self, msg):
self.logger.info(msg)
def warning(self, msg):
self.logger.warning(msg)
def error(self, msg):
self.logger.error(msg)
def critical(self, msg):
self.logger.critical(msg)
if __name__ == "__main__":
pass
フォルダ階層は次のようになります。
- /パーサー
- /パーサー/base.py
- /パーサー/shared.py
- /パーサー/parserone.py
- /パーサー/parsertwo.py
- /パーサー/parsersthree.py
- /パーサー/...
- /パーサー/ログ/base.log
- /パーサー/ログ/shared.log
- /Parsers.logs/parserOne.log
すべてのパーサーはロガー ファイル (base.py と shared.py も) をインポートし、次のようにロガーを初期化します。
logger = Log(os.path.basename(__file__))
logger.warning("Something has happened..")
これは問題なく動作し、ログに書き込みますが、問題は、base.py がクエリなどに関するログを書き込み、パーサーが失敗などに関するログを書き込むことです (shared.py と同じです)。全部同じに見える..
➜ logs tail parserOne.log
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/
➜ logs tail base.log
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/
➜ logs tail shared.log
2012-10-26 16:35:21,250 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/scherpe-omzetdaling-televisiereclame/
2012-10-26 16:35:21,286 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/nominaties-mercurs-bekend2/
2012-10-26 16:35:21,322 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/traditionele-media-nog-steeds-populair-bij-jongeren/
2012-10-26 16:35:21,371 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/persgroep-blijft-sponsor-san/
2012-10-26 16:35:21,407 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/pg-overtreft-verwachtingen/
2012-10-26 16:35:21,443 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/discovery-networks-introduceert-discovery-client-productions/
2012-10-26 16:35:21,479 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/stoelendans-bij-wehkamp.nl/
2012-10-26 16:35:21,563 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/amazon-duikt-in-rode-cijfers/
2012-10-26 16:35:21,599 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/linkedin-rabobank-meest-populaire-werkgever/
2012-10-26 16:35:21,683 INFO Data was sucessfuly saved: http://www.adformatie.nl/nieuws/bericht/veronica-uitgeverij-wil-naar-amsterdam/
すべてのログ ファイルが同じなのはなぜですか? それらが異なるファイルである場合は?!
乾杯。