Created
July 16, 2018 05:49
-
-
Save medecau/51cca2c75c674105a7de8c0aace5ad15 to your computer and use it in GitHub Desktop.
Exception > Dict > SQLite
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
import datetime as dt | |
import logging | |
import sys | |
from playhouse.dataset import DataSet # this can be any DB ;) | |
params = 'timestamp name level pathname lineno msg args exc_info func sinfo'.split(' ') | |
db = DataSet('sqlite:///db.sqlite') | |
logs = db['logs'] | |
class DictHandler(logging.Handler): | |
def emit(self, record): | |
record_dict = {p: getattr(record, p) for p in params if hasattr(record, p)} | |
logs.insert(**record_dict) | |
def ehook(etype, evalue, etrace): | |
exc_info = (etype, evalue, etrace) | |
logging.exception(evalue, exc_info=exc_info, stack_info=True, extra=dict(timestamp=dt.datetime.utcnow())) | |
sys.__excepthook__(*exc_info) | |
default_logger = logging.getLogger() | |
default_logger.addHandler(DictHandler()) | |
default_logger.setLevel(logging.INFO) | |
sys.excepthook = ehook | |
def main(): | |
open('nope.txt', 'r') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment