From 62987449189c4501c7467c98e9ec50532d482cea Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 18 Dec 2023 11:27:28 +0100 Subject: [PATCH] Better logging --- Framework/Logging/FileLog.cs | 17 +++++++++++++ Tools/BiblioTech/AdminChecker.cs | 16 +++++++++++-- Tools/BiblioTech/BaseCommand.cs | 6 +++-- Tools/BiblioTech/CommandHandler.cs | 10 +++++++- .../BiblioTech/Commands/GetBalanceCommand.cs | 2 -- Tools/BiblioTech/Commands/MintCommand.cs | 7 +++--- Tools/BiblioTech/Configuration.cs | 14 ++++++----- Tools/BiblioTech/Program.cs | 24 +++++++++++++------ Tools/BiblioTech/RandomBusyMessage.cs | 2 +- 9 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 Framework/Logging/FileLog.cs diff --git a/Framework/Logging/FileLog.cs b/Framework/Logging/FileLog.cs new file mode 100644 index 0000000..cc5e995 --- /dev/null +++ b/Framework/Logging/FileLog.cs @@ -0,0 +1,17 @@ +namespace Logging +{ + public class FileLog : BaseLog + { + public FileLog(string fullFilename) + { + FullFilename = fullFilename; + } + + public string FullFilename { get; } + + protected override string GetFullName() + { + return FullFilename; + } + } +} diff --git a/Tools/BiblioTech/AdminChecker.cs b/Tools/BiblioTech/AdminChecker.cs index 4701520..e1c82cd 100644 --- a/Tools/BiblioTech/AdminChecker.cs +++ b/Tools/BiblioTech/AdminChecker.cs @@ -1,4 +1,5 @@ -using Discord.WebSocket; +using Discord; +using Discord.WebSocket; namespace BiblioTech { @@ -7,6 +8,7 @@ namespace BiblioTech private SocketGuild guild = null!; private ulong[] adminIds = Array.Empty(); private DateTime lastUpdate = DateTime.MinValue; + private ISocketMessageChannel adminChannel = null!; public void SetGuild(SocketGuild guild) { @@ -20,11 +22,21 @@ namespace BiblioTech return adminIds.Contains(userId); } - public bool IsAdminChannel(ISocketMessageChannel channel) + public bool IsAdminChannel(IChannel channel) { return channel.Name == Program.Config.AdminChannelName; } + public ISocketMessageChannel GetAdminChannel() + { + return adminChannel; + } + + public void SetAdminChannel(ISocketMessageChannel adminChannel) + { + this.adminChannel = adminChannel; + } + private bool ShouldUpdate() { return !adminIds.Any() || (DateTime.UtcNow - lastUpdate) > TimeSpan.FromMinutes(10); diff --git a/Tools/BiblioTech/BaseCommand.cs b/Tools/BiblioTech/BaseCommand.cs index dc23d68..44695fd 100644 --- a/Tools/BiblioTech/BaseCommand.cs +++ b/Tools/BiblioTech/BaseCommand.cs @@ -17,22 +17,24 @@ namespace BiblioTech try { + Program.Log.Log($"Responding to '{Name}'"); var context = new CommandContext(command, command.Data.Options); await command.RespondAsync(StartingMessage, ephemeral: IsEphemeral(context)); await Invoke(context); } catch (Exception ex) { + var msg = "Failed with exception: " + ex; if (IsInAdminChannel(command)) { - var msg = "Failed with exception: " + ex; await command.FollowupAsync(msg.Substring(0, Math.Min(1900, msg.Length))); } else { await command.FollowupAsync("Something failed while trying to do that...", ephemeral: true); + await Program.AdminChecker.GetAdminChannel().SendMessageAsync(msg); } - Console.WriteLine(ex); + Program.Log.Error(msg); } } diff --git a/Tools/BiblioTech/CommandHandler.cs b/Tools/BiblioTech/CommandHandler.cs index 88b45aa..800ac4b 100644 --- a/Tools/BiblioTech/CommandHandler.cs +++ b/Tools/BiblioTech/CommandHandler.cs @@ -23,18 +23,26 @@ namespace BiblioTech { var guild = client.Guilds.Single(g => g.Name == Program.Config.ServerName); Program.AdminChecker.SetGuild(guild); + Program.Log.Log($"Initializing for guild: '{guild.Name}'"); + + var adminChannels = guild.TextChannels.Where(Program.AdminChecker.IsAdminChannel).ToArray(); + if (adminChannels == null || !adminChannels.Any()) throw new Exception("No admin message channel"); + Program.AdminChecker.SetAdminChannel(adminChannels.First()); var builders = commands.Select(c => { + var msg = $"Building command '{c.Name}' with options: "; var builder = new SlashCommandBuilder() .WithName(c.Name) .WithDescription(c.Description); foreach (var option in c.Options) { + msg += option.Name + " "; builder.AddOption(option.Build()); } + Program.Log.Log(msg); return builder; }); @@ -48,7 +56,7 @@ namespace BiblioTech catch (HttpException exception) { var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented); - Console.WriteLine(json); + Program.Log.Error(json); } } diff --git a/Tools/BiblioTech/Commands/GetBalanceCommand.cs b/Tools/BiblioTech/Commands/GetBalanceCommand.cs index 60118f5..964d7c2 100644 --- a/Tools/BiblioTech/Commands/GetBalanceCommand.cs +++ b/Tools/BiblioTech/Commands/GetBalanceCommand.cs @@ -1,7 +1,5 @@ using BiblioTech.Options; using CodexContractsPlugin; -using CodexPlugin; -using Core; using GethPlugin; namespace BiblioTech.Commands diff --git a/Tools/BiblioTech/Commands/MintCommand.cs b/Tools/BiblioTech/Commands/MintCommand.cs index 2481aa0..aa2ccf6 100644 --- a/Tools/BiblioTech/Commands/MintCommand.cs +++ b/Tools/BiblioTech/Commands/MintCommand.cs @@ -48,7 +48,7 @@ namespace BiblioTech.Commands if (ShouldMintTestTokens(contracts, addr)) { var transaction = contracts.MintTestTokens(addr, defaultTestTokensToMint); - report.Add($"Minted {defaultTestTokensToMint}. ({FormatTransactionLink(transaction)})"); + report.Add($"Minted {defaultTestTokensToMint} {FormatTransactionLink(transaction)}"); return new Transaction(defaultTestTokensToMint, transaction); } @@ -61,7 +61,7 @@ namespace BiblioTech.Commands if (ShouldSendEth(gethNode, addr)) { var transaction = gethNode.SendEth(addr, defaultEthToSend); - report.Add($"Sent {defaultEthToSend}. ({FormatTransactionLink(transaction)})"); + report.Add($"Sent {defaultEthToSend} {FormatTransactionLink(transaction)}"); return new Transaction(defaultEthToSend, transaction); } report.Add("Eth balance is over threshold. (No Eth sent.)"); @@ -82,7 +82,8 @@ namespace BiblioTech.Commands private string FormatTransactionLink(string transaction) { - return $"https://explorer.testnet.codex.storage/tx/{transaction}"; + var url = $"https://explorer.testnet.codex.storage/tx/{transaction}"; + return $"- [View on block explorer]({url}){Environment.NewLine}Transaction ID - `{transaction}`"; } } } diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index eb58870..bce62ea 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -19,12 +19,6 @@ namespace BiblioTech [Uniform("admin-channel-name", "ac", "ADMINCHANNELNAME", true, "Name of the Discord server channel where admin commands are allowed.")] public string AdminChannelName { get; set; } = "admin-channel"; - [Uniform("kube-config", "kc", "KUBECONFIG", true, "Path to Kubeconfig file. Use a Kubeconfig with read-only access.")] - public string KubeConfigFile { get; set; } = "null"; - - [Uniform("kube-namespace", "kn", "KUBENAMESPACE", true, "Kubernetes namespace.")] - public string KubeNamespace { get; set; } = string.Empty; - public string EndpointsPath { get @@ -40,5 +34,13 @@ namespace BiblioTech return Path.Combine(DataPath, "users"); } } + + public string LogPath + { + get + { + return Path.Combine(DataPath, "logs"); + } + } } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index f759d97..22e68f8 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -2,6 +2,7 @@ using BiblioTech.Commands; using Discord; using Discord.WebSocket; +using Logging; namespace BiblioTech { @@ -11,13 +12,19 @@ namespace BiblioTech public static Configuration Config { get; private set; } = null!; public static UserRepo UserRepo { get; } = new UserRepo(); - public static AdminChecker AdminChecker { get; } = new AdminChecker(); + public static AdminChecker AdminChecker { get; private set; } = null!; + public static ILog Log { get; private set; } = null!; public static Task Main(string[] args) { var uniformArgs = new ArgsUniform(PrintHelp, args); Config = uniformArgs.Parse(); + Log = new LogSplitter( + new FileLog(Path.Combine(Config.LogPath, "discordbot.log")), + new ConsoleLog() + ); + EnsurePath(Config.DataPath); EnsurePath(Config.UserDataPath); EnsurePath(Config.EndpointsPath); @@ -27,9 +34,9 @@ namespace BiblioTech public async Task MainAsync() { - Console.WriteLine("Starting Codex Discord Bot..."); + Log.Log("Starting Codex Discord Bot..."); client = new DiscordSocketClient(); - client.Log += Log; + client.Log += ClientLog; var associateCommand = new UserAssociateCommand(); var sprCommand = new SprCommand(); @@ -43,18 +50,21 @@ namespace BiblioTech await client.LoginAsync(TokenType.Bot, Config.ApplicationToken); await client.StartAsync(); - Console.WriteLine("Running..."); + + AdminChecker = new AdminChecker(); + + Log.Log("Running..."); await Task.Delay(-1); } private static void PrintHelp() { - Console.WriteLine("BiblioTech - Codex Discord Bot"); + Log.Log("BiblioTech - Codex Discord Bot"); } - private Task Log(LogMessage msg) + private Task ClientLog(LogMessage msg) { - Console.WriteLine(msg.ToString()); + Log.Log("DiscordClient: " + msg.ToString()); return Task.CompletedTask; } diff --git a/Tools/BiblioTech/RandomBusyMessage.cs b/Tools/BiblioTech/RandomBusyMessage.cs index 5582767..baa370e 100644 --- a/Tools/BiblioTech/RandomBusyMessage.cs +++ b/Tools/BiblioTech/RandomBusyMessage.cs @@ -19,7 +19,7 @@ public static string Get() { - return messages[random.Next(messages.Length)]; + return "Hold on: " + messages[random.Next(messages.Length)]; } } }