Skip to content

Commit aa1c3c7

Browse files
committed
(AI) Write a prettier formatter
1 parent ccb889f commit aa1c3c7

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Logging.Console;
3+
using Microsoft.Extensions.Logging.Abstractions;
4+
using static Crayon.Output;
5+
6+
namespace systray_doom;
7+
8+
/// <summary>
9+
/// A custom console formatter that uses Crayon for coloring and outputs logs in a single line.
10+
/// </summary>
11+
public sealed class CrayonConsoleFormatter : ConsoleFormatter
12+
{
13+
public CrayonConsoleFormatter() : base(nameof(CrayonConsoleFormatter))
14+
{
15+
}
16+
17+
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
18+
{
19+
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
20+
if (string.IsNullOrEmpty(message))
21+
{
22+
return;
23+
}
24+
25+
var logLevel = logEntry.LogLevel;
26+
var categoryName = logEntry.Category;
27+
var timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
28+
29+
// Apply colors based on log level
30+
var levelText = GetLogLevelText(logLevel);
31+
var coloredLevel = ApplyLogLevelColor(levelText, logLevel);
32+
var coloredMessage = ApplyMessageColor(message, logLevel);
33+
34+
// Format: [HH:mm:ss.fff] LEVEL Category: Message
35+
textWriter.WriteLine($"{Dim($"[{timestamp}]")} {coloredLevel} {Dim($"{categoryName}:")} {coloredMessage}");
36+
37+
// Write exception if present
38+
if (logEntry.Exception != null)
39+
{
40+
textWriter.WriteLine(Red(logEntry.Exception.ToString()));
41+
}
42+
}
43+
44+
private static string GetLogLevelText(LogLevel logLevel) => logLevel switch
45+
{
46+
LogLevel.Trace => "TRCE",
47+
LogLevel.Debug => "DBUG",
48+
LogLevel.Information => "INFO",
49+
LogLevel.Warning => "WARN",
50+
LogLevel.Error => "ERRO",
51+
LogLevel.Critical => "CRIT",
52+
LogLevel.None => "NONE",
53+
_ => "UNKN"
54+
};
55+
56+
private static string ApplyLogLevelColor(string levelText, LogLevel logLevel) => logLevel switch
57+
{
58+
LogLevel.Trace => Dim(levelText),
59+
LogLevel.Debug => Dim(levelText),
60+
LogLevel.Information => levelText,
61+
LogLevel.Warning => Yellow(levelText),
62+
LogLevel.Error => Red(levelText),
63+
LogLevel.Critical => Bold(Red(levelText)),
64+
_ => levelText
65+
};
66+
67+
private static string ApplyMessageColor(string message, LogLevel logLevel) => logLevel switch
68+
{
69+
LogLevel.Trace => Dim(message),
70+
LogLevel.Debug => Dim(message),
71+
LogLevel.Information => message,
72+
LogLevel.Warning => Yellow(message),
73+
LogLevel.Error => Red(message),
74+
LogLevel.Critical => Bold(Red(message)),
75+
_ => message
76+
};
77+
}

systray_doom/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
using systray_doom;
2222
using Windows.Graphics.DirectX;
2323
using Microsoft.Extensions.Logging;
24+
using Microsoft.Extensions.Logging.Console;
2425

2526
// Set in csproj, FYI.
2627
// [assembly:AssemblyTitle("Systray Doom")]
2728

28-
// Create a console logger
29+
// Create a console logger with custom Crayon formatter
2930
using var loggerFactory = LoggerFactory.Create(builder =>
30-
builder.AddConsole()
31-
.SetMinimumLevel(LogLevel.Debug));
31+
builder.AddConsole(options =>
32+
{
33+
options.FormatterName = nameof(CrayonConsoleFormatter);
34+
})
35+
.AddConsoleFormatter<CrayonConsoleFormatter, ConsoleFormatterOptions>()
36+
.SetMinimumLevel(LogLevel.Debug));
3237
var logger = loggerFactory.CreateLogger("SystrayDoom");
3338

3439
logger.LogInformation("Starting doom...");

0 commit comments

Comments
 (0)