Skip to content

Commit

Permalink
Now uses logging.config's dictConfig format in the config json
Browse files Browse the repository at this point in the history
Cloese #23
  • Loading branch information
scottmconway committed Jan 12, 2024
1 parent 111e4c1 commit dcf5c07
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ If you're interested in why I have quotes around "encryption", check out [my blo
Anyway, to find the "encrypted" variants of these parameters, fire up your browser of choice, open the network monitor, and log in to the service. The `POST` request to `https://buyerapi.shopgoodwill.com/api/SignIn/Login` will contain the values that you're looking for. Those values should be stored in the `encrypted_username` and `encrypted_password` fields.

### `logging`
`log_level` - sets the log level to subscribe to
`gotify` - only required if you wish to use gotify as a logging destination
The contents of this section are passed directly to `logging.config`'s `dictConfig` intializer. See [logging's documentation](https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig) for what information can be provided here. The example config contains a single gotifyHandler instance for logging alerts from both `bid_sniper` and `alert_on_new_query_results`.

### `seen_listings_filename`
This is the path of the file that will have "seen" listings written to, so we can track "new" ones. This is used by `alert_on_new_query_results.py`, and should probably be moved elsewhere.
Expand Down
18 changes: 14 additions & 4 deletions alert_on_new_query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import json
import logging
import logging.config
import os
import re
from typing import Dict, List
Expand Down Expand Up @@ -250,11 +251,20 @@ def main():
# logging setup
logger = logging.getLogger("shopgoodwill_alert_on_new_query_results")
logging_conf = config.get("logging", dict())
logger.setLevel(logging_conf.get("log_level", logging.INFO))
if "gotify" in logging_conf:
from gotify_handler import GotifyHandler

logger.addHandler(GotifyHandler(**logging_conf["gotify"]))
# check if we're using logging.config.dictConfig or not
#
# load entire logging config from dictConfig format
if logging_conf.get("version", 0) >= 1:
logging.config.dictConfig(logging_conf)

# legacy logging config format
else:
logger.setLevel(logging_conf.get("log_level", logging.INFO))
if "gotify" in logging_conf:
from gotify_handler import GotifyHandler

logger.addHandler(GotifyHandler(**logging_conf["gotify"]))

# data source setup
if args.data_source == "saved_searches":
Expand Down
32 changes: 20 additions & 12 deletions bid_sniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import json
import logging
import logging.config
import queue
from json.decoder import JSONDecodeError
from logging.handlers import QueueHandler, QueueListener
Expand Down Expand Up @@ -92,22 +93,29 @@ def __init__(self, config: Dict, dry_run: bool = False) -> None:
self.default_note = self.config["bid_sniper"].get("favorite_default_note", None)

# logging setup
self.logger = logging.getLogger("shopgoodwill_bid_sniper")
logging.basicConfig()
logging_conf = config.get("logging", dict())
self.logger.setLevel(logging_conf.get("log_level", logging.INFO))
self.logger = logging.getLogger("shopgoodwill_bid_sniper")
# check if we're using logging.config.dictConfig or not
#
# load entire logging config from dictConfig format
if logging_conf.get("version", 0) >= 1:
logging.config.dictConfig(logging_conf)

log_queue = queue.Queue()
queue_handler = QueueHandler(log_queue)
# legacy logging config format
else:
logging.basicConfig()
self.logger.setLevel(logging_conf.get("log_level", logging.INFO))
log_queue = queue.Queue()
queue_handler = QueueHandler(log_queue)

if "gotify" in logging_conf:
from gotify_handler import GotifyHandler
if "gotify" in logging_conf:
from gotify_handler import GotifyHandler

queue_listener = QueueListener(
log_queue, GotifyHandler(**logging_conf["gotify"])
)
self.logger.addHandler(queue_handler)
queue_listener.start()
queue_listener = QueueListener(
log_queue, GotifyHandler(**logging_conf["gotify"])
)
self.logger.addHandler(queue_handler)
queue_listener.start()

# TODO since this is a daemon,
# we'll actually have to bother refreshing the token!
Expand Down
28 changes: 21 additions & 7 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
{
"logging": {
"log_level": 20,
"gotify": {
"app_token": "",
"server_url": "",
"extras": {
"client::display": {
"contentType": "text/markdown"
"version": 1,
"handlers": {
"gotifyHandler": {
"class": "gotify_handler.GotifyHandler",
"level": "INFO",
"app_token": "",
"server_url": "https://gotify.example.com",
"extras": {
"client::display": {
"contentType": "text/markdown"
}
}
}
},
"loggers": {
"shopgoodwill_alert_on_new_query_results": {
"handlers": ["gotifyHandler"],
"level": "INFO"
},
"shopgoodwill_bid_sniper": {
"handlers": ["gotifyHandler"],
"level": "INFO"
}
}
},
"auth_info": {
Expand Down

0 comments on commit dcf5c07

Please sign in to comment.