Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable loggers (file and console) #72

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -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/
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
35 changes: 24 additions & 11 deletions diffengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down