Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for the UserLogDefaultLogLevel to be set independent of system logs #4318

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ internal class StartHostAction : BaseAction

public string? HostRuntime { get; set; }

public string UserLogLevel { get; set; }

public StartHostAction(ISecretsManager secretsManager, IProcessManager processManager)
{
_secretsManager = secretsManager;
Expand Down Expand Up @@ -178,6 +180,11 @@ public override ICommandLineParserResult ParseArgs(string[] args)
.WithDescription($"If provided, determines which version of the host to start. Allowed values are '{DotnetConstants.InProc6HostRuntime}', '{DotnetConstants.InProc8HostRuntime}', and 'default' (which runs the out-of-process host).")
.Callback(startHostFromRuntime => HostRuntime = startHostFromRuntime);

Parser
.Setup<string>("userLogLevel")
.WithDescription($"If provided, determines the log level of user logs. Allowed values are '{LogLevel.Trace}', '{LogLevel.Debug}', '{LogLevel.Information}', '{LogLevel.Warning}', '{LogLevel.Error}', '{LogLevel.Critical}' and '{LogLevel.None}'. The default is Information.")
.Callback(userLogLevel => UserLogLevel = userLogLevel);

var parserResult = base.ParseArgs(args);
bool verboseLoggingArgExists = parserResult.UnMatchedOptions.Any(o => o.LongName.Equals("verbose", StringComparison.OrdinalIgnoreCase));
// Input args do not contain --verbose flag
Expand All @@ -190,7 +197,7 @@ public override ICommandLineParserResult ParseArgs(string[] args)

private async Task<IWebHost> BuildWebHost(ScriptApplicationHostOptions hostOptions, Uri listenAddress, Uri baseAddress, X509Certificate2 certificate)
{
LoggingFilterHelper loggingFilterHelper = new LoggingFilterHelper(_hostJsonConfig, VerboseLogging);
LoggingFilterHelper loggingFilterHelper = new LoggingFilterHelper(_hostJsonConfig, VerboseLogging, UserLogLevel);
if (GlobalCoreToolsSettings.CurrentWorkerRuntime == WorkerRuntime.dotnet ||
GlobalCoreToolsSettings.CurrentWorkerRuntime == WorkerRuntime.dotnetIsolated)
{
Expand Down Expand Up @@ -400,7 +407,10 @@ public override async Task RunAsync()
{
await PreRunConditions();
var isVerbose = VerboseLogging.HasValue && VerboseLogging.Value;


//set --userLogLevel flag
ConfigureUserLogLevelFromFlagOrSettings();

// Return if running is delegated to another version of Core Tools
if (await TryHandleInProcDotNetLaunchAsync())
{
Expand Down Expand Up @@ -831,5 +841,37 @@ private void EnsureWorkerRuntimeIsSet()
// Update local.settings.json
WorkerRuntimeLanguageHelper.SetWorkerRuntime(_secretsManager, GlobalCoreToolsSettings.CurrentWorkerRuntime.ToString());
}

private void ConfigureUserLogLevelFromFlagOrSettings()
{
if (!string.IsNullOrEmpty(UserLogLevel))
{
if (IsValidUserLogLevel(UserLogLevel))
{
Environment.SetEnvironmentVariable("AzureFunctionsJobHost__Logging__LogLevel__Function", UserLogLevel);
}
}
else
{
UserLogLevel = GetUserLogLevelFromLocalSettings();
}
}

private bool IsValidUserLogLevel(string UserLogLevel)
{
if (LoggingFilterHelper.ValidUserLogLevels.Contains(UserLogLevel, StringComparer.OrdinalIgnoreCase) == false)
{
throw new CliException($"The userLogLevel value provided, '{UserLogLevel}', is invalid. Valid values are '{string.Join("', '", LoggingFilterHelper.ValidUserLogLevels)}'. The default is Information.");
}
return true;
}

private string GetUserLogLevelFromLocalSettings()
{
var UserLogLevel = Environment.GetEnvironmentVariable(Constants.FunctionsLoggingLogLevel)
?? _secretsManager.GetSecrets().FirstOrDefault(s => s.Key.Equals(Constants.FunctionsLoggingLogLevel, StringComparison.OrdinalIgnoreCase)).Value;

return UserLogLevel;
}
}
}
1 change: 1 addition & 0 deletions src/Azure.Functions.Cli/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ internal static class Constants
public const string AzureDevSessionsRemoteHostName = "AzureDevSessionsRemoteHostName";
public const string AzureDevSessionsPortSuffixPlaceholder = "<port>";
public const string GitHubReleaseApiUrl = "https://api.github.com/repos/Azure/azure-functions-core-tools/releases/latest";
public const string FunctionsLoggingLogLevel = "AzureFunctionsJobHost__Logging__LogLevel__Function";

// Sample format https://n12abc3t-<port>.asse.devtunnels.ms/

Expand Down
1 change: 1 addition & 0 deletions src/Azure.Functions.Cli/Common/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal static class Utilities
{
public const string LogLevelSection = "loglevel";
public const string LogLevelDefaultSection = "Default";
public const string LogLevelFunctionSection = "Function";

internal static void PrintLogo()
{
Expand Down
22 changes: 18 additions & 4 deletions src/Azure.Functions.Cli/Diagnostics/LoggingFilterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class LoggingFilterHelper
public const string Ci_Continuous_Integration = "CONTINUOUS_INTEGRATION"; // Travis CI, Cirrus CI
public const string Ci_Build_Number = "BUILD_NUMBER"; // Travis CI, Cirrus CI
public const string Ci_Run_Id = "RUN_ID"; // TaskCluster, dsari
public static readonly string[] ValidUserLogLevels = ["Trace", "Debug", "Information", "Warning", "Error", "Critical", "None"];

public LoggingFilterHelper(IConfigurationRoot hostJsonConfig, bool? verboseLogging)
public LoggingFilterHelper(IConfigurationRoot hostJsonConfig, bool? verboseLogging, string userLogLevel = null)
{
VerboseLogging = verboseLogging.HasValue && verboseLogging.Value;

Expand All @@ -31,10 +32,22 @@ public LoggingFilterHelper(IConfigurationRoot hostJsonConfig, bool? verboseLoggi
{
SystemLogDefaultLogLevel = LogLevel.Information;
}
if (Utilities.LogLevelExists(hostJsonConfig, Utilities.LogLevelDefaultSection, out LogLevel logLevel))
if (Utilities.LogLevelExists(hostJsonConfig, Utilities.LogLevelDefaultSection, out LogLevel defaultLogLevel))
{
SystemLogDefaultLogLevel = logLevel;
UserLogDefaultLogLevel = logLevel;
SystemLogDefaultLogLevel = defaultLogLevel;
UserLogDefaultLogLevel = defaultLogLevel;
}

// set user log value to Function
if ( Utilities.LogLevelExists(hostJsonConfig, Utilities.LogLevelFunctionSection, out LogLevel functionLogLevel))
{
UserLogDefaultLogLevel = functionLogLevel;
}

// set user log value to --userLoglevel Flag
if (!string.IsNullOrEmpty(userLogLevel) && Enum.TryParse(userLogLevel, true, out LogLevel UserLogLevel))
{
UserLogDefaultLogLevel = UserLogLevel;
}
}

Expand Down Expand Up @@ -68,5 +81,6 @@ internal bool IsCiEnvironment(bool verboseLoggingArgExists)
}
return false;
}

}
}
Loading