PKJirequests_handler/__init__.py""" Handler for logging records to a Restful API. :param api_url: URL to post records to. :param auth: Credentials in format (username, password). :param level: Logging level (DEBUG, INFO, ERROR, WARNING, CRITICAL). :param filter: Minimum record level to emit. :param bubble: Should bubble. """ import arrow import requests from logbook import LogRecord, Handler __version__ = "0.0.6" class RequestsHandler(Handler): """ Handler for logging records to a Restful API. :param api_url: URL to post records to. :param auth: Credentials in format (username, password). :param level: Logging level (DEBUG, INFO, ERROR, WARNING, CRITICAL). :param filter: Minimum record level to emit. :param bubble: Should bubble. """ def __init__(self, api_url: str, auth: tuple, level: str = 'INFO', filter: str = None, bubble: bool = False): Handler.__init__(self, level, filter, bubble) self.api_url = api_url self.auth = auth self.log_record_fields = [ "channel", "level", "lineno", "filename", "module", "func_name", "message", "log_time" ] def emit(self, record: LogRecord): log_record = record.to_dict() log_record["log_time"] = arrow.utcnow().strftime( "%Y-%m-%d %H:%M:%S.%f") log_record = {k: v for k, v in log_record.items() if k in self.log_record_fields} try: requests.post(url=self.api_url, data=log_record, auth=self.auth) except Exception as e: print(e) def main(): pass if __name__ == '__main__': main() PK؍J9I!requests_handler/test_handlers.pyimport logbook from iologger import iologger from requests_handler import RequestsHandler def test_handler(): logger = logbook.Logger("TestLogger") requests_handler = RequestsHandler( "https://autosweet-data.com/api/v1/logs", auth=('admin', 'aut0sw33t')) requests_handler.push_application() logger.debug("A debug message") logger.critical("A critical message!") requests_handler.pop_application() def test_iologger(): @iologger() def my_func(string: str, integer: int) -> str: return f"my string was {string} and my integer was {integer}." requests_handler = RequestsHandler( "https://autosweet-data.com/api/v1/logs", auth=('admin', 'aut0sw33t')) requests_handler.push_application() my_func("Abraham", integer=15) my_func("Isaac", 0) requests_handler.pop_application() def main(): pass if __name__ == '__main__': test_iologger() PK!H|&Ub&requests_handler-0.0.6.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,Q034 /, (-JLR()*M ILR(4KM̫#DPK!H5-)requests_handler-0.0.6.dist-info/METADATAmn0~=C,QP!TX-q,:}T87YH9zޤ#mj +)F뼔2}#TiwqPt83.'VfӔ0yq65,ޒc}k*vQpsrGdMA>M_'Ot\EB]㞛N /Ho ڣ)LuKlh')zvq¯?07jm̶B_glzq27b(#|pTQG$ .DrHPK!HVU&'requests_handler-0.0.6.dist-info/RECORD=s0)B@:K(E$oz.ەm_J%)K*OJyZse?g7Zp +NV4M{EO!;,8J.)b3Wѐa?xkfkjxJQ(DX3R" ;z>$̥"|^q.cx/7 g~]ݶ/{X%-Н?\7PKJirequests_handler/__init__.pyPK؍J9I!requests_handler/test_handlers.pyPK!H|&Ub& requests_handler-0.0.6.dist-info/WHEELPK!H5-) requests_handler-0.0.6.dist-info/METADATAPK!HVU&' requests_handler-0.0.6.dist-info/RECORDPK5