Skip to content

Commit

Permalink
Refactor original logger to simple logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Nov 21, 2024
1 parent 1036d56 commit 3f3a15c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 16 deletions.
9 changes: 0 additions & 9 deletions Snowflake.Data.Tests/SFBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
112 changes: 112 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Logger/SFSimpleLoggerTest.cs
Original file line number Diff line number Diff line change
@@ -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<SFLoggerTest>();
Assert.IsInstanceOf<Log4NetImpl>(_logger);
}

[Test]
public void TestSettingCustomLogger()
{
SFLoggerFactory.DisableSimpleLogger();
_logger = SFLoggerFactory.GetSimpleLogger<SFLoggerTest>();
Assert.IsInstanceOf<SFLoggerEmptySimpleImpl>(_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<SFLoggerTest>();
}
}
}
28 changes: 21 additions & 7 deletions Snowflake.Data/Logger/SFLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net7.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net8.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net6.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net7.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on MAC (net8.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net6.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net7.0, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net8.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net462, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net471, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net472, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net48, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, AZURE)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Windows (net481, AWS)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

Check warning on line 16 in Snowflake.Data/Logger/SFLoggerFactory.cs

View workflow job for this annotation

GitHub Actions / Tests on Linux (net6.0, GCP)

The field 'SFLoggerFactory.simpleLogger' is assigned but its value is never used

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;
Expand All @@ -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<T>()
{
// If true, return the default/specified logger
if (isLoggerEnabled)
if (isSimpleLoggerEnabled)
{
ILog loggerL = LogManager.GetLogger(typeof(T));
return new Log4NetImpl(loggerL);
Expand All @@ -58,7 +72,7 @@ internal static ILogger GetLogger<T>()
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
Expand All @@ -68,7 +82,7 @@ internal static ILogger GetLogger<T>()

return factory.CreateLogger<T>();
}
return logger;
return customLogger;
}
// Else, return the empty logger implementation which outputs nothing
else
Expand Down

0 comments on commit 3f3a15c

Please sign in to comment.