Skip to content

Commit

Permalink
Embed format implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
2press committed Jan 7, 2019
1 parent 36bb9b4 commit 4b4cf5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
31 changes: 19 additions & 12 deletions discordlogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import absolute_import, unicode_literals

import logging
import traceback
from datetime import datetime

import requests

Expand Down Expand Up @@ -51,19 +53,24 @@ def format(self, record):
Format message content, timestamp when it was logged and a
coloured border depending on the severity of the message
"""
return {'content': record.getMessage()}
ret = {
'ts': record.created,
'text': record.getMessage(),
}
msg = record.getMessage()
exc = record.__dict__['exc_info']
if exc:
msg = msg + '\n```{}```'.format(traceback.format_exc())
embed = dict()
embed["description"] = msg
embed['timestamp'] = datetime.utcnow().isoformat()
embed['author'] = {'name': '{}@{}'.format(
record.name, record.filename)}
try:
loglevel_colour = {
'INFO': 'good',
'WARNING': 'warning',
'ERROR': '#E91E63',
'CRITICAL': 'danger',
colors = {
'DEBUG': 810979,
'INFO': 1756445,
'WARNING': 15633170,
'ERROR': 16731648,
'CRITICAL': 16711680,
}
ret['color'] = loglevel_colour[record.levelname]
embed['color'] = colors[record.levelname]
except KeyError:
pass
return {'attachments': [ret]}
return {'embeds': [embed]}
9 changes: 8 additions & 1 deletion test/test_discordlogger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import logging

from discordlogger import DiscordHandler
from discordlogger import DiscordFormatter, DiscordHandler

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
h = DiscordHandler(
webhook_url=URL)
h.setLevel(logging.INFO)
h.formatter = DiscordFormatter()
logger.addHandler(h)
logger.info("Hello World")
logger.warning('Warning!')
data = dict()
try:
print(data['hello'])
except Exception:
logger.exception('message')

0 comments on commit 4b4cf5a

Please sign in to comment.