Skip to content

Commit

Permalink
Refactor SFLoggerPair
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Dec 6, 2024
1 parent a7535dc commit 26a5d53
Show file tree
Hide file tree
Showing 53 changed files with 652 additions and 460 deletions.
24 changes: 12 additions & 12 deletions Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void TestThatChangesLogLevel()

// assert
Assert.IsFalse(logger.IsDebugEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
Assert.IsFalse(logger.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsErrorEnabled());
Assert.IsFalse(logger.IsFatalEnabled());

Expand All @@ -66,8 +66,8 @@ public void TestThatChangesLogLevel()

// assert
Assert.IsFalse(logger.IsDebugEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
Assert.IsFalse(logger.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsErrorEnabled());
Assert.IsTrue(logger.IsFatalEnabled());

Expand All @@ -76,8 +76,8 @@ public void TestThatChangesLogLevel()

// assert
Assert.IsFalse(logger.IsDebugEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
Assert.IsFalse(logger.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsTrue(logger.IsErrorEnabled());
Assert.IsTrue(logger.IsFatalEnabled());

Expand All @@ -86,8 +86,8 @@ public void TestThatChangesLogLevel()

// assert
Assert.IsFalse(logger.IsDebugEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsTrue(logger.IsWarningEnabled());
Assert.IsFalse(logger.IsInfoEnabled());
Assert.IsTrue(logger.IsWarnEnabled());
Assert.IsTrue(logger.IsErrorEnabled());
Assert.IsTrue(logger.IsFatalEnabled());

Expand All @@ -96,8 +96,8 @@ public void TestThatChangesLogLevel()

// assert
Assert.IsTrue(logger.IsDebugEnabled());
Assert.IsTrue(logger.IsInformationEnabled());
Assert.IsTrue(logger.IsWarningEnabled());
Assert.IsTrue(logger.IsInfoEnabled());
Assert.IsTrue(logger.IsWarnEnabled());
Assert.IsTrue(logger.IsErrorEnabled());
Assert.IsTrue(logger.IsFatalEnabled());
}
Expand All @@ -111,8 +111,8 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly()

// act
logger.Debug(DebugMessage);
logger.Information(InfoMessage);
logger.Warning(WarnMessage);
logger.Info(InfoMessage);
logger.Warn(WarnMessage);
logger.Error(ErrorMessage);
logger.Fatal(FatalMessage);

Expand Down
140 changes: 140 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Logger/SFLoggerPairTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

using NUnit.Framework;
using Snowflake.Data.Configuration;
using Snowflake.Data.Log;
using System;

namespace Snowflake.Data.Tests.UnitTests
{
[TestFixture, NonParallelizable]
class SFLoggerPairTest
{
SFLogger _loggerPair;

[OneTimeSetUp]
public static void BeforeAll()
{
// 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");
}

[SetUp]
public void BeforeTest()
{
_loggerPair = SFLoggerFactory.GetLogger<SFLoggerPairTest>();
}

[TearDown]
public void AfterTest()
{
// Return to default setting
SFLoggerFactory.EnableSFLogger();
}

[Test]
public void TestUsingSFLogger()
{
SFLoggerFactory.EnableSFLogger();
_loggerPair = SFLoggerFactory.GetLogger<SFLoggerPairTest>();
Assert.IsInstanceOf<SFLoggerPair>(_loggerPair);
}

[Test]
public void TestIsDebugEnabled(
[Values(false, true)] bool isEnabled)
{
_loggerPair = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _loggerPair.IsDebugEnabled());
_loggerPair.Debug("debug log message", new Exception("test exception"));
}

[Test]
public void TestIsInfoEnabled(
[Values(false, true)] bool isEnabled)
{
_loggerPair = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _loggerPair.IsInfoEnabled());
_loggerPair.Info("info log message", new Exception("test exception"));
}

[Test]
public void TestIsWarnEnabled(
[Values(false, true)] bool isEnabled)
{
_loggerPair = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _loggerPair.IsWarnEnabled());
_loggerPair.Warn("warn log message", new Exception("test exception"));
}

[Test]
public void TestIsErrorEnabled(
[Values(false, true)] bool isEnabled)
{
_loggerPair = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _loggerPair.IsErrorEnabled());
_loggerPair.Error("error log message", new Exception("test exception"));
}

[Test]
public void TestIsFatalEnabled(
[Values(false, true)] bool isEnabled)
{
_loggerPair = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _loggerPair.IsFatalEnabled());
_loggerPair.Fatal("fatal log message", new Exception("test exception"));
}

[Test]
public void TestGetAppenders()
{
Assert.Throws<NotImplementedException>(() => _loggerPair.GetAppenders());
}

[Test]
public void TestAddAppender()
{
Assert.Throws<NotImplementedException>(() => _loggerPair.AddAppender(new SFConsoleAppender()));
}

[Test]
public void TestRemoveAppender()
{
Assert.Throws<NotImplementedException>(() => _loggerPair.RemoveAppender(new SFConsoleAppender()));
}

[Test]
public void TestSetLevel()
{
Assert.Throws<NotImplementedException>(() => _loggerPair.SetLevel(LoggingEvent.DEBUG));
}

private SFLogger GetLogger(bool isEnabled)
{
if (isEnabled)
{
SFLoggerFactory.EnableSFLogger();
}
else
{
SFLoggerFactory.DisableSFLogger();
}

return SFLoggerFactory.GetLogger<SFLoggerPairTest>();
}
}
}
14 changes: 7 additions & 7 deletions Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void AfterTest()
public void TestUsingSFLogger()
{
SFLoggerFactory.EnableSFLogger();
_logger = SFLoggerFactory.GetSFLogger<ILoggerTest>();
_logger = SFLoggerFactory.GetSFLogger<SFLoggerTest>();
Assert.IsInstanceOf<SFLoggerImpl>(_logger);
}

