DCLogging is a simple logging library that sends log messages to a Discord channel.
The setup is simple and can be done via Maven or Gradle. You can find the latest version on this maven repository.
Repository
Add the following repository to your pom.xml
:
<repository>
<id>rettichlp-repository</id>
<name>RettichLP Repository</name>
<url>https://maven.rettichlp.de/releases</url>
</repository>
Dependency
Add the following dependency to your pom.xml
:
<dependency>
<groupId>de.rettichlp</groupId>
<artifactId>dclogging</artifactId>
<version>[VERSION]</version>
</dependency>
Repository
Add the following repository to your build.gradle.kts
:
maven {
name = "rettichlpRepository"
url = uri("https://maven.rettichlp.de/releases")
}
Dependency
Add the following dependency to your build.gradle.kts
:
implementation("de.rettichlp:dclogging:[VERSION]")
Repository
Add the following repository to your build.gradle
:
maven {
name "rettichlpRepository"
url "https://maven.rettichlp.de/releases"
}
Dependency
Add the following dependency to your build.gradle
:
implementation "de.rettichlp:dclogging:[VERSION]"
- To use the library, you need to create your own Discord bot at the Discord Developer Portal. Save the bot token (handle it as a secret), you will need it later.
- After creating the bot, you need to invite it to your server.
- To get the guild ID, right-click on the server icon and click on "Copy ID". You need this ID to send messages to the correct server.
- To get the text channel ID, right-click on the text channel and click on "Copy ID". You need this ID to send messages to the correct channel.
Create a new instance of the DiscordLogging
class. This class is used to send log messages to a Discord channel.
DiscordLogging discordLogging = DiscordLogging.builder()
.botToken("<yout-bot-token>") // required
.guildId("<your-guild-id>") // required
.textChannelId("<textChannelId>") // optional (default = discord guild system channel)
.appendStacktraceToError(false) // optional (default = true)
.build();
The templates are used to format the messages that are sent to the Discord channel. There are default templates for the different log levels (INFO, WARN, ERROR). You can also create your own templates.
Default templates:
Custom templates:
The following placeholders are available:
%timestamp%
- The timestamp of the log message%message%
- The message that was logged
MessageTemplate myCustomInfoMessageTemplate = new MessageTemplate("%timestamp% INFO: %message%");
MessageTemplate myCustomWarnMessageTemplate = new MessageTemplate("%timestamp% WARN: %message%");
MessageTemplate myCustomErrorMessageTemplate = new MessageTemplate("%timestamp% ERROR: %message%");
DiscordLogging discordLogging = DiscordLogging.builder()
.botToken("<yout-bot-token>") // required
.guildId("<your-guild-id>") // required
.textChannelId("<textChannelId>") // optional (default = discord guild system channel)
.appendStacktraceToError(false) // optional (default = true)
.infoMessageTemplate(myCustomInfoMessageTemplate) // optional
.warnMessageTemplate(myCustomWarnMessageTemplate) // optional
.errorMessageTemplate(myCustomErrorMessageTemplate) // optional
.build();
Now use the generated discordLogging
instance to log messages:
discordLogging.info("This is an info message");
discordLogging.warn("This is a warning message");
discordLogging.error("This is an error message");
You can also use placeholders in the message:
String messageType = "information";
discordLogging.info("This is an {} message", messageType);