-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add slack logger * bump version to 0.0.8 * check against more modern versions of python * also change django versions to test against * tests passing locally * bump versions * fix test paths * fix py3.1 version in github tests ?? 3.10.5 to be explicit * add pytz since it was removed in django 4
- Loading branch information
Showing
8 changed files
with
168 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from collections import OrderedDict | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
Most of this Slack logging is pulled from here: | ||
https://github.com/junhwi/python-slack-logger/ | ||
""" | ||
import os | ||
import logging | ||
import json | ||
|
||
from logging import LogRecord | ||
from urllib.parse import urlparse | ||
from logging.handlers import HTTPHandler | ||
|
||
|
||
class SlackHandler(HTTPHandler): | ||
def __init__(self, url, username=None, icon_url=None, icon_emoji=None, channel=None, mention=None): | ||
o = urlparse(url) | ||
is_secure = o.scheme == 'https' | ||
HTTPHandler.__init__(self, o.netloc, o.path, method="POST", secure=is_secure) | ||
self.username = username | ||
self.icon_url = icon_url | ||
self.icon_emoji = icon_emoji | ||
self.channel = channel | ||
self.mention = mention and mention.lstrip('@') | ||
|
||
def mapLogRecord(self, record): | ||
text = self.format(record) | ||
|
||
if isinstance(self.formatter, SlackFormatter): | ||
payload = { | ||
'attachments': [ | ||
text, | ||
], | ||
} | ||
if self.mention: | ||
payload['text'] = '<@{0}>'.format(self.mention) | ||
else: | ||
if self.mention: | ||
text = '<@{0}> {1}'.format(self.mention, text) | ||
payload = { | ||
'text': text, | ||
} | ||
|
||
if self.username: | ||
payload['username'] = self.username | ||
if self.icon_url: | ||
payload['icon_url'] = self.icon_url | ||
if self.icon_emoji: | ||
payload['icon_emoji'] = self.icon_emoji | ||
if self.channel: | ||
payload['channel'] = self.channel | ||
|
||
ret = { | ||
'payload': json.dumps(payload), | ||
} | ||
return ret | ||
|
||
|
||
class SlackFormatter(logging.Formatter): | ||
def format(self, record): | ||
ret = {} | ||
if record.levelname == 'INFO': | ||
ret['color'] = 'good' | ||
elif record.levelname == 'WARNING': | ||
ret['color'] = 'warning' | ||
elif record.levelname == 'ERROR': | ||
ret['color'] = '#E91E63' | ||
elif record.levelname == 'CRITICAL': | ||
ret['color'] = 'danger' | ||
|
||
ret['author_name'] = record.levelname | ||
ret['title'] = record.name | ||
ret['ts'] = record.created | ||
ret['text'] = super(SlackFormatter, self).format(record) | ||
return ret | ||
|
||
|
||
class SlackLogFilter(logging.Filter): | ||
""" | ||
Logging filter to decide when logging to Slack is requested, using | ||
the `extra` kwargs: | ||
`logger.info("...", extra={'notify_slack': True})` | ||
""" | ||
|
||
def filter(self, record): | ||
return getattr(record, 'notify_slack', False) | ||
|
||
|
||
class CkcSlackHandler(SlackHandler): | ||
""" | ||
Override the default handler to insert our own URL | ||
""" | ||
def __init__(self, **kwargs): | ||
url = os.getenv('SLACK_WEBHOOK_URL') | ||
super().__init__(url, **kwargs) | ||
|
||
def format(self, record: LogRecord) -> str: | ||
"""Surround our log message in a "code block" for styling.""" | ||
return f"```{super().format(record)}```" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
# These requirements are for local development and testing of the module | ||
|
||
# python packaging | ||
twine==3.1.1 | ||
twine==4.0.1 | ||
|
||
# django stuff | ||
Django==3.1.12 | ||
djangorestframework==3.12.4 | ||
Django==4.0.6 | ||
djangorestframework==3.13.1 | ||
pytz==2022.1 | ||
|
||
# factories | ||
factory-boy==3.2.0 | ||
factory-boy==3.2.1 | ||
|
||
# tests | ||
pytest==5.4.1 | ||
pytest-django==3.9.0 | ||
pytest-pythonpath==0.7.3 | ||
flake8==3.7.9 | ||
pytest==7.1.2 | ||
pytest-django==4.5.2 | ||
flake8==4.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,19 +27,17 @@ per-file-ignores = | |
[tool:pytest] | ||
addopts = --reuse-db | ||
DJANGO_SETTINGS_MODULE = testproject.settings | ||
python_paths = testproject | ||
pythonpath = . testproject | ||
python_files = | ||
tests/integration/*.py | ||
tests/functional/*.py | ||
test_paths = | ||
tests/ | ||
|
||
[metadata] | ||
name = django-ckc | ||
author = Eric Carmichael | ||
author_email = [email protected] | ||
description = tools, utilities, etc. we use across projects @ ckc | ||
version = 0.0.7 | ||
version = 0.0.8 | ||
url = https://github.com/ckcollab/django-ckc | ||
keywords = | ||
django | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters