SupportChild/Logger.cs

160 lines
5.2 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
2022-08-21 07:33:27 +00:00
using System;
using DSharpPlus;
using DSharpPlus.Exceptions;
2022-08-21 07:33:27 +00:00
namespace SupportChild;
2024-12-26 05:41:36 +00:00
internal class LogTestFactory : ILoggerProvider
2022-08-21 07:33:27 +00:00
{
2024-12-26 05:41:36 +00:00
public void Dispose() { }
public ILogger CreateLogger(string categoryName)
{
2024-12-26 05:41:36 +00:00
return Logger.instance;
}
2024-12-26 05:41:36 +00:00
}
public class Logger : ILogger
{
public static Logger instance { get; } = new Logger();
private static LogLevel minimumLogLevel = LogLevel.Trace;
private readonly object @lock = new();
2022-08-21 07:33:27 +00:00
2024-12-26 05:41:36 +00:00
internal static void SetLogLevel(LogLevel level)
{
2024-12-26 05:41:36 +00:00
minimumLogLevel = level;
}
2022-08-21 07:33:27 +00:00
2024-12-26 05:41:36 +00:00
internal static void Debug(string message, Exception exception = null)
{
2024-12-26 05:41:36 +00:00
instance.Log(LogLevel.Debug, new EventId(420, "BOT"), exception, message);
}
2022-08-21 07:33:27 +00:00
2024-12-26 05:41:36 +00:00
internal static void Log(string message, Exception exception = null)
{
2024-12-26 05:41:36 +00:00
instance.Log(LogLevel.Information, new EventId(420, "BOT"), exception, message);
}
internal static void Warn(string message, Exception exception = null)
{
instance.Log(LogLevel.Warning, new EventId(420, "BOT"), exception, message);
}
internal static void Error(string message, Exception exception = null)
{
instance.Log(LogLevel.Error, new EventId(420, "BOT"), exception, message);
}
internal static void Fatal(string message, Exception exception = null)
{
instance.Log(LogLevel.Critical, new EventId(420, "BOT"), exception, message);
}
public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= minimumLogLevel && logLevel != LogLevel.None;
}
public IDisposable BeginScope<TState>(TState state) where TState : notnull => default;
private static ConsoleColor GetLogLevelColour(LogLevel logLevel)
{
return logLevel switch
{
2024-12-26 05:41:36 +00:00
LogLevel.Trace => ConsoleColor.White,
LogLevel.Debug => ConsoleColor.DarkGray,
LogLevel.Information => ConsoleColor.DarkBlue,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Error => ConsoleColor.Red,
_ => ConsoleColor.White
};
}
2022-08-21 07:33:27 +00:00
2024-12-26 05:41:36 +00:00
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
2024-12-26 05:41:36 +00:00
if (!IsEnabled(logLevel))
return;
2024-12-26 12:26:48 +00:00
// Ratelimit messages are usually warnings, but they are unimportant in this case so downgrade them to debug.
if (formatter(state, exception).StartsWith("Hit Discord ratelimit on route ") && logLevel == LogLevel.Warning)
{
logLevel = LogLevel.Debug;
}
// The bot will handle NotFoundExceptions on its own, downgrade to debug
else if (exception is NotFoundException && eventId == LoggerEvents.RestError)
2024-12-26 12:26:48 +00:00
{
logLevel = LogLevel.Debug;
}
2024-12-26 05:41:36 +00:00
string[] logLevelParts = logLevel switch
{
2024-12-26 05:41:36 +00:00
LogLevel.Trace => ["[", "Trace", "] "],
LogLevel.Debug => ["[", "Debug", "] "],
2024-12-26 12:35:11 +00:00
LogLevel.Information => [" [", "Info", "] "],
LogLevel.Warning => [" [", "Warn", "] "],
2024-12-26 05:41:36 +00:00
LogLevel.Error => ["[", "Error", "] "],
2024-12-26 12:35:11 +00:00
LogLevel.Critical => [" [", "\u001b[1mCrit\u001b[0m", "] "],
_ => [" [", "None", "] "],
2024-12-26 05:41:36 +00:00
};
lock (@lock)
{
2024-12-26 05:41:36 +00:00
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("[");
Console.ResetColor();
Console.ForegroundColor = GetLogLevelColour(logLevel);
2024-12-26 06:26:54 +00:00
if (logLevel == LogLevel.Critical)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
}
2024-12-26 05:41:36 +00:00
Console.Write($"{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")}");
2024-12-26 06:26:54 +00:00
Console.ResetColor();
2024-12-26 05:41:36 +00:00
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("[");
Console.ForegroundColor = eventId == 420 ? ConsoleColor.Green : ConsoleColor.DarkGreen;
Console.Write(eventId == 420 ? "BOT" : "API");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("] ");
Console.Write(logLevelParts[0]);
Console.ForegroundColor = GetLogLevelColour(logLevel);
if (logLevel == LogLevel.Critical)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
}
Console.Write(logLevelParts[1]);
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(logLevelParts[2]);
Console.ResetColor();
if (logLevel is LogLevel.Trace or LogLevel.Debug)
{
Console.ForegroundColor = ConsoleColor.Gray;
}
2024-12-26 06:26:54 +00:00
else if (logLevel is LogLevel.Critical or LogLevel.Error)
{
Console.ForegroundColor = ConsoleColor.Red;
}
2024-12-26 05:41:36 +00:00
Console.WriteLine(formatter(state, exception));
if (exception != null)
{
2024-12-26 06:26:54 +00:00
Console.ForegroundColor = ConsoleColor.Gray;
2024-12-26 05:41:36 +00:00
Console.WriteLine($"{exception} : {exception.Message}\n{exception.StackTrace}");
}
Console.ResetColor();
}
}
2022-08-21 07:33:27 +00:00
}