-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Logger.cs
162 lines (134 loc) · 5.32 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Microsoft.Extensions.Logging;
using System;
using DSharpPlus;
using DSharpPlus.Exceptions;
namespace SupportBoi;
internal class LogTestFactory : ILoggerProvider
{
public void Dispose() { /* nothing to dispose */ }
public ILogger CreateLogger(string categoryName)
{
return Logger.instance;
}
}
public class Logger : ILogger
{
public static Logger instance { get; } = new Logger();
private static LogLevel minimumLogLevel = LogLevel.Trace;
private readonly object @lock = new();
internal static void SetLogLevel(LogLevel level)
{
minimumLogLevel = level;
}
internal static void Debug(string message, Exception exception = null)
{
instance.Log(LogLevel.Debug, new EventId(420, "BOT"), exception, message);
}
internal static void Log(string message, Exception exception = null)
{
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
{
LogLevel.Trace => ConsoleColor.White,
LogLevel.Debug => ConsoleColor.DarkGray,
LogLevel.Information => ConsoleColor.DarkBlue,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Error => ConsoleColor.Red,
_ => ConsoleColor.White
};
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
// 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)
{
logLevel = LogLevel.Debug;
}
string[] logLevelParts = logLevel switch
{
LogLevel.Trace => ["[", "Trace", "] "],
LogLevel.Debug => ["[", "Debug", "] "],
LogLevel.Information => [" [", "Info", "] "],
LogLevel.Warning => [" [", "Warn", "] "],
LogLevel.Error => ["[", "Error", "] "],
LogLevel.Critical => [" [", "\u001b[1mCrit\u001b[0m", "] "],
_ => [" [", "None", "] "],
};
lock (@lock)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("[");
Console.ResetColor();
Console.ForegroundColor = GetLogLevelColour(logLevel);
if (logLevel == LogLevel.Critical)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
}
Console.Write($"{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")}");
Console.ResetColor();
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;
}
else if (logLevel is LogLevel.Critical or LogLevel.Error)
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.WriteLine(formatter(state, exception));
if (exception != null)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"{exception} : {exception.Message}\n{exception.StackTrace}");
}
Console.ResetColor();
}
}
}