diff --git a/README.md b/README.md index 5d603e4..85ff84b 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Then you will configure a cron entry for each account: If there are multiple feeds for an account, you can setup the `config.yml` like so: -```yml +```yaml - name: The Globe and Mail - Report on Business twitter: access_token: ACCESS_TOKEN @@ -138,7 +138,7 @@ The configuration file has support for [environment variables](https://medium.co For instance, say you want to keep your Twitter credentials safe. You'd keep a reference to it in the `config.yaml` this way: -```yml +```yaml twitter: consumer_key: "${MY_CONSUMER_KEY_ENV_VAR}" consumer_secret: "${MY_CONSUMER_SECRET_ENV_VAR}" @@ -176,7 +176,7 @@ zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Then you would use the `ACCESS_TOKEN` and the `ACCESS_TOKEN_SECRET` inside the config like this -```shell script +```yaml feeds: - name: My new feed url: http://www.mynewfeed.com/feed/ @@ -190,7 +190,7 @@ feeds: Diffengine has support for `geckodriver` and `chromedriver`. You can configure this in the `config.yaml`. The keys are the following ones. -```yml +```yaml webdriver: engine: executable_path: @@ -201,7 +201,7 @@ webdriver: The `geckodriver` is properly defined by default. In case you need to configure it, then: -```yml +```yaml webdriver: engine: "geckodriver" executable_path: null (this config has no use with geckodriver) @@ -212,7 +212,7 @@ webdriver: If you want to use `chromedriver` locally, then you should leave the config this way: -```yml +```yaml webdriver: engine: "chromedriver" executable_path: null ("chromedriver" by default) @@ -224,13 +224,28 @@ webdriver: If you use Heroku, then you have to add the [Heroku chromedriver buildpack](https://github.com/heroku/heroku-buildpack-chromedriver). And then use the environment vars provided automatically by it. -```yml +```yaml webdriver: engine: "chromedriver" executable_path: "${CHROMEDRIVER_PATH}" binary_location: "${GOOGLE_CHROME_BIN}" ``` +### Configuring the loggers + +By default, the script will log everyhintg to `./diffengine.log`. +Anyway, you can disable the file logger and/or enable the console logger as well. +You can modify the log filename, too. + +If no present, the default values will be the following ones. +```yaml +log: diffengine.log +logger: + file: true + console : false +``` + +Logging to the console could be useful to see what's happening if the app lives in services like Heroku. ## Develop diff --git a/diffengine/__init__.py b/diffengine/__init__.py index 3970697..c02d9bd 100755 --- a/diffengine/__init__.py +++ b/diffengine/__init__.py @@ -350,16 +350,27 @@ def _generate_diff_images(self): browser.save_screenshot(self.thumbnail_path) -def setup_logging(): - path = config.get("log", home_path("diffengine.log")) - logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", - filename=path, - filemode="a", - ) - logging.getLogger("readability.readability").setLevel(logging.WARNING) - logging.getLogger("tweepy.binder").setLevel(logging.WARNING) +def setup_logging(log_file=True, log_console=False): + # TODO. Configurable verbosity + log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + log_formatter = logging.Formatter(log_format) + + handlers = [] + if log_file: + filename = config.get("log", home_path("diffengine.log")) + file_handler = logging.FileHandler(filename=filename, mode="a") + file_handler.setFormatter(log_formatter) + handlers.append(file_handler) + + if log_console: + console_handler = logging.StreamHandler(sys.stdout) + console_handler.setFormatter(log_formatter) + handlers.append(console_handler) + + if handlers: + logging.basicConfig(level=logging.INFO, format=log_format, handlers=handlers) + logging.getLogger("readability.readability").setLevel(logging.WARNING) + logging.getLogger("tweepy.binder").setLevel(logging.WARNING) def load_config(prompt=True): @@ -534,7 +545,9 @@ def init(new_home, prompt=True): executable_path = config.get("webdriver.executable_path") binary_location = config.get("webdriver.binary_location") browser = setup_browser(engine, executable_path, binary_location) - setup_logging() + setup_logging( + config.get("logger.file", True), config.get("logger.console", False) + ) setup_db() except RuntimeError as e: logging.error("Could not finish the setup", e)