Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Quick start
"class": "python_telegram_logger.Handler",
"token": "bot_token",
"chat_ids": [123456789, -1234567891011],

"proxy_url": "https://user:[email protected]:3128/" # optional
}
},
"loggers": {
Expand Down
15 changes: 9 additions & 6 deletions python_telegram_logger/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@


class Handler(logging.handlers.QueueHandler):

"""
Base handler which instantiate and start queue listener and message dispatcher
"""

def __init__(self, token: str, chat_ids: list, format: str=HTML,
disable_notifications: bool=False, disable_preview: bool=False):
def __init__(self, token: str, chat_ids: list, format: str = HTML,
disable_notifications: bool = False, disable_preview: bool = False, proxy_url: str = None):
"""
:param token: telegram bot API token
:param chat_ids: list of intergers with chat ids
Expand All @@ -44,7 +43,7 @@ def __init__(self, token: str, chat_ids: list, format: str=HTML,
except Exception:
raise Exception("TelegramLogging. Unknown format '%s'" % format)

self.handler = LogMessageDispatcher(token, chat_ids, disable_notifications, disable_preview)
self.handler = LogMessageDispatcher(token, chat_ids, disable_notifications, disable_preview, proxy_url)
self.handler.setFormatter(formatter())
listener = logging.handlers.QueueListener(queue, self.handler)
listener.start()
Expand All @@ -67,7 +66,8 @@ class LogMessageDispatcher(logging.Handler):
MAX_MSG_LEN = 4096
API_CALL_INTERVAL = 1 / 30 # 30 calls per second

def __init__(self, token: str, chat_ids: list, disable_notifications: bool=False, disable_preview: bool=False):
def __init__(self, token: str, chat_ids: list, disable_notifications: bool = False,
disable_preview: bool = False, proxy_url: str = None):
"""
See Handler args
"""
Expand All @@ -76,6 +76,10 @@ def __init__(self, token: str, chat_ids: list, disable_notifications: bool=False
self.disable_notifications = disable_notifications
self.disable_preview = disable_preview
self.session = requests.Session()

if proxy_url:
self.session.proxies = {'https': proxy_url}

super().__init__()

@property
Expand Down Expand Up @@ -118,7 +122,6 @@ def emit(self, record):
disable_web_page_preview=self.disable_preview,
disable_notifications=self.disable_notifications
)

response = self.session.get(url, timeout=self.TIMEOUT)
if not response.ok:
logger.warning("Telegram log dispatching failed with status code '%s'" % response.status_code)
Expand Down