Skip to content

Commit

Permalink
Add ability to turn on different log levels + improve discord client …
Browse files Browse the repository at this point in the history
…logging + update readme (#72)
  • Loading branch information
shayaantx authored May 15, 2022
1 parent e54069c commit 300acd7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ botdarr:
| Environment Variable | Description | Required | Default |
| :---: | :---: | :---: | :---: |
| DISCORD_TOKEN | The discord bot token (don't share) | yes - if you use discord | |
| DISCORD_CHANNELS | The actual discord channel(s) the bot lives in | yes - if you use discord |
| DISCORD_CHANNELS | The actual discord channel(s) names the bot has access to. If multiple channel names specified, separate via a comma | yes - if you use discord |
| TELEGRAM_TOKEN | The telegram bot token (don't share) | yes - if you use telegram |
| TELEGRAM_PRIVATE_CHANNELS | Your actual telegram channels your bot can respond in. This should be a list containing the name and id of the channel, i.e., CHANNEL_NAME:CHANNEL_ID to get the channel id, right click any post in private channel and copy post link you should see something like this, https://t.me/c/1408146664/63 the id is between c/<id>/<postId> example: plex-channel1:id1,plex-channel2:id2 | yes - if you use telegram |
| TELEGRAM_PRIVATE_GROUPS | Your actual telegram groups your bot can respond in. This should be a list containing the name and id of the group, i.e., GROUP_NAME:GROUP_ID to get the channel id, right click any post in private group and copy post link (you need to enable message history for this) you should see something like this, https://t.me/c/1408146664/63 the id is between c/<id>/<postId> example: plex-group1:id1,plex-group2:id2 | yes - if you use telegram |
Expand Down Expand Up @@ -178,6 +178,7 @@ botdarr:
| COMMAND_PREFIX | The command prefix (default is !). Any prefix is allowed (but I haven't tested every single prefix in every client) | yes | ! |
| STATUS_ENDPOINTS | Endpoints that can be used to return statuses via !status command. The endpoints are separated by a comma and each endpoint is in the following format - name:hostname:port | no | |
| TIMEOUT | The connection/read timeout value (in milliseconds) for all outbound requests | no | 5000 |
| LOG_LEVEL | The log4j log level | no | info |
<br/>

## Usage
Expand Down
1 change: 1 addition & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ if [ ! -e "$propertiesFile" ]; then
[[ ! -z "${MAX_MOVIE_REQUESTS_PER_USER}" ]] && addConfiguration "max-movie-requests-per-user" "${MAX_MOVIE_REQUESTS_PER_USER}" "${propertiesFile}"
[[ ! -z "${EXISTING_ITEM_PATHS_BLACKLIST}" ]] && addConfiguration "existing-item-paths-blacklist" "${EXISTING_ITEM_PATHS_BLACKLIST}" "${propertiesFile}"
[[ ! -z "${STATUS_ENDPOINTS}" ]] && addConfiguration "status-endpoints" "${STATUS_ENDPOINTS}" "${propertiesFile}"
[[ ! -z "${LOG_LEVEL}" ]] && addConfiguration "log-level" "${LOG_LEVEL}" "${propertiesFile}"

addConfiguration "max-downloads-to-show" "${MAX_DOWNLOADS_TO_SHOW:-20}" "${propertiesFile}"
addConfiguration "max-results-to-show" "${MAX_RESULTS_TO_SHOW:-20}" "${propertiesFile}"
Expand Down
10 changes: 8 additions & 2 deletions sample.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Your bot token goes here (don't share)
#discord-token=
# The actual discord channel(s) the bot lives in
# The actual discord channel(s) name the bot lives in
# This is the specific channel(s) name, not the channel id
# If you specify multiple channels, they should be separated by a column
#discord-channels=

# Your slack bot oauth authentication token
Expand Down Expand Up @@ -88,4 +90,8 @@ existing-item-paths-blacklist=

# Comma delimited Status endpoints (i.e., endpoints you want checked when the !status command is called)
# i.e., some-name:hostname:port,some-other-name:hostname2:port
#status-endpoints=
#status-endpoints=

# The log4j log level (i.e., info, debug, trace, warn)
# This is info by default
#log-level=
20 changes: 19 additions & 1 deletion src/main/java/com/botdarr/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import com.botdarr.clients.matrix.MatrixBootstrap;
import com.botdarr.clients.slack.SlackBootstrap;
import com.botdarr.clients.telegram.TelegramBootstrap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.util.Strings;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.function.BiConsumer;

import static com.botdarr.commands.StatusCommand.*;

Expand All @@ -36,6 +41,14 @@ private Config() {
properties = new Properties();
properties.load(input);

String logLevel = properties.getProperty(Constants.LOG_LEVEL);
if (!Strings.isEmpty(logLevel)) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
config.getLoggers().forEach((s, loggerConfig) -> loggerConfig.setLevel(Level.valueOf(logLevel)));
ctx.updateLoggers();
}

List<ChatClientBootstrap> availableBootstraps = Arrays.asList(
new DiscordBootstrap(),
new MatrixBootstrap(),
Expand Down Expand Up @@ -85,7 +98,7 @@ private Config() {
LOGGER.warn("Lidarr commands are not enabled, make sure you set the lidarr url, path, token, default profile");
}

String configuredPrefix = properties.getProperty(Config.Constants.COMMAND_PREFIX);
String configuredPrefix = properties.getProperty(Constants.COMMAND_PREFIX);
if (!Strings.isEmpty(configuredPrefix)) {
chatClientBootstrap.validatePrefix(configuredPrefix);
}
Expand Down Expand Up @@ -387,6 +400,11 @@ public static final class Constants {
* Connection/read timeouts for all outbound requests
*/
public static final String TIMEOUT = "timeout";

/**
* Config for the log level
*/
public static final String LOG_LEVEL = "log-level";
}

private static String propertiesPath = "config/properties";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.collect.Sets;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel;
import org.apache.logging.log4j.LogManager;

import java.util.*;
import java.util.List;
Expand All @@ -32,9 +33,11 @@ private void sendMessages(MessageSender messageSender, String channelName) {
Set<String> supportedDiscordChannels = Sets.newHashSet(Splitter.on(',').trimResults().split(Config.getProperty(Config.Constants.DISCORD_CHANNELS)));
for (TextChannel textChannel : jda.getTextChannels()) {
if (!supportedDiscordChannels.contains(textChannel.getName())) {
LogManager.getLogger("com.botdarr.clients.discord").warn("Channel " + textChannel.getName() + " is not configured to receive messages");
continue;
}
if (channelName != null && !channelName.equalsIgnoreCase(textChannel.getName())) {
LogManager.getLogger("com.botdarr.clients.discord").debug("Channel " + textChannel.getName() + " doesn't match target channel " + channelName);
continue;
}
messageSender.send(textChannel);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.3
5.3.4

0 comments on commit 300acd7

Please sign in to comment.