Skip to content

Commit

Permalink
Merge pull request #38 from configcat/improved-log-messages
Browse files Browse the repository at this point in the history
improves logging based the latest onboarding tests
  • Loading branch information
laliconfigcat authored Sep 6, 2022
2 parents 27563df + 9a397f8 commit 0e8e6e9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/ConfigCatClient.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30907.101
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigCatClient", "ConfigCatClient\ConfigCatClient.csproj", "{3046A17C-28AC-49A7-8B7B-E618FB5992F0}"
EndProject
Expand Down
16 changes: 5 additions & 11 deletions src/ConfigCatClient/Evaluate/EvaluateLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ public override string ToString()
{
var result = new StringBuilder();

result.AppendLine($" Evaluate '{KeyName}'");

result.AppendLine($" VariationId: {this.VariationId ?? "null"}");

result.AppendLine($" User object: {this.User.Serialize()}");

result.AppendLine($"Evaluating '{KeyName}'");
foreach (var o in this.Operations)
{
result.AppendLine(" " + o);
result.AppendLine(" " + o);
}

result.AppendLine($" Returning: {this.ReturnValue}");
result.Append($" Returning '{this.ReturnValue}' (VariationId: '{this.VariationId ?? "null"}').");

return result.ToString();
}
Expand All @@ -61,8 +55,8 @@ public static string FormatComparator(Comparator comparator)
Comparator.NumberGreaterThanEqual => ">=",
Comparator.NumberEqual => "=",
Comparator.NumberNotEqual => "!=",
Comparator.SensitiveOneOf => "IS ONE OF (Sensitive)",
Comparator.SensitiveNotOneOf => "IS NOT ONE OF (Sensitive)",
Comparator.SensitiveOneOf => "IS ONE OF (hashed)",
Comparator.SensitiveNotOneOf => "IS NOT ONE OF (hashed)",
_ => comparator.ToString()
};
}
Expand Down
43 changes: 25 additions & 18 deletions src/ConfigCatClient/Evaluate/RolloutEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public T Evaluate<T>(IDictionary<string, Setting> settings, string key, T defaul
{
if (settings.Count == 0)
{
this.log.Error($"Config JSON is not present. Returning defaultValue: [{defaultValue}].");
this.log.Error($"Config JSON is not present. Returning the defaultValue defined in the app source code: '{defaultValue}'.");
return defaultValue;
}

Expand All @@ -40,7 +40,7 @@ public object Evaluate(IDictionary<string, Setting> settings, string key, object
{
if (settings.Count == 0)
{
this.log.Error($"Config JSON is not present. Returning defaultValue: [{defaultValue}].");
this.log.Error($"Config JSON is not present. Returning the defaultValue defined in the app source code: '{defaultValue}'.");
return defaultValue;
}

Expand All @@ -56,7 +56,7 @@ public string EvaluateVariationId(IDictionary<string, Setting> settings, string
{
if (settings.Count == 0)
{
this.log.Error($"Config JSON is not present. Returning defaultVariationId: [{defaultVariationId}].");
this.log.Error($"Config JSON is not present. Returning defaultVariationId: '{defaultVariationId}'.");
return defaultVariationId;
}

Expand All @@ -69,7 +69,7 @@ private EvaluateResult EvaluateLogic(IDictionary<string, Setting> settings, stri
if (!settings.TryGetValue(key, out var setting))
{
var keys = string.Join(",", settings.Keys.Select(s => $"'{s}'").ToArray());
this.log.Error($"Evaluating '{key}' failed. Returning default value: '{logDefaultValue}'. Here are the available keys: {keys}.");
this.log.Error($"Evaluating '{key}' failed (key not found in ConfigCat). Returning the defaultValue that you defined in the source code: '{logDefaultValue}'. Here are the available keys: {keys}.");
return null;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ private EvaluateResult EvaluateLogic(IDictionary<string, Setting> settings, stri
}
else if (setting.RolloutRules.Any() || setting.RolloutPercentageItems.Any())
{
this.log.Warning($"Evaluating '{key}'. UserObject missing! You should pass a UserObject to GetValue() or GetValueAsync(), in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object");
this.log.Warning($"Cannot evaluate targeting rules and % options for '{key}' (UserObject missing). You should pass a UserObject to GetValue() or GetValueAsync() in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object");
}

// regular evaluate
Expand All @@ -129,7 +129,7 @@ private EvaluateResult EvaluateLogic(IDictionary<string, Setting> settings, stri
}
finally
{
this.log.Information(evaluateLog.ToString());
this.log.Information($"{evaluateLog}");
}
}

Expand All @@ -145,17 +145,22 @@ private static bool TryEvaluateVariations<T>(ICollection<RolloutPercentageItem>
var hashValue = hashCandidate.Hash().Substring(0, 7);

var hashScale = int.Parse(hashValue, NumberStyles.HexNumber) % 100;
evaluateLog.Log($"Applying the % option that matches the User's pseudo-random '{hashScale}' (this value is sticky and consistent across all SDKs):");

var bucket = 0;

foreach (var variation in rolloutPercentageItems.OrderBy(o => o.Order))
{
bucket += variation.Percentage;

if (hashScale >= bucket) continue;
if (hashScale >= bucket)
{
evaluateLog.Log($" - % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => no match");
continue;
}
result.Value = variation.Value;
result.VariationId = variation.VariationId;
evaluateLog.Log($"Evaluating % options, '{key}' evaluated to '{variation.Value}'.");
evaluateLog.Log($" - % option: [IF {bucket} > {hashScale} THEN '{variation.Value}'] => MATCH, applying % option");
return true;
}
}
Expand All @@ -169,24 +174,26 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use

if (rules is { Count: > 0 })
{
logger.Log($"Applying the first targeting rule that matches the User '{user.Serialize()}':");
foreach (var rule in rules.OrderBy(o => o.Order))
{
result.Value = rule.Value;
result.VariationId = rule.VariationId;

string l = $" - rule: [IF User.{rule.ComparisonAttribute} {EvaluateLogger<T>.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' THEN {rule.Value}] => ";
if (!user.AllAttributes.ContainsKey(rule.ComparisonAttribute))
{
logger.Log(l + "no match");
continue;
}

var comparisonAttributeValue = user.AllAttributes[rule.ComparisonAttribute];
if (string.IsNullOrEmpty(comparisonAttributeValue))
{
logger.Log(l + "no match");
continue;
}

string l = $"Evaluate rule: '{comparisonAttributeValue}' {EvaluateLogger<T>.FormatComparator(rule.Comparator)} '{rule.ComparisonValue}' => ";

switch (rule.Comparator)
{
case Comparator.In:
Expand All @@ -196,7 +203,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use
.Select(t => t.Trim())
.Contains(comparisonAttributeValue))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -212,7 +219,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use
.Select(t => t.Trim())
.Contains(comparisonAttributeValue))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -224,7 +231,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use

if (comparisonAttributeValue.Contains(rule.ComparisonValue))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -236,7 +243,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use

if (!comparisonAttributeValue.Contains(rule.ComparisonValue))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -253,7 +260,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use

if (EvaluateSemVer(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -271,7 +278,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use

if (EvaluateNumber(comparisonAttributeValue, rule.ComparisonValue, rule.Comparator))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -285,7 +292,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use
.Select(t => t.Trim())
.Contains(comparisonAttributeValue.Hash()))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand All @@ -299,7 +306,7 @@ private static bool TryEvaluateRules<T>(ICollection<RolloutRule> rules, User use
.Select(t => t.Trim())
.Contains(comparisonAttributeValue.Hash()))
{
logger.Log(l + "match");
logger.Log(l + "MATCH, applying rule");

return true;
}
Expand Down
13 changes: 7 additions & 6 deletions src/ConfigCatClient/Logging/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,31 @@ public ConsoleLogger(LogLevel logLevel)
/// <inheritdoc />
public void Debug(string message)
{
Console.WriteLine(FormatMessage(LogLevel.Debug, message));
PrintMessage(LogLevel.Debug, message);
}

/// <inheritdoc />
public void Error(string message)
{
Console.WriteLine(FormatMessage(LogLevel.Error, message));
PrintMessage(LogLevel.Error, message);
}

/// <inheritdoc />
public void Information(string message)
{
Console.WriteLine(FormatMessage(LogLevel.Info, message));
PrintMessage(LogLevel.Info, message);
}

/// <inheritdoc />
public void Warning(string message)
{
Console.WriteLine(FormatMessage(LogLevel.Warning, message));
PrintMessage(LogLevel.Warning, message);
}

private string FormatMessage(LogLevel logLevel, string message)
private void PrintMessage(LogLevel logLevel, string message)
{
return $"ConfigCat - {logLevel} - {message}";
string logLevelPadded = logLevel.ToString().ToUpper().PadRight(7);
Console.WriteLine($"ConfigCat.{logLevelPadded} {message}");
}
}
}
10 changes: 5 additions & 5 deletions src/ConfigCatClient/Logging/LogLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
public enum LogLevel
{
/// <summary>
/// No tracing and any debugging messages.
/// No messages are logged.
/// </summary>
Off = 0,
/// <summary>
/// Error messages.
/// Error messages are logged. All other messages are discarded.
/// </summary>
Error = 1,
/// <summary>
/// Error and warning messages.
/// Warning and Error messages should be logged. Information and Debug messages are discarded.
/// </summary>
Warning = 2,
/// <summary>
/// Information, Error and Warning messages.
/// Information, Warning and Error are logged. Debug messages are discarded.
/// </summary>
Info = 3,
/// <summary>
/// All messages
/// All messages should be logged.
/// </summary>
Debug = 4
}
Expand Down

0 comments on commit 0e8e6e9

Please sign in to comment.