Module discord_lumberjack.handlers.discord_handler
Classes
class DiscordHandler (url: str, level: int = 0, message_creator: MessageCreator = None, http_headers: Mapping[str, Any] = None, flush_on_exit: bool = True)
-
A base class for logging handlers that send messages to Discord.
Args
url
:str
- The URL to make the request to. This can be a webhook URL, a channel URL, a direct message URL, or any other URL that Discord supports.
level
:int
, optional- The level at which to log. Defaults to logging.NOTSET.
message_creator
:MessageCreator
, optional- An instance of MessageCreator or one of its subclasses that will be used to create the message to send from each log record. Defaults to one that sends messages in monospace.
http_headers
:Mapping[str, Any]
, optional- A mapping of HTTP headers to send with the request. Defaults to an empty mapping.
Initializes the instance - basically setting the formatter to None and the filter list to empty.
Ancestors
- logging.Handler
- logging.Filterer
Subclasses
Methods
def emit(self, record: logging.LogRecord) ‑> None
-
Log the messages to Discord.
This method is non-blocking. The message will be send in the background.
Args
record
:logging.LogRecord
- The log record to send.
def flush(self, raise_exceptions=True)
-
Block until all logged messages are sent to Discord.
If an exception was raised while sending a message, it will be re-raised if
raise_exceptions
is True.You do not need to call this method to ensure all messages are sent before exiting the main thread unless you have set
flush_on_exit
in the constructor to False.Args
raise_exceptions
:bool
, optional- Whether to re-raise any exceptions that were raised while sending messages. Defaults to True.
Raises
Exception
- If an exception was raised while sending a message, and
raise_exceptions
is True.
def prepare_messages(self, record: logging.LogRecord) ‑> Iterable[Dict[str, Any]]
-
Given a log record, obtain all the message objects that will be sent to Discord.
This method gets all the messages from the message creator and calls transform_message which may be overridden by subclasses to transform each message as desired by each handler.
Args
record
:logging.LogRecord
- The log record to send.
Returns
Iterable[Dict[str, Any]]
- The messages to send to Discord.
def transform_message(self, message: Dict[str, Any]) ‑> Dict[str, Any]
-
Transform a message before sending it to Discord.
This method is called for each message that is sent to Discord. It is called before any fields are filtered out.
This method may be overridden by subclasses to transform each message as desired by each handler. By default, it keeps the message as is.
Args
message
:Dict[str, Any]
- The message to transform.
Returns
Dict[str, Any]
- The transformed message.