| | | 1 | | using ArturRios.Logging.Configuration; |
| | | 2 | | using ArturRios.Logging.Factories; |
| | | 3 | | using ArturRios.Logging.Interfaces; |
| | | 4 | | using ArturRios.Util.Collections; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Logging.Loggers; |
| | | 7 | | |
| | 29 | 8 | | public class ConsoleLogger(ConsoleLoggerConfiguration configuration) : IInternalLogger |
| | | 9 | | { |
| | 1 | 10 | | private static readonly Lock s_writeLock = new(); |
| | | 11 | | |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | public void Trace(string message, string filePath, string methodName) |
| | 3 | 14 | | { |
| | 3 | 15 | | Write(CustomLogLevel.Trace, filePath, methodName, message); |
| | 3 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public void Debug(string message, string filePath, string methodName) |
| | 3 | 20 | | { |
| | 3 | 21 | | Write(CustomLogLevel.Debug, filePath, methodName, message); |
| | 3 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public void Info(string message, string filePath, string methodName) |
| | 20 | 26 | | { |
| | 20 | 27 | | Write(CustomLogLevel.Information, filePath, methodName, message); |
| | 20 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public void Warn(string message, string filePath, string methodName) |
| | 2 | 32 | | { |
| | 2 | 33 | | Write(CustomLogLevel.Warning, filePath, methodName, message); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public void Error(string message, string filePath, string methodName) |
| | 3 | 38 | | { |
| | 3 | 39 | | Write(CustomLogLevel.Error, filePath, methodName, message); |
| | 3 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public void Exception(string message, string filePath, string methodName) |
| | 2 | 44 | | { |
| | 2 | 45 | | Write(CustomLogLevel.Exception, filePath, methodName, message); |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public void Critical(string message, string filePath, string methodName) |
| | 2 | 50 | | { |
| | 2 | 51 | | Write(CustomLogLevel.Critical, filePath, methodName, message); |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public void Fatal(string message, string filePath, string methodName) |
| | 2 | 56 | | { |
| | 2 | 57 | | Write(CustomLogLevel.Fatal, filePath, methodName, message); |
| | 2 | 58 | | } |
| | | 59 | | |
| | 1 | 60 | | private static string GetAnsiColorSequence(CustomLogLevel level) => level switch |
| | 1 | 61 | | { |
| | 0 | 62 | | CustomLogLevel.Trace => AnsiColors.DarkGray, |
| | 0 | 63 | | CustomLogLevel.Debug => AnsiColors.Cyan, |
| | 1 | 64 | | CustomLogLevel.Information => AnsiColors.Green, |
| | 0 | 65 | | CustomLogLevel.Warning => AnsiColors.Yellow, |
| | 0 | 66 | | CustomLogLevel.Error => AnsiColors.Red, |
| | 0 | 67 | | CustomLogLevel.Exception => AnsiColors.Magenta, |
| | 0 | 68 | | CustomLogLevel.Critical or CustomLogLevel.Fatal => AnsiColors.BrightRed, |
| | 0 | 69 | | _ => AnsiColors.White |
| | 1 | 70 | | }; |
| | | 71 | | |
| | | 72 | | private void Write(CustomLogLevel level, string filePath, string methodName, string message) |
| | 37 | 73 | | { |
| | 37 | 74 | | var entry = LogEntryFactory.Create(level, filePath, methodName, message); |
| | | 75 | | |
| | 37 | 76 | | if (configuration.UseColors) |
| | 1 | 77 | | { |
| | 1 | 78 | | _ = ConsoleAnsi.EnableVirtualTerminalProcessing(); |
| | | 79 | | |
| | 1 | 80 | | var ansiColor = GetAnsiColorSequence(level); |
| | | 81 | | |
| | | 82 | | const string colorReset = "\e[0m"; |
| | | 83 | | |
| | | 84 | | lock (s_writeLock) |
| | 1 | 85 | | { |
| | 1 | 86 | | Console.Write(ansiColor + entry + colorReset); |
| | 1 | 87 | | } |
| | 1 | 88 | | } |
| | | 89 | | else |
| | 36 | 90 | | { |
| | | 91 | | lock (s_writeLock) |
| | 36 | 92 | | { |
| | 36 | 93 | | Console.Write(entry); |
| | 36 | 94 | | } |
| | 36 | 95 | | } |
| | 37 | 96 | | } |
| | | 97 | | } |