Skip to content

Adding /set-log-level command #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.togetherjava.tjbot.commands.reminder.RemindCommand;
import org.togetherjava.tjbot.commands.reminder.RemindRoutine;
import org.togetherjava.tjbot.commands.system.BotCore;
import org.togetherjava.tjbot.commands.system.LogLevelCommand;
import org.togetherjava.tjbot.commands.tags.TagCommand;
import org.togetherjava.tjbot.commands.tags.TagManageCommand;
import org.togetherjava.tjbot.commands.tags.TagSystem;
Expand Down Expand Up @@ -85,6 +86,7 @@ public enum Features {
features.add(new RejoinModerationRoleListener(actionsStore, config));

// Slash commands
features.add(new LogLevelCommand());
features.add(new PingCommand());
features.add(new TeXCommand());
features.add(new TagCommand(tagSystem));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.togetherjava.tjbot.commands.system;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configurator;
import org.jetbrains.annotations.NotNull;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.commands.SlashCommandVisibility;

import java.util.stream.Stream;

/**
* Implements the '/set-log-level' command which can be used to change the log level used by the
* bot, while it is running.
* <p>
* Example usage:
*
* <pre>
* {@code
* /set-log-level level: INFO
* }
* </pre>
*/
public final class LogLevelCommand extends SlashCommandAdapter {
private static final String LOG_LEVEL_OPTION = "level";

/**
* Creates a new instance.
*/
public LogLevelCommand() {
super("set-log-level", "Changes the log level of the bot while it is running.",
SlashCommandVisibility.GUILD);

OptionData option =
new OptionData(OptionType.STRING, LOG_LEVEL_OPTION, "the log level to set", true);
Stream.of(Level.values()).map(Level::name).forEach(level -> option.addChoice(level, level));

getData().addOptions(option);
}

// Security warning about changing log configs. We only change the level, that is safe.
@SuppressWarnings("squid:S4792")
@Override
public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
String levelText = event.getOption(LOG_LEVEL_OPTION).getAsString();
Level level = Level.getLevel(levelText);

if (level == null) {
event.reply("The selected log level '%s' is unknown.".formatted(levelText))
.setEphemeral(true)
.queue();
return;
}

Configurator.setAllLevels(LogManager.getRootLogger().getName(), level);
event.reply("Set the log level to '%s'.".formatted(levelText)).queue();
}
}