Enable debug logging from single environment variable or static field.

This commit is contained in:
benbierens 2023-11-08 09:24:39 +01:00
parent 073f0d86a1
commit 90070de028
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
12 changed files with 21 additions and 37 deletions

View File

@ -13,16 +13,21 @@ namespace Logging
public abstract class BaseLog : ILog
{
public static bool EnableDebugLogging { get; set; } = false;
private readonly NumberSource subfileNumberSource = new NumberSource(0);
private readonly bool debug;
private readonly List<BaseLogStringReplacement> replacements = new List<BaseLogStringReplacement>();
private LogFile? logFile;
protected BaseLog(bool debug)
public BaseLog()
{
this.debug = debug;
IsDebug =
EnableDebugLogging ||
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("LOGDEBUG")) ||
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("DEBUGLOG"));
}
protected bool IsDebug { get; private set; }
protected abstract string GetFullName();
public LogFile LogFile
@ -41,7 +46,7 @@ namespace Logging
public void Debug(string message = "", int skipFrames = 0)
{
if (debug)
if (IsDebug)
{
var callerName = DebugStack.GetCallerName(skipFrames);
Log($"(debug)({callerName}) {message}");
@ -72,7 +77,7 @@ namespace Logging
private string ApplyReplacements(string str)
{
if (debug) return str;
if (IsDebug) return str;
foreach (var replacement in replacements)
{
str = replacement.Apply(str);

View File

@ -2,10 +2,6 @@
{
public class ConsoleLog : BaseLog
{
public ConsoleLog() : base(false)
{
}
protected override string GetFullName()
{
return "CONSOLE";

View File

@ -2,13 +2,11 @@
{
public class LogConfig
{
public LogConfig(string logRoot, bool debugEnabled)
public LogConfig(string logRoot)
{
LogRoot = logRoot;
DebugEnabled = debugEnabled;
}
public string LogRoot { get; }
public bool DebugEnabled { get; }
}
}

View File

@ -2,10 +2,6 @@
{
public class NullLog : BaseLog
{
public NullLog() : base(false)
{
}
public string FullFilename { get; set; } = "NULL";
protected override string GetFullName()
@ -15,11 +11,12 @@
public override void Log(string message)
{
if (IsDebug) base.Log(message);
}
public override void Error(string message)
{
Console.WriteLine("Error: " + message);
base.Error(message);
}
public override void AddStringReplace(string from, string to)

View File

@ -18,7 +18,7 @@ namespace CodexContractsPlugin
{
var config = startupConfig.Get<CodexContractsContainerConfig>();
var address = config.GethNode.StartResult.Container.GetAddress(new ConsoleLog(), GethContainerRecipe.HttpPortTag);
var address = config.GethNode.StartResult.Container.GetAddress(new NullLog(), GethContainerRecipe.HttpPortTag);
AddEnvVar("DISTTEST_NETWORK_URL", address.ToString());
AddEnvVar("HARDHAT_NETWORK", "codexdisttestnetwork");

View File

@ -21,7 +21,7 @@ namespace ContinuousTests
public void Run()
{
var logConfig = new LogConfig(config.LogPath, false);
var logConfig = new LogConfig(config.LogPath);
var startTime = DateTime.UtcNow;
var overviewLog = new LogSplitter(

View File

@ -31,7 +31,7 @@ namespace ContinuousTests
this.handle = handle;
this.cancelToken = cancelToken;
testName = handle.Test.GetType().Name;
fixtureLog = new FixtureLog(new LogConfig(config.LogPath, false), DateTime.UtcNow, testName);
fixtureLog = new FixtureLog(new LogConfig(config.LogPath), DateTime.UtcNow, testName);
entryPoint = entryPointFactory.CreateEntryPoint(config.KubeConfigFile, config.DataPath, config.CodexDeployment.Metadata.KubeNamespace, fixtureLog);
ApplyLogReplacements(fixtureLog, startupChecker);

View File

@ -23,7 +23,7 @@ namespace ContinuousTests
public void Check()
{
var log = new FixtureLog(new LogConfig(config.LogPath, false), DateTime.UtcNow, "StartupChecks");
var log = new FixtureLog(new LogConfig(config.LogPath), DateTime.UtcNow, "StartupChecks");
log.Log("Starting continuous test run...");
IncludeDeploymentConfiguration(log);
log.Log("Checking configuration...");

View File

@ -7,22 +7,19 @@ namespace DistTestCore
{
private readonly string? kubeConfigFile;
private readonly string logPath;
private readonly bool logDebug;
private readonly string dataFilesPath;
public Configuration()
{
kubeConfigFile = GetNullableEnvVarOrDefault("KUBECONFIG", null);
logPath = GetEnvVarOrDefault("LOGPATH", "CodexTestLogs");
logDebug = GetEnvVarOrDefault("LOGDEBUG", "false").ToLowerInvariant() == "true";
dataFilesPath = GetEnvVarOrDefault("DATAFILEPATH", "TestDataFiles");
}
public Configuration(string? kubeConfigFile, string logPath, bool logDebug, string dataFilesPath)
public Configuration(string? kubeConfigFile, string logPath, string dataFilesPath)
{
this.kubeConfigFile = kubeConfigFile;
this.logPath = logPath;
this.logDebug = logDebug;
this.dataFilesPath = dataFilesPath;
}
@ -48,7 +45,7 @@ namespace DistTestCore
public Logging.LogConfig GetLogConfig()
{
return new Logging.LogConfig(logPath, debugEnabled: logDebug);
return new Logging.LogConfig(logPath);
}
public string GetFileManagerFolder()

View File

@ -6,11 +6,6 @@ namespace DistTestCore.Logs
{
private bool hasFailed;
public BaseTestLog(bool debug)
: base(debug)
{
}
public void WriteLogTag()
{
var runId = NameUtils.GetRunId();

View File

@ -5,18 +5,15 @@ namespace DistTestCore.Logs
public class FixtureLog : BaseTestLog
{
private readonly string fullName;
private readonly LogConfig config;
public FixtureLog(LogConfig config, DateTime start, string name = "")
: base(config.DebugEnabled)
{
fullName = NameUtils.GetFixtureFullName(config, start, name);
this.config = config;
}
public TestLog CreateTestLog(string name = "")
{
return new TestLog(fullName, config.DebugEnabled, name);
return new TestLog(fullName, name);
}
public void DeleteFolder()

View File

@ -5,8 +5,7 @@
private readonly string methodName;
private readonly string fullName;
public TestLog(string folder, bool debug, string name = "")
: base(debug)
public TestLog(string folder, string name = "")
{
methodName = NameUtils.GetTestMethodName(name);
fullName = Path.Combine(folder, methodName);