-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Jake-Pullen/feature/better_error_handling
Feature/better error handling
- Loading branch information
Showing
11 changed files
with
550 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ data/* | |
.venv/* | ||
__pycache__/* | ||
*/__pycache__/* | ||
*.pbix | ||
*.pbix | ||
/logs/* |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import datetime as dt | ||
import json | ||
import logging | ||
from typing import override | ||
|
||
class custom_json_logger(logging.Formatter): | ||
def __init__( | ||
self, | ||
*, | ||
format_keys: dict[str,str] | None = None, | ||
): | ||
super().__init__() | ||
self.format_keys = format_keys if format_keys is not None else {} | ||
|
||
@override | ||
def format(self, record: logging.LogRecord) -> str: | ||
record_dict = self._prepare_log_dict(record) | ||
return json.dumps(record_dict, default=str) | ||
|
||
def _prepare_log_dict(self, record: logging.LogRecord) -> dict: | ||
always_fields = { | ||
"message" : record.getMessage(), | ||
"timestamp" : dt.datetime.fromtimestamp( | ||
record.created, tz=dt.timezone.utc | ||
).isoformat(), | ||
} | ||
if record.exc_info is not None: | ||
always_fields["exc_info"] = self.formatException(record.exc_info) | ||
|
||
if record.stack_info is not None: | ||
always_fields["stack_info"] = self.formatStack(record.stack_info) | ||
|
||
message = { | ||
key: msg_val | ||
if (msg_val := always_fields.pop(val, None)) is not None | ||
else getattr(record, val) | ||
for key, val in self.format_keys.items() | ||
} | ||
message.update(always_fields) | ||
return message | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SUCCESS = 0 | ||
MISSING_ENV_VARS = 1 | ||
MISSING_CONFIG_FILE = 2 | ||
CORRUPTED_CONFIG_FILE = 3 | ||
UNAUTHORIZED_API_TOKEN = 4 | ||
REQUESTS_ERROR = 5 | ||
BAD_REQUEST = 6 | ||
FORBIDDEN = 7 | ||
NOT_FOUND = 8 | ||
CONFLICT = 9 | ||
MOVE_FILE_ERROR = 10 | ||
DUPLICATE_RESOLUTION_ERROR = 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
version: 1 | ||
disable_existing_loggers: False | ||
formatters: | ||
simple: | ||
format: "%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - %(message)s" | ||
datefmt: "%Y-%m-%d %H:%M:%S%z" | ||
json: | ||
"()": config.custom_json_logger.custom_json_logger | ||
format_keys: | ||
level: levelname | ||
timestamp: timestamp | ||
logger: name | ||
module: module | ||
function: funcName | ||
line: lineno | ||
message: message | ||
thread_name: threadName | ||
handlers: | ||
stderr: | ||
class: logging.StreamHandler | ||
level: INFO | ||
formatter: simple | ||
stream: ext://sys.stdout | ||
file: | ||
class: logging.handlers.RotatingFileHandler | ||
level: DEBUG | ||
formatter: json | ||
filename: logs/dpfy_log.jsonl | ||
maxBytes: 10485760 # 10MB | ||
backupCount: 10 | ||
queue_handler: | ||
class: logging.handlers.QueueHandler | ||
handlers: | ||
- stderr | ||
- file | ||
respect_handler_level: True | ||
loggers: | ||
root: | ||
level: DEBUG | ||
handlers: | ||
- queue_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.