-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathDynamicLogLevelProvider.cs
More file actions
102 lines (89 loc) · 3.9 KB
/
DynamicLogLevelProvider.cs
File metadata and controls
102 lines (89 loc) · 3.9 KB
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
using System;
using System.Collections.Generic;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Telemetry;
using Microsoft.Extensions.Logging;
namespace Azure.DataApiBuilder.Service.Telemetry
{
/// <summary>
/// Provides dynamic log level control with support for CLI override, runtime config, and MCP.
/// </summary>
public class DynamicLogLevelProvider : ILogLevelController
{
/// <summary>
/// Maps MCP log level strings to Microsoft.Extensions.Logging.LogLevel.
/// MCP levels: debug, info, notice, warning, error, critical, alert, emergency.
/// </summary>
private static readonly Dictionary<string, LogLevel> _mcpLevelMapping = new(StringComparer.OrdinalIgnoreCase)
{
["debug"] = LogLevel.Debug,
["info"] = LogLevel.Information,
["notice"] = LogLevel.Information, // MCP "notice" maps to Information (no direct equivalent)
["warning"] = LogLevel.Warning,
["error"] = LogLevel.Error,
["critical"] = LogLevel.Critical,
["alert"] = LogLevel.Critical, // MCP "alert" maps to Critical
["emergency"] = LogLevel.Critical // MCP "emergency" maps to Critical
};
public LogLevel CurrentLogLevel { get; private set; }
public bool IsCliOverridden { get; private set; }
public bool IsConfigOverridden { get; private set; }
public void SetInitialLogLevel(LogLevel logLevel = LogLevel.Error, bool isCliOverridden = false, bool isConfigOverridden = false)
{
CurrentLogLevel = logLevel;
IsCliOverridden = isCliOverridden;
IsConfigOverridden = isConfigOverridden;
}
public void UpdateFromRuntimeConfig(RuntimeConfig runtimeConfig)
{
// Only update if CLI didn't override
if (!IsCliOverridden)
{
CurrentLogLevel = runtimeConfig.GetConfiguredLogLevel();
// Track if config explicitly set a log level (not just using defaults)
IsConfigOverridden = !runtimeConfig.IsLogLevelNull();
}
}
/// <summary>
/// Updates the log level from an MCP logging/setLevel request.
/// Precedence (highest to lowest):
/// 1. CLI --LogLevel flag (IsCliOverridden = true)
/// 2. Config runtime.telemetry.log-level (IsConfigOverridden = true)
/// 3. MCP logging/setLevel
///
/// If CLI or Config overrode, this method accepts the request silently but does not change the level.
/// </summary>
/// <param name="mcpLevel">The MCP log level string (e.g., "debug", "info", "warning", "error").</param>
/// <returns>True if the level was changed; false if CLI/Config override prevented the change or level was invalid.</returns>
public bool UpdateFromMcp(string mcpLevel)
{
// If CLI overrode the log level, accept the request but don't change anything.
// This prevents MCP clients from getting errors, but CLI wins.
if (IsCliOverridden)
{
return false;
}
// If Config explicitly set the log level, accept the request but don't change anything.
// Config has second precedence after CLI.
if (IsConfigOverridden)
{
return false;
}
if (string.IsNullOrWhiteSpace(mcpLevel))
{
return false;
}
if (_mcpLevelMapping.TryGetValue(mcpLevel, out LogLevel logLevel))
{
CurrentLogLevel = logLevel;
return true;
}
// Unknown level - don't change, but don't fail either
return false;
}
public bool ShouldLog(LogLevel logLevel)
{
return logLevel >= CurrentLogLevel;
}
}
}