Skip to content

Commit

Permalink
Updates logLevel to include plugins. Closes #167 (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored Dec 12, 2023
1 parent 8ba2896 commit b26abb2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
5 changes: 5 additions & 0 deletions dev-proxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

PluginEvents pluginEvents = new PluginEvents();
ILogger logger = new ConsoleLogger(ProxyCommandHandler.Configuration, pluginEvents);
// set the log level if specified through args
if (ProxyHost.LogLevel is not null)
{
logger.LogLevel = ProxyHost.LogLevel.Value;
}
IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration);
ProxyHost proxyHost = new();
RootCommand rootCommand = proxyHost.GetRootCommand(logger);
Expand Down
66 changes: 53 additions & 13 deletions dev-proxy/ProxyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class ProxyHost
{
private Option<int?> _portOption;
private Option<string?> _ipAddressOption;
private Option<LogLevel?> _logLevelOption;
private static Option<LogLevel?>? _logLevelOption;
private Option<bool?> _recordOption;
private Option<IEnumerable<int>?> _watchPidsOption;
private Option<IEnumerable<string>?> _watchProcessNamesOption;
Expand Down Expand Up @@ -87,8 +87,56 @@ public static string ConfigFile
}
}

public ProxyHost()
private static bool _logLevelResolved = false;
private static LogLevel? _logLevel;
public static LogLevel? LogLevel
{
get
{
if (_logLevelResolved)
{
return _logLevel;
}

if (_logLevelOption is null)
{
_logLevelOption = new Option<LogLevel?>(
"--log-level",
$"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}"
)
{
ArgumentHelpName = "logLevel"
};
_logLevelOption.AddValidator(input => {
if (!Enum.TryParse<LogLevel>(input.Tokens.First().Value, true, out var logLevel)) {
input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}";
}
});
}

var result = _logLevelOption.Parse(Environment.GetCommandLineArgs());
// since we're parsing all args, and other options are not instantiated yet
// we're getting here a bunch of other errors, so we only need to look for
// errors related to the log level option
var error = result.Errors.Where(e => e.SymbolResult?.Symbol == _logLevelOption).FirstOrDefault();
if (error is not null)
{
// Logger is not available here yet so we need to fallback to Console
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(error.Message);
Console.ForegroundColor = color;
Environment.Exit(1);
}

_logLevel = result.GetValueForOption(_logLevelOption);
_logLevelResolved = true;

return _logLevel;
}
}

public ProxyHost() {
_portOption = new Option<int?>("--port", "The port for the proxy to listen on");
_portOption.AddAlias("-p");
_portOption.ArgumentHelpName = "port";
Expand All @@ -105,16 +153,6 @@ public ProxyHost()
}
});

_logLevelOption = new Option<LogLevel?>("--log-level", $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}");
_logLevelOption.ArgumentHelpName = "logLevel";
_logLevelOption.AddValidator(input =>
{
if (!Enum.TryParse<LogLevel>(input.Tokens.First().Value, true, out var logLevel))
{
input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}";
}
});

_recordOption = new Option<bool?>("--record", "Use this option to record all request logs");

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

0 comments on commit b26abb2

Please sign in to comment.