mirror of
https://github.com/chubin/wttr.in
synced 2024-11-14 16:17:19 +00:00
Add lib/proxy_log.py
This commit is contained in:
parent
5a22d31839
commit
e4d4ebc41f
1 changed files with 45 additions and 0 deletions
45
lib/proxy_log.py
Normal file
45
lib/proxy_log.py
Normal file
|
@ -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=")])
|
Loading…
Reference in a new issue