Module discord_lumberjack.handlers
Handlers
This python module provides several logging handlers (located in the discord_lumberjack.handlers
module) which will send the logs it recieves to a Discord webhook, server channel, or DM channel.
The available handlers are:
DiscordChannelHandler
- Uses a bot token and a channel ID to send logs to the given channel from the given bot.DiscordDMHandler
- Uses a bot token and a user ID to send logs to the given user from the given bot.DiscordWebhookHandler
- Uses a webhook URL to send the logs to.DiscordHandler
- This is the base class for the other three. You probably don't want to use this unless you're creating your own fancy handler.
Sub-modules
discord_lumberjack.handlers.discord_channel_handler
discord_lumberjack.handlers.discord_dm_handler
discord_lumberjack.handlers.discord_handler
discord_lumberjack.handlers.discord_webhook_handler
Classes
class DiscordChannelHandler (bot_token: str, channel_id: int, level: int = 0, message_creator: MessageCreator = None)
-
A logging handler that sends messages to a Discord Channel from a Bot.
Args
bot_token
:str
- The authentication token of the Bot to send the message with.
channel_id
:int
- The ID of the Channel to send the message to.
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.
Initializes the instance - basically setting the formatter to None and the filter list to empty.
Ancestors
- DiscordHandler
- logging.Handler
- logging.Filterer
Subclasses
Inherited members
class DiscordDMHandler (bot_token: str, user_id: int, level: int = 0, message_creator: MessageCreator = None)
-
A logging handler that sends messages to a Discord Direct Message Channel from a Bot.
Upon construction, this handler will attempt to create a DM channel with the user specified by the user_id argument. If this fails, the constructor will raise a ValueError.
Since a DM channel is a kind of channel, this handler is a subclass of DiscordChannelHandler.
Args
bot_token
:str
- The authentication token of the Bot to send the message with.
user_id
:int
- The ID of the user to send the message to.
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.
Initializes the instance - basically setting the formatter to None and the filter list to empty.
Ancestors
- DiscordChannelHandler
- DiscordHandler
- logging.Handler
- logging.Filterer
Methods
def create_dm_channel(self, user_id: int, bot_token: str) ‑> int
-
Create a DM channel through the discord API.
Args
user_id
:int
- The ID of the user to create a DM channel with.
Returns
int
- The ID of the DM channel.
Raises
ValueError
- If the bot was unable to create a DM channel with the user.
Inherited members
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.
class DiscordWebhookHandler (url: str, level: int = 0, message_creator: MessageCreator = None, username: str = None, avatar_url: str = None)
-
A logging handler that sends messages to a Discord webhook.
The username and avatar fields will override those provided by the message creator if provided here.
Args
url
:str
- The URL to make the request to. This must be a webhook URL.
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.
username
:str
, optional- The username to use when sending messages. Defaults to None.
avatar_url
:str
, optional- The avatar URL to use when sending messages. Defaults to None.
Initializes the instance - basically setting the formatter to None and the filter list to empty.
Ancestors
- DiscordHandler
- logging.Handler
- logging.Filterer
Methods
def transform_message(self, message: Dict[str, Any]) ‑> Dict[str, Any]
-
Replace the username and avatar fields set by the message creator (if any) with those provided to the handler (if any).
Args
message
:Dict[str, Any]
- The message provided by the message creator.
Returns
Dict[str, Any]
- The transformed message.
Inherited members