[Test]
public void TestUsingEmptyLogger()
{
SFLoggerFactory.DisableSFLogger();
_logger = SFLoggerFactory.GetSFLogger<ILoggerTest>();
_logger = SFLoggerFactory.GetSFLogger<SFLoggerTest>();
Assert.IsInstanceOf<SFLoggerEmptyImpl>(_logger);
}

Expand All @@ -67,8 +67,8 @@ public void TestIsInfoEnabled(
{
_logger = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _logger.IsInformationEnabled());
_logger.Information("info log message", new Exception("test exception"));
Assert.AreEqual(isEnabled, _logger.IsInfoEnabled());
_logger.Info("info log message", new Exception("test exception"));
}

[Test]
Expand All @@ -77,8 +77,8 @@ public void TestIsWarnEnabled(
{
_logger = GetLogger(isEnabled);

Assert.AreEqual(isEnabled, _logger.IsWarningEnabled());
_logger.Warning("warn log message", new Exception("test exception"));
Assert.AreEqual(isEnabled, _logger.IsWarnEnabled());
_logger.Warn("warn log message", new Exception("test exception"));
}

[Test]
Expand Down Expand Up @@ -180,7 +180,7 @@ private SFLogger GetLogger(bool isEnabled)
SFLoggerFactory.DisableSFLogger();
}

return SFLoggerFactory.GetSFLogger<ILoggerTest>(false);
return SFLoggerFactory.GetSFLogger<SFLoggerTest>(false);
}
}
}
32 changes: 16 additions & 16 deletions Snowflake.Data/Client/SnowflakeDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public class SnowflakeDbCommand : DbCommand

private SnowflakeDbParameterCollection parameterCollection;

private SFLoggerPair _loggerPair = SFLoggerPair.GetLoggerPair<SnowflakeDbCommand>();
private SFLogger _logger = SFLoggerFactory.GetLogger<SnowflakeDbCommand>();

private readonly QueryResultsAwaiter _queryResultsAwaiter = QueryResultsAwaiter.Instance;

public SnowflakeDbCommand()
{
_loggerPair.LogDebug("Constructing SnowflakeDbCommand class");
_logger.Debug("Constructing SnowflakeDbCommand class");
// by default, no query timeout
this.CommandTimeout = 0;
parameterCollection = new SnowflakeDbParameterCollection();
Expand Down Expand Up @@ -165,7 +165,7 @@ public override void Cancel()

public override int ExecuteNonQuery()
{
_loggerPair.LogDebug($"ExecuteNonQuery");
_logger.Debug($"ExecuteNonQuery");
SFBaseResultSet resultSet = ExecuteInternal();
long total = 0;
do
Expand All @@ -190,7 +190,7 @@ public override int ExecuteNonQuery()

public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"ExecuteNonQueryAsync");
_logger.Debug($"ExecuteNonQueryAsync");
cancellationToken.ThrowIfCancellationRequested();

var resultSet = await ExecuteInternalAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -217,7 +217,7 @@ public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellat

public override object ExecuteScalar()
{
_loggerPair.LogDebug($"ExecuteScalar");
_logger.Debug($"ExecuteScalar");
SFBaseResultSet resultSet = ExecuteInternal();

if(resultSet.Next())
Expand All @@ -228,7 +228,7 @@ public override object ExecuteScalar()

public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"ExecuteScalarAsync");
_logger.Debug($"ExecuteScalarAsync");
cancellationToken.ThrowIfCancellationRequested();

var result = await ExecuteInternalAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -263,22 +263,22 @@ protected override DbParameter CreateDbParameter()

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
_loggerPair.LogDebug($"ExecuteDbDataReader");
_logger.Debug($"ExecuteDbDataReader");
SFBaseResultSet resultSet = ExecuteInternal();
return new SnowflakeDbDataReader(this, resultSet);
}

protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"ExecuteDbDataReaderAsync");
_logger.Debug($"ExecuteDbDataReaderAsync");
try
{
var result = await ExecuteInternalAsync(cancellationToken).ConfigureAwait(false);
return new SnowflakeDbDataReader(this, result);
}
catch (Exception ex)
{
_loggerPair.LogError("The command failed to execute.", ex);
_logger.Error("The command failed to execute.", ex);
throw;
}
}
Expand All @@ -290,7 +290,7 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBeha
/// <returns>The query id.</returns>
public string ExecuteInAsyncMode()
{
_loggerPair.LogDebug($"ExecuteInAsyncMode");
_logger.Debug($"ExecuteInAsyncMode");
SFBaseResultSet resultSet = ExecuteInternal(asyncExec: true);
return resultSet.queryId;
}
Expand All @@ -303,7 +303,7 @@ public string ExecuteInAsyncMode()
/// <returns>The query id.</returns>
public async Task<string> ExecuteAsyncInAsyncMode(CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"ExecuteAsyncInAsyncMode");
_logger.Debug($"ExecuteAsyncInAsyncMode");
var resultSet = await ExecuteInternalAsync(cancellationToken, asyncExec: true).ConfigureAwait(false);
return resultSet.queryId;
}
Expand All @@ -315,7 +315,7 @@ public async Task<string> ExecuteAsyncInAsyncMode(CancellationToken cancellation
/// <returns>The query status.</returns>
public QueryStatus GetQueryStatus(string queryId)
{
_loggerPair.LogDebug($"GetQueryStatus");
_logger.Debug($"GetQueryStatus");
return _queryResultsAwaiter.GetQueryStatus(connection, queryId);
}

Expand All @@ -327,7 +327,7 @@ public QueryStatus GetQueryStatus(string queryId)
/// <returns>The query status.</returns>
public async Task<QueryStatus> GetQueryStatusAsync(string queryId, CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"GetQueryStatusAsync");
_logger.Debug($"GetQueryStatusAsync");
return await _queryResultsAwaiter.GetQueryStatusAsync(connection, queryId, cancellationToken);
}

Expand All @@ -338,7 +338,7 @@ public async Task<QueryStatus> GetQueryStatusAsync(string queryId, CancellationT
/// <returns>The query results.</returns>
public DbDataReader GetResultsFromQueryId(string queryId)
{
_loggerPair.LogDebug($"GetResultsFromQueryId");
_logger.Debug($"GetResultsFromQueryId");

Task task = _queryResultsAwaiter.RetryUntilQueryResultIsAvailable(connection, queryId, CancellationToken.None, false);
task.Wait();
Expand All @@ -356,7 +356,7 @@ public DbDataReader GetResultsFromQueryId(string queryId)
/// <returns>The query results.</returns>
public async Task<DbDataReader> GetResultsFromQueryIdAsync(string queryId, CancellationToken cancellationToken)
{
_loggerPair.LogDebug($"GetResultsFromQueryIdAsync");
_logger.Debug($"GetResultsFromQueryIdAsync");

await _queryResultsAwaiter.RetryUntilQueryResultIsAvailable(connection, queryId, cancellationToken, true);

Expand Down Expand Up @@ -464,7 +464,7 @@ private void CheckIfCommandTextIsSet()
if (string.IsNullOrEmpty(CommandText))
{
var errorMessage = "Unable to execute command due to command text not being set";
_loggerPair.LogError(errorMessage);
_logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
Expand Down
Loading

0 comments on commit 26a5d53

Please sign in to comment.