From 90070de028e3ccda7fc8ce00cbbf577f8b0fc161 Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 8 Nov 2023 09:24:39 +0100 Subject: [PATCH] Enable debug logging from single environment variable or static field. --- Framework/Logging/BaseLog.cs | 15 ++++++++++----- Framework/Logging/ConsoleLog.cs | 4 ---- Framework/Logging/LogConfig.cs | 4 +--- Framework/Logging/NullLog.cs | 7 ++----- .../CodexContractsContainerRecipe.cs | 2 +- .../CodexContinuousTests/ContinuousTestRunner.cs | 2 +- Tests/CodexContinuousTests/SingleTestRun.cs | 2 +- Tests/CodexContinuousTests/StartupChecker.cs | 2 +- Tests/DistTestCore/Configuration.cs | 7 ++----- Tests/DistTestCore/Logs/BaseTestLog.cs | 5 ----- Tests/DistTestCore/Logs/FixtureLog.cs | 5 +---- Tests/DistTestCore/Logs/TestLog.cs | 3 +-- 12 files changed, 21 insertions(+), 37 deletions(-) diff --git a/Framework/Logging/BaseLog.cs b/Framework/Logging/BaseLog.cs index d16a1c9..bb5df83 100644 --- a/Framework/Logging/BaseLog.cs +++ b/Framework/Logging/BaseLog.cs @@ -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 replacements = new List(); 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); diff --git a/Framework/Logging/ConsoleLog.cs b/Framework/Logging/ConsoleLog.cs index 61e6115..61da5c8 100644 --- a/Framework/Logging/ConsoleLog.cs +++ b/Framework/Logging/ConsoleLog.cs @@ -2,10 +2,6 @@ { public class ConsoleLog : BaseLog { - public ConsoleLog() : base(false) - { - } - protected override string GetFullName() { return "CONSOLE"; diff --git a/Framework/Logging/LogConfig.cs b/Framework/Logging/LogConfig.cs index eb91fdb..b7bc937 100644 --- a/Framework/Logging/LogConfig.cs +++ b/Framework/Logging/LogConfig.cs @@ -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; } } } diff --git a/Framework/Logging/NullLog.cs b/Framework/Logging/NullLog.cs index bbd3214..fb7a70e 100644 --- a/Framework/Logging/NullLog.cs +++ b/Framework/Logging/NullLog.cs @@ -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) diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsContainerRecipe.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsContainerRecipe.cs index 26d6461..1dffe3f 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsContainerRecipe.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsContainerRecipe.cs @@ -18,7 +18,7 @@ namespace CodexContractsPlugin { var config = startupConfig.Get(); - 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"); diff --git a/Tests/CodexContinuousTests/ContinuousTestRunner.cs b/Tests/CodexContinuousTests/ContinuousTestRunner.cs index 8461f7f..7e4ffd9 100644 --- a/Tests/CodexContinuousTests/ContinuousTestRunner.cs +++ b/Tests/CodexContinuousTests/ContinuousTestRunner.cs @@ -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( diff --git a/Tests/CodexContinuousTests/SingleTestRun.cs b/Tests/CodexContinuousTests/SingleTestRun.cs index 156cbb9..f86e502 100644 --- a/Tests/CodexContinuousTests/SingleTestRun.cs +++ b/Tests/CodexContinuousTests/SingleTestRun.cs @@ -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); diff --git a/Tests/CodexContinuousTests/StartupChecker.cs b/Tests/CodexContinuousTests/StartupChecker.cs index 94e318c..b232265 100644 --- a/Tests/CodexContinuousTests/StartupChecker.cs +++ b/Tests/CodexContinuousTests/StartupChecker.cs @@ -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..."); diff --git a/Tests/DistTestCore/Configuration.cs b/Tests/DistTestCore/Configuration.cs index 35b9823..40f11de 100644 --- a/Tests/DistTestCore/Configuration.cs +++ b/Tests/DistTestCore/Configuration.cs @@ -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() diff --git a/Tests/DistTestCore/Logs/BaseTestLog.cs b/Tests/DistTestCore/Logs/BaseTestLog.cs index 99cd0f1..e1f6d3d 100644 --- a/Tests/DistTestCore/Logs/BaseTestLog.cs +++ b/Tests/DistTestCore/Logs/BaseTestLog.cs @@ -6,11 +6,6 @@ namespace DistTestCore.Logs { private bool hasFailed; - public BaseTestLog(bool debug) - : base(debug) - { - } - public void WriteLogTag() { var runId = NameUtils.GetRunId(); diff --git a/Tests/DistTestCore/Logs/FixtureLog.cs b/Tests/DistTestCore/Logs/FixtureLog.cs index c32ce87..8da2a48 100644 --- a/Tests/DistTestCore/Logs/FixtureLog.cs +++ b/Tests/DistTestCore/Logs/FixtureLog.cs @@ -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() diff --git a/Tests/DistTestCore/Logs/TestLog.cs b/Tests/DistTestCore/Logs/TestLog.cs index 0f8831b..6e23413 100644 --- a/Tests/DistTestCore/Logs/TestLog.cs +++ b/Tests/DistTestCore/Logs/TestLog.cs @@ -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);