Skip to content

Commit b26abb2

Browse files
Updates logLevel to include plugins. Closes #167 (#413)
1 parent 8ba2896 commit b26abb2

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

dev-proxy/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
PluginEvents pluginEvents = new PluginEvents();
99
ILogger logger = new ConsoleLogger(ProxyCommandHandler.Configuration, pluginEvents);
10+
// set the log level if specified through args
11+
if (ProxyHost.LogLevel is not null)
12+
{
13+
logger.LogLevel = ProxyHost.LogLevel.Value;
14+
}
1015
IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration);
1116
ProxyHost proxyHost = new();
1217
RootCommand rootCommand = proxyHost.GetRootCommand(logger);

dev-proxy/ProxyHost.cs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class ProxyHost
1111
{
1212
private Option<int?> _portOption;
1313
private Option<string?> _ipAddressOption;
14-
private Option<LogLevel?> _logLevelOption;
14+
private static Option<LogLevel?>? _logLevelOption;
1515
private Option<bool?> _recordOption;
1616
private Option<IEnumerable<int>?> _watchPidsOption;
1717
private Option<IEnumerable<string>?> _watchProcessNamesOption;
@@ -87,8 +87,56 @@ public static string ConfigFile
8787
}
8888
}
8989

90-
public ProxyHost()
90+
private static bool _logLevelResolved = false;
91+
private static LogLevel? _logLevel;
92+
public static LogLevel? LogLevel
9193
{
94+
get
95+
{
96+
if (_logLevelResolved)
97+
{
98+
return _logLevel;
99+
}
100+
101+
if (_logLevelOption is null)
102+
{
103+
_logLevelOption = new Option<LogLevel?>(
104+
"--log-level",
105+
$"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}"
106+
)
107+
{
108+
ArgumentHelpName = "logLevel"
109+
};
110+
_logLevelOption.AddValidator(input => {
111+
if (!Enum.TryParse<LogLevel>(input.Tokens.First().Value, true, out var logLevel)) {
112+
input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}";
113+
}
114+
});
115+
}
116+
117+
var result = _logLevelOption.Parse(Environment.GetCommandLineArgs());
118+
// since we're parsing all args, and other options are not instantiated yet
119+
// we're getting here a bunch of other errors, so we only need to look for
120+
// errors related to the log level option
121+
var error = result.Errors.Where(e => e.SymbolResult?.Symbol == _logLevelOption).FirstOrDefault();
122+
if (error is not null)
123+
{
124+
// Logger is not available here yet so we need to fallback to Console
125+
var color = Console.ForegroundColor;
126+
Console.ForegroundColor = ConsoleColor.Red;
127+
Console.Error.WriteLine(error.Message);
128+
Console.ForegroundColor = color;
129+
Environment.Exit(1);
130+
}
131+
132+
_logLevel = result.GetValueForOption(_logLevelOption);
133+
_logLevelResolved = true;
134+
135+
return _logLevel;
136+
}
137+
}
138+
139+
public ProxyHost() {
92140
_portOption = new Option<int?>("--port", "The port for the proxy to listen on");
93141
_portOption.AddAlias("-p");
94142
_portOption.ArgumentHelpName = "port";
@@ -105,16 +153,6 @@ public ProxyHost()
105153
}
106154
});
107155

108-
_logLevelOption = new Option<LogLevel?>("--log-level", $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}");
109-
_logLevelOption.ArgumentHelpName = "logLevel";
110-
_logLevelOption.AddValidator(input =>
111-
{
112-
if (!Enum.TryParse<LogLevel>(input.Tokens.First().Value, true, out var logLevel))
113-
{
114-
input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}";
115-
}
116-
});
117-
118156
_recordOption = new Option<bool?>("--record", "Use this option to record all request logs");
119157

120158
_watchPidsOption = new Option<IEnumerable<int>?>("--watch-pids", "The IDs of processes to watch for requests");
@@ -145,7 +183,9 @@ public RootCommand GetRootCommand(ILogger logger)
145183
var command = new RootCommand {
146184
_portOption,
147185
_ipAddressOption,
148-
_logLevelOption,
186+
// _logLevelOption is set while initialize the Program
187+
// As such, it's always set here
188+
_logLevelOption!,
149189
_recordOption,
150190
_watchPidsOption,
151191
_watchProcessNamesOption,

0 commit comments

Comments
 (0)