| | | 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 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Writes log entries to the system console, optionally colored by level. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Writes are serialized on a process-wide lock, so entries from concurrent threads never interleave. |
| | | 13 | | /// </remarks> |
| | | 14 | | /// <param name="configuration">Controls whether ANSI color sequences are emitted.</param> |
| | 29 | 15 | | public class ConsoleLogger(ConsoleLoggerConfiguration configuration) : IInternalLogger |
| | | 16 | | { |
| | 1 | 17 | | private static readonly Lock s_writeLock = new(); |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public void Trace(string message, string filePath, string methodName) |
| | | 21 | | { |
| | 3 | 22 | | Write(CustomLogLevel.Trace, filePath, methodName, message); |
| | 3 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public void Debug(string message, string filePath, string methodName) |
| | | 27 | | { |
| | 3 | 28 | | Write(CustomLogLevel.Debug, filePath, methodName, message); |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public void Info(string message, string filePath, string methodName) |
| | | 33 | | { |
| | 20 | 34 | | Write(CustomLogLevel.Information, filePath, methodName, message); |
| | 20 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public void Warn(string message, string filePath, string methodName) |
| | | 39 | | { |
| | 2 | 40 | | Write(CustomLogLevel.Warning, filePath, methodName, message); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public void Error(string message, string filePath, string methodName) |
| | | 45 | | { |
| | 3 | 46 | | Write(CustomLogLevel.Error, filePath, methodName, message); |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public void Exception(string message, string filePath, string methodName) |
| | | 51 | | { |
| | 2 | 52 | | Write(CustomLogLevel.Exception, filePath, methodName, message); |
| | 2 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public void Critical(string message, string filePath, string methodName) |
| | | 57 | | { |
| | 2 | 58 | | Write(CustomLogLevel.Critical, filePath, methodName, message); |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public void Fatal(string message, string filePath, string methodName) |
| | | 63 | | { |
| | 2 | 64 | | Write(CustomLogLevel.Fatal, filePath, methodName, message); |
| | 2 | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | private static string GetAnsiColorSequence(CustomLogLevel level) => level switch |
| | 1 | 68 | | { |
| | 0 | 69 | | CustomLogLevel.Trace => AnsiColors.DarkGray, |
| | 0 | 70 | | CustomLogLevel.Debug => AnsiColors.Cyan, |
| | 1 | 71 | | CustomLogLevel.Information => AnsiColors.Green, |
| | 0 | 72 | | CustomLogLevel.Warning => AnsiColors.Yellow, |
| | 0 | 73 | | CustomLogLevel.Error => AnsiColors.Red, |
| | 0 | 74 | | CustomLogLevel.Exception => AnsiColors.Magenta, |
| | 0 | 75 | | CustomLogLevel.Critical or CustomLogLevel.Fatal => AnsiColors.BrightRed, |
| | 0 | 76 | | _ => AnsiColors.White |
| | 1 | 77 | | }; |
| | | 78 | | |
| | | 79 | | private void Write(CustomLogLevel level, string filePath, string methodName, string message) |
| | | 80 | | { |
| | 37 | 81 | | var entry = LogEntryFactory.Create(level, filePath, methodName, message); |
| | | 82 | | |
| | 37 | 83 | | if (configuration.UseColors) |
| | | 84 | | { |
| | 1 | 85 | | _ = ConsoleAnsi.EnableVirtualTerminalProcessing(); |
| | | 86 | | |
| | 1 | 87 | | var ansiColor = GetAnsiColorSequence(level); |
| | | 88 | | |
| | | 89 | | const string colorReset = "\e[0m"; |
| | | 90 | | |
| | | 91 | | lock (s_writeLock) |
| | | 92 | | { |
| | 1 | 93 | | Console.Write(ansiColor + entry + colorReset); |
| | 1 | 94 | | } |
| | | 95 | | } |
| | | 96 | | else |
| | 36 | 97 | | { |
| | | 98 | | lock (s_writeLock) |
| | | 99 | | { |
| | 36 | 100 | | Console.Write(entry); |
| | 36 | 101 | | } |
| | | 102 | | } |
| | 37 | 103 | | } |
| | | 104 | | } |