Files
nt8-sdk/TASK-04-log-level.md
2026-02-24 15:00:41 -05:00

117 lines
3.1 KiB
Markdown

# TASK-04: Add Log Level Filter to BasicLogger
**File:** `src/NT8.Core/Logging/BasicLogger.cs`
**Priority:** HIGH
**No dependencies**
**Estimated time:** 20 min
---
## Background
`BasicLogger` currently writes every log level to console unconditionally. When `EnableVerboseLogging` is false in NT8, you want to suppress `Debug` and `Trace` output.
The current `ILogger` interface (check `src/NT8.Core/Logging/ILogger.cs`) only defines:
- `LogDebug`, `LogInformation`, `LogWarning`, `LogError`, `LogCritical`
---
## Exact Changes Required
### 1. Add `LogLevel` enum (check if it already exists first — search the project for `LogLevel`)
If it does NOT already exist, add it inside `BasicLogger.cs` or as a separate file in the same folder:
```csharp
namespace NT8.Core.Logging
{
/// <summary>
/// Log severity levels.
/// </summary>
public enum LogLevel
{
Debug = 0,
Information = 1,
Warning = 2,
Error = 3,
Critical = 4
}
}
```
### 2. Add `MinimumLevel` property to `BasicLogger`
```csharp
/// <summary>
/// Minimum log level to write. Messages below this level are suppressed.
/// Default is Information.
/// </summary>
public LogLevel MinimumLevel { get; set; }
```
### 3. Update constructor to default to `Information`
```csharp
public BasicLogger(string categoryName = "")
{
_categoryName = categoryName;
MinimumLevel = LogLevel.Information;
}
```
### 4. Update `WriteLog()` to skip below minimum
Add a level parameter and check at the start:
```csharp
private void WriteLog(LogLevel level, string levelLabel, string message, params object[] args)
{
if (level < MinimumLevel)
return;
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
var formattedMessage = args.Length > 0 ? String.Format(message, args) : message;
var category = !String.IsNullOrEmpty(_categoryName) ? String.Format("[{0}] ", _categoryName) : "";
Console.WriteLine(String.Format("{0} [{1}] {2}{3}", timestamp, levelLabel, category, formattedMessage));
}
```
### 5. Update each public method to pass its level
```csharp
public void LogDebug(string message, params object[] args)
{
WriteLog(LogLevel.Debug, "DEBUG", message, args);
}
public void LogInformation(string message, params object[] args)
{
WriteLog(LogLevel.Information, "INFO", message, args);
}
public void LogWarning(string message, params object[] args)
{
WriteLog(LogLevel.Warning, "WARN", message, args);
}
public void LogError(string message, params object[] args)
{
WriteLog(LogLevel.Error, "ERROR", message, args);
}
public void LogCritical(string message, params object[] args)
{
WriteLog(LogLevel.Critical, "CRITICAL", message, args);
}
```
---
## Acceptance Criteria
- [ ] `MinimumLevel = LogLevel.Warning` suppresses `LogDebug` and `LogInformation` calls
- [ ] `LogWarning`, `LogError`, `LogCritical` still write when `MinimumLevel = LogLevel.Warning`
- [ ] Default `MinimumLevel` is `Information` (backward compatible)
- [ ] `verify-build.bat` passes
- [ ] All existing tests pass (no test should be checking console output for Debug messages)