From e4d4ebc41ffa0351d6269b270bc82cfb5e3ca959 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Tue, 25 Oct 2022 19:16:48 +0200 Subject: [PATCH] Add lib/proxy_log.py --- lib/proxy_log.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/proxy_log.py diff --git a/lib/proxy_log.py b/lib/proxy_log.py new file mode 100644 index 0000000..5673569 --- /dev/null +++ b/lib/proxy_log.py @@ -0,0 +1,45 @@ +""" +Logger of proxy queries + +""" + +# pylint: disable=consider-using-with,too-few-public-methods + +import datetime + +class Logger: + + """ + Generic logger. + For specific loggers, _shorten_query() should be rewritten. + """ + + def __init__(self, filename): + + self._filename = filename + self._file = open(filename, "a", encoding="utf-8") + + def _shorten_query(self, query): + return query + + def log(self, query, error): + """ + Log `query` and `error` + """ + + message = str(datetime.datetime.now()) + query = self._shorten_query(query) + if error != "": + message += " ERR " + query + " " + error + else: + message = " OK " + query + + self._file.write(message+"\n") + +class LoggerWWO(Logger): + """ + WWO logger. + """ + + def _shorten_query(self, query): + return "".join([x for x in query.split("&") if x.startswith("q=")])