Skip to content

Add proxy-server support #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,52 @@ In such case, you can omit the `hook_url` key in the logging config.
.. _logging: https://docs.python.org/3/library/logging.html
.. _incoming webhook integration: https://my.slack.com/services/new/incoming-webhook/
.. _dictConfig: https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig

Proxy-server Support
++++++++++++++++++++

It's possible to use an external proxy-server. To do it just add proxy address as
proxy_url argument in the SlackHandler:

.. code-block:: python

h = SlackHandler(hook_url=URL, proxy_url=PROXY_URL)


or via config:

.. code-block:: python

DICT_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'slack_filter': {
'()': 'webhook_logger.slack.SlackLogFilter',
}
},
'handlers': {
'slack': {
'level': 'INFO',
'filters': ['slack_filter'],
'class': 'webhook_logger.slack.SlackHandler',
'hook_url': 'https://hooks.slack.com/services/XXXXXXXX/AAAAAAAAA/aaaaaaaaaaaa',
'proxy_url': 'proxy:3128',
'formatter': 'slack_format',
}
},
'formatters': {
'slack_format': {
'()': 'webhook_logger.slack.SlackFormatter',
'title': 'Your optional title'
},
},
'loggers': {
'my_logger': {
'handlers': ['slack'],
'level': 'DEBUG'
}
}
}


8 changes: 6 additions & 2 deletions webhook_logger/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
class SlackHandler(logging.Handler):
"""Logging handler to post to Slack to the webhook URL"""

def __init__(self, hook_url=None, *args, **kwargs):
def __init__(self, hook_url=None, proxy_url=None, *args, **kwargs):
super(SlackHandler, self).__init__(*args, **kwargs)
self._hook_url = hook_url
self._proxies = {
'http': proxy_url,
'https': proxy_url,
} if proxy_url else {}
self.formatter = SimpleSlackFormatter()

@property
Expand All @@ -31,7 +35,7 @@ def emit(self, record):
"""
try:
slack_data = self.format(record)
requests.post(self.hook_url, json=slack_data)
requests.post(self.hook_url, json=slack_data, proxies=self._proxies)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E501 line too long (80 > 79 characters)

except Exception:
self.handleError(record)

Expand Down