diff --git a/Snowflake.Data.Tests/SFBaseTest.cs b/Snowflake.Data.Tests/SFBaseTest.cs index 6153b337e..c05b042b9 100755 --- a/Snowflake.Data.Tests/SFBaseTest.cs +++ b/Snowflake.Data.Tests/SFBaseTest.cs @@ -195,15 +195,6 @@ public void Setup() var logRepository = log4net.LogManager.GetRepository(Assembly.GetEntryAssembly()); log4net.Config.XmlConfigurator.Configure(logRepository, new FileInfo("App.config")); #endif - ILoggerFactory factory = LoggerFactory.Create( - builder => builder - .AddLog4Net() - .SetMinimumLevel(LogLevel.Debug)); - - var logger = factory.CreateLogger("SFBaseTest"); - SFLoggerFactory.SetCustomLogger(logger); - SFLoggerFactory.EnableLogger(); - var cloud = Environment.GetEnvironmentVariable("snowflake_cloud_env"); Assert.IsTrue(cloud == null || cloud == "AWS" || cloud == "AZURE" || cloud == "GCP", "{0} is not supported. Specify AWS, AZURE or GCP as cloud environment", cloud); diff --git a/Snowflake.Data.Tests/UnitTests/Logger/SFSimpleLoggerTest.cs b/Snowflake.Data.Tests/UnitTests/Logger/SFSimpleLoggerTest.cs new file mode 100644 index 000000000..7ed3f20d8 --- /dev/null +++ b/Snowflake.Data.Tests/UnitTests/Logger/SFSimpleLoggerTest.cs @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. + */ + +using NUnit.Framework; +using Snowflake.Data.Configuration; +using Snowflake.Data.Log; + +namespace Snowflake.Data.Tests.UnitTests +{ + [TestFixture, NonParallelizable] + class SFSimpleLoggerTest + { + SFLogger _logger; + + [OneTimeSetUp] + public static void BeforeTest() + { + // Log level defaults to Warn on net6.0 builds in github actions + // Set the root level to Debug + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Debug, "STDOUT"); + } + + [OneTimeTearDown] + public static void AfterAll() + { + EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, "STDOUT"); + } + + [TearDown] + public void AfterTest() + { + // Return to default setting + SFLoggerFactory.EnableSimpleLogger(); + } + + [Test] + public void TestUsingSimpleLogger() + { + SFLoggerFactory.EnableSimpleLogger(); + _logger = SFLoggerFactory.GetSimpleLogger(); + Assert.IsInstanceOf(_logger); + } + + [Test] + public void TestSettingCustomLogger() + { + SFLoggerFactory.DisableSimpleLogger(); + _logger = SFLoggerFactory.GetSimpleLogger(); + Assert.IsInstanceOf(_logger); + } + + [Test] + public void TestIsDebugEnabled( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + + Assert.AreEqual(isEnabled, _logger.IsDebugEnabled()); + } + + [Test] + public void TestIsInfoEnabled( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + + Assert.AreEqual(isEnabled, _logger.IsInfoEnabled()); + } + + [Test] + public void TestIsWarnEnabled( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + + Assert.AreEqual(isEnabled, _logger.IsWarnEnabled()); + } + + [Test] + public void TestIsErrorEnabled( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + + Assert.AreEqual(isEnabled, _logger.IsErrorEnabled()); + } + + [Test] + public void TestIsFatalEnabled( + [Values(false, true)] bool isEnabled) + { + _logger = GetLogger(isEnabled); + + Assert.AreEqual(isEnabled, _logger.IsFatalEnabled()); + } + + private SFLogger GetLogger(bool isEnabled) + { + if (isEnabled) + { + SFLoggerFactory.EnableSimpleLogger(); + } + else + { + SFLoggerFactory.DisableSimpleLogger(); + } + + return SFLoggerFactory.GetSimpleLogger(); + } + } +} diff --git a/Snowflake.Data/Logger/SFLoggerFactory.cs b/Snowflake.Data/Logger/SFLoggerFactory.cs index 0107b4635..8fe71b486 100755 --- a/Snowflake.Data/Logger/SFLoggerFactory.cs +++ b/Snowflake.Data/Logger/SFLoggerFactory.cs @@ -11,12 +11,26 @@ public class SFLoggerFactory { private static bool isLoggerEnabled = false; - private static ILogger logger = null; + private static bool isSimpleLoggerEnabled = true; + + private static SFLogger simpleLogger = null; + + private static ILogger customLogger = null; private SFLoggerFactory() { } + public static void DisableSimpleLogger() + { + isSimpleLoggerEnabled = false; + } + + public static void EnableSimpleLogger() + { + isSimpleLoggerEnabled = true; + } + public static void DisableLogger() { isLoggerEnabled = false; @@ -29,18 +43,18 @@ public static void EnableLogger() public static void UseDefaultLogger() { - logger = null; + customLogger = null; } public static void SetCustomLogger(ILogger customLogger) - { - logger = customLogger; + { + SFLoggerFactory.customLogger = customLogger; } internal static SFLogger GetSimpleLogger() { // If true, return the default/specified logger - if (isLoggerEnabled) + if (isSimpleLoggerEnabled) { ILog loggerL = LogManager.GetLogger(typeof(T)); return new Log4NetImpl(loggerL); @@ -58,7 +72,7 @@ internal static ILogger GetLogger() if (isLoggerEnabled) { // If no logger specified, use the default logger: Microsoft's console logger - if (logger == null) + if (customLogger == null) { ILoggerFactory factory = LoggerFactory.Create( builder => builder @@ -68,7 +82,7 @@ internal static ILogger GetLogger() return factory.CreateLogger(); } - return logger; + return customLogger; } // Else, return the empty logger implementation which outputs nothing else