discord_logging.handler.DiscordHandler

class DiscordHandler

Bases: logging.Handler

Output logs to Discord chat.

A handler class which writes logging records, appropriately formatted, to a Discord Server using webhooks.

Methods

__init__(service_name, webhook_url[, ...])

param service_name

Shows at the bot username in Discord.

attempt_to_report_failure(resp, orignal)

Attempt to report a failure to deliver a log message.

clip_content(content[, max_len, clip_to_end])

Make sure the text fits to a Discord message.

emit(record)

Send a log entry to Discord.

should_format_as_code_block(record, msg)

Figure out whether we want to use code block formatting in Discord.

split_by_break_character(content)

Split the inbound log message to several Discord messages.

Attributes

name

__init__(service_name, webhook_url, colours={None: 2040357, 50: 14362664, 40: 14362664, 30: 16497928, 20: 2196944, 10: 8947848}, emojis={None: '', 50: '🆘', 40: '❌', 30: '⚠️', 20: '', 10: ''}, avatar_url=None, rate_limit_retry=True, embed_line_wrap_threshold=60, message_break_char=None, discord_timeout=5.0)
Parameters
  • service_name (str) – Shows at the bot username in Discord.

  • webhook_url (str) – Channel webhook URL. See README for details.

  • colours – Log level to Discord embed color mapping

  • emojis – Log level to emoticon decoration mapping. If present this is appended as a prefix to the first line of the log.

  • avatar_url (Optional[str]) – Bot profile picture

  • rate_limit_retry (bool) – Try to recover when Discord server tells us to slow down

  • embed_line_wrap_threshold (int) – How many characters a text line can contain until we go to “long line” output format.

  • message_break_char (Optional[str]) – If given, manually split log entry text to several Discord messages by this character. Useful if your application output long custom messages. For example, you can use ellipsis in your log message to split in several Discord messages to overcome the 2000 character limitation.

  • discord_timeout (float) – How many seconds to wait before giving up on Discord request.

attempt_to_report_failure(resp, orignal)

Attempt to report a failure to deliver a log message.

Usually this happens if we pass content to Discord the server does not like.

More information

Parameters
  • resp (requests.models.Response) –

  • orignal (discord_webhook.webhook.DiscordWebhook) –

clip_content(content, max_len=1900, clip_to_end=True)

Make sure the text fits to a Discord message.

Discord max message length is 2000 chars.

Parameters

content (str) –

Return type

str

emit(record)

Send a log entry to Discord.

Parameters

record (logging.LogRecord) –

should_format_as_code_block(record, msg)

Figure out whether we want to use code block formatting in Discord.

Check for new lines and long lines in the log message.

Parameters
  • record (logging.LogRecord) –

  • msg (str) –

Return type

bool

split_by_break_character(content)

Split the inbound log message to several Discord messages.

Uses manual break character method if set. If not set, then do nothing.

Parameters

content (str) –

Return type

List[str]