Skip to content

Commit

Permalink
Refactor logger functions add logger pair class
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Dec 5, 2024
1 parent f1fb9c2 commit c9d8d92
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 78 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.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
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.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
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.IsInfoEnabled());
Assert.IsFalse(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsFalse(logger.IsWarningEnabled());
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.IsInfoEnabled());
Assert.IsTrue(logger.IsWarnEnabled());
Assert.IsFalse(logger.IsInformationEnabled());
Assert.IsTrue(logger.IsWarningEnabled());
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.IsInfoEnabled());
Assert.IsTrue(logger.IsWarnEnabled());
Assert.IsTrue(logger.IsInformationEnabled());
Assert.IsTrue(logger.IsWarningEnabled());
Assert.IsTrue(logger.IsErrorEnabled());
Assert.IsTrue(logger.IsFatalEnabled());
}
Expand All @@ -111,8 +111,8 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly()

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

Expand Down
8 changes: 4 additions & 4 deletions Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void TestIsInfoEnabled(
{
_logger = GetLogger(isEnabled);

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

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

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

[Test]
Expand Down
49 changes: 16 additions & 33 deletions Snowflake.Data/Client/SnowflakeDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ public class SnowflakeDbCommand : DbCommand

private SnowflakeDbParameterCollection parameterCollection;

private SFLogger _snowflakeLogger = SFLoggerFactory.GetSFLogger<SnowflakeDbCommand>();

private ILogger _customLogger = SFLoggerFactory.GetCustomLogger<SnowflakeDbCommand>();
private SFLoggerPair _loggerPair = SFLoggerPair.GetLoggerPair<SnowflakeDbCommand>();

private readonly QueryResultsAwaiter _queryResultsAwaiter = QueryResultsAwaiter.Instance;

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

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

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

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

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

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

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

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

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

protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
_snowflakeLogger.Debug($"ExecuteDbDataReaderAsync");
_customLogger.LogDebug($"ExecuteDbDataReaderAsync");
_loggerPair.LogDebug($"ExecuteDbDataReaderAsync");
try
{
var result = await ExecuteInternalAsync(cancellationToken).ConfigureAwait(false);
return new SnowflakeDbDataReader(this, result);
}
catch (Exception ex)
{
_snowflakeLogger.Error("The command failed to execute.", ex);
_customLogger.LogError("The command failed to execute.", ex);
_loggerPair.LogError("The command failed to execute.", ex);

Check warning on line 282 in Snowflake.Data/Client/SnowflakeDbCommand.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Client/SnowflakeDbCommand.cs#L282

Added line #L282 was not covered by tests
throw;
}
}
Expand All @@ -301,8 +291,7 @@ protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBeha
/// <returns>The query id.</returns>
public string ExecuteInAsyncMode()
{
_snowflakeLogger.Debug($"ExecuteInAsyncMode");
_customLogger.LogDebug($"ExecuteInAsyncMode");
_loggerPair.LogDebug($"ExecuteInAsyncMode");
SFBaseResultSet resultSet = ExecuteInternal(asyncExec: true);
return resultSet.queryId;
}
Expand All @@ -315,8 +304,7 @@ public string ExecuteInAsyncMode()
/// <returns>The query id.</returns>
public async Task<string> ExecuteAsyncInAsyncMode(CancellationToken cancellationToken)
{
_snowflakeLogger.Debug($"ExecuteAsyncInAsyncMode");
_customLogger.LogDebug($"ExecuteAsyncInAsyncMode");
_loggerPair.LogDebug($"ExecuteAsyncInAsyncMode");
var resultSet = await ExecuteInternalAsync(cancellationToken, asyncExec: true).ConfigureAwait(false);
return resultSet.queryId;
}
Expand All @@ -328,8 +316,7 @@ public async Task<string> ExecuteAsyncInAsyncMode(CancellationToken cancellation
/// <returns>The query status.</returns>
public QueryStatus GetQueryStatus(string queryId)
{
_snowflakeLogger.Debug($"GetQueryStatus");
_customLogger.LogDebug($"GetQueryStatus");
_loggerPair.LogDebug($"GetQueryStatus");
return _queryResultsAwaiter.GetQueryStatus(connection, queryId);
}

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

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

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

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

Expand Down Expand Up @@ -481,8 +465,7 @@ private void CheckIfCommandTextIsSet()
if (string.IsNullOrEmpty(CommandText))
{
var errorMessage = "Unable to execute command due to command text not being set";
_snowflakeLogger.Debug(errorMessage);
_customLogger.LogError(errorMessage);
_loggerPair.LogError(errorMessage);
throw new Exception(errorMessage);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Snowflake.Data/Configuration/EasyLoggingConfigFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private string GetFilePathFromInputParameter(string filePath, string inputDescri
{
return null;
}
s_logger.Info($"Using config file specified from {inputDescription}: {filePath}");
s_logger.Information($"Using config file specified from {inputDescription}: {filePath}");
return filePath;
}

Expand All @@ -77,7 +77,7 @@ private string SearchForConfigInDirectory(Func<string> directoryProvider, string
var directory = directoryProvider.Invoke();
if (string.IsNullOrEmpty(directory))
{
s_logger.Warn($"The {directoryDescription} directory could not be determined and will be skipped");
s_logger.Warning($"The {directoryDescription} directory could not be determined and will be skipped");
return null;
}

Expand All @@ -95,7 +95,7 @@ private string OnlyIfFileExists(string filePath, string directoryDescription)
{
if (_fileOperations.Exists(filePath))
{
s_logger.Info($"Using config file specified from {directoryDescription} directory: {filePath}");
s_logger.Information($"Using config file specified from {directoryDescription} directory: {filePath}");
return filePath;
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion Snowflake.Data/Configuration/EasyLoggingConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void CheckForUnknownFields(string fileContent)
.Cast<JProperty>()
.Where(property => !knownProperties.Contains(property.Name, StringComparer.OrdinalIgnoreCase))
.ToList()
.ForEach(unknownKey => s_logger.Warn($"Unknown field from config: {unknownKey.Name}"));
.ForEach(unknownKey => s_logger.Warning($"Unknown field from config: {unknownKey.Name}"));
}
}
}
16 changes: 8 additions & 8 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public virtual void Init(string configFilePathFromConnectionString)
}
if (string.IsNullOrEmpty(configFilePathFromConnectionString))
{
s_logger.Info($"Attempting to enable easy logging without a config file specified from connection string");
s_logger.Information($"Attempting to enable easy logging without a config file specified from connection string");
}
else
{
s_logger.Info($"Attempting to enable easy logging using config file specified from connection string: {configFilePathFromConnectionString}");
s_logger.Information($"Attempting to enable easy logging using config file specified from connection string: {configFilePathFromConnectionString}");
}
var config = _easyLoggingConfigProvider.ProvideConfig(configFilePathFromConnectionString);
if (config == null)
Expand All @@ -76,8 +76,8 @@ public virtual void Init(string configFilePathFromConnectionString)
}
var logLevel = GetLogLevel(config.CommonProps.LogLevel);
var logPath = GetLogPath(config.CommonProps.LogPath);
s_logger.Info($"LogLevel set to {logLevel}");
s_logger.Info($"LogPath set to {logPath}");
s_logger.Information($"LogLevel set to {logLevel}");
s_logger.Information($"LogPath set to {logPath}");
_easyLoggerManager.ReconfigureEasyLogging(logLevel, logPath);
_initTrialParameters = new EasyLoggingInitTrialParameters(configFilePathFromConnectionString);
}
Expand All @@ -100,7 +100,7 @@ private bool AllowedToInitialize(string configFilePathFromConnectionString)
var isAllowedToInitialize = !everTriedToInitialize || (triedToInitializeWithoutConfigFile && isGivenConfigFilePath);
if (!isAllowedToInitialize && _initTrialParameters.HasDifferentConfigPath(configFilePathFromConnectionString))
{
s_logger.Warn($"Easy logging will not be configured for CLIENT_CONFIG_FILE={configFilePathFromConnectionString} because it was previously configured for a different client config");
s_logger.Warning($"Easy logging will not be configured for CLIENT_CONFIG_FILE={configFilePathFromConnectionString} because it was previously configured for a different client config");
}

return isAllowedToInitialize;
Expand All @@ -110,7 +110,7 @@ private EasyLoggingLogLevel GetLogLevel(string logLevel)
{
if (string.IsNullOrEmpty(logLevel))
{
s_logger.Warn("LogLevel in client config not found. Using default value: OFF");
s_logger.Warning("LogLevel in client config not found. Using default value: OFF");

Check warning on line 113 in Snowflake.Data/Core/Session/EasyLoggingStarter.cs

View check run for this annotation

Codecov / codecov/patch

Snowflake.Data/Core/Session/EasyLoggingStarter.cs#L113

Added line #L113 was not covered by tests
return EasyLoggingLogLevel.Off;
}
return EasyLoggingLogLevelExtensions.From(logLevel);
Expand All @@ -121,7 +121,7 @@ private string GetLogPath(string logPath)
var logPathOrDefault = logPath;
if (string.IsNullOrEmpty(logPath))
{
s_logger.Warn("LogPath in client config not found. Using home directory as a default value");
s_logger.Warning("LogPath in client config not found. Using home directory as a default value");
logPathOrDefault = HomeDirectoryProvider.HomeDirectory(_environmentOperations);
if (string.IsNullOrEmpty(logPathOrDefault))
{
Expand Down Expand Up @@ -163,7 +163,7 @@ private void CheckDirPermissionsOnlyAllowUser(string dirPath)
var dirPermissions = _unixOperations.GetDirPermissions(dirPath);
if (dirPermissions != FileAccessPermissions.UserReadWriteExecute)
{
s_logger.Warn($"Access permission for the logs directory is currently " +
s_logger.Warning($"Access permission for the logs directory is currently " +
$"{UnixFilePermissionsConverter.ConvertFileAccessPermissionsToInt(dirPermissions)} " +
$"and is potentially accessible to users other than the owner of the logs directory");
}
Expand Down
8 changes: 4 additions & 4 deletions Snowflake.Data/Logger/SFLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ interface SFLogger
{
bool IsDebugEnabled();

bool IsInfoEnabled();
bool IsInformationEnabled();

bool IsWarnEnabled();
bool IsWarningEnabled();

bool IsErrorEnabled();

bool IsFatalEnabled();

void Debug(string msg, Exception ex = null);

void Info(string msg, Exception ex = null);
void Information(string msg, Exception ex = null);

void Warn(string msg, Exception ex = null);
void Warning(string msg, Exception ex = null);

void Error(string msg, Exception ex = null);

Expand Down
8 changes: 4 additions & 4 deletions Snowflake.Data/Logger/SFLoggerEmptyImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public bool IsDebugEnabled()
return false;
}

public bool IsInfoEnabled()
public bool IsInformationEnabled()
{
return false;
}

public bool IsWarnEnabled()
public bool IsWarningEnabled()
{
return false;
}
Expand All @@ -42,12 +42,12 @@ public void Debug(string msg, Exception ex)
return;
}

public void Info(string msg, Exception ex)
public void Information(string msg, Exception ex)
{
return;
}

public void Warn(string msg, Exception ex)
public void Warning(string msg, Exception ex)
{
return;
}
Expand Down
Loading

0 comments on commit c9d8d92

Please sign in to comment.