diff --git a/CepTracker.py b/CepTracker.py index 95ae919..8629c5b 100755 --- a/CepTracker.py +++ b/CepTracker.py @@ -4,13 +4,25 @@ import requests import re +import logging.config +import os +import yaml class CepTracker(): def __init__(self): + self.log_path = 'log.yaml' + self._config_log_file() + self.logger = logging.getLogger(__name__) + self.url = 'http://m.correios.com.br/movel/buscaCepConfirma.do' + def _config_log_file(self): + with open(self.log_path, 'rt') as f: + config = yaml.load(f.read()) + logging.config.dictConfig(config) + def _request(self, cep): response = requests.post(self.url, data={ 'cepEntrada': cep, @@ -18,7 +30,11 @@ def _request(self, cep): 'cepTemp': '', 'metodo': 'buscarCep' }) - response.raise_for_status() + try: + response.raise_for_status() + except requests.exceptions.HTTPError as ex: + logging.error('Erro request site Correios', exc_info=True) + raise ex return response.text def _get_infos_(self, cep): diff --git a/Makefile b/Makefile index 13108d1..2d91fe3 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ test: pep8 .PHONY: pep8 pep8: - @flake8 * --ignore=F403,F401 --exclude=requirements.txt,*.pyc,*.md,COPYING,Makefile,*.wsgi,*celerybeat-schedule* + @flake8 * --ignore=F403,F401 --exclude=requirements.txt,*.pyc,*.md,COPYING,Makefile,*.wsgi,*celerybeat-schedule*,*.yaml,*.log diff --git a/log.yaml b/log.yaml new file mode 100644 index 0000000..73404e2 --- /dev/null +++ b/log.yaml @@ -0,0 +1,23 @@ +version: 1 +formatters: + simple: + format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + +handlers: + info_file_handler: + class: logging.handlers.RotatingFileHandler + level: ERROR + formatter: simple + filename: errors.log + maxBytes: 10485760 + backupCount: 20 + encoding: utf-8 + +loggers: + __name__: + level: ERROR + handlers: [info_file_handler] + propagate: no + +root: + handlers: [info_file_handler] diff --git a/requirements.txt b/requirements.txt index 95908b6..1302189 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ packtrack==0.1.0 xmltodict flake8 celery[mongodb] +PyYAML==3.10