-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-856231 Enable easy logging (#774)
### Description Enable easy logging feature. ### Checklist - [x] Code compiles correctly - [x] Code is formatted according to [Coding Conventions](../CodingConventions.md) - [x] Created tests which fail without the change (if possible) - [x] All tests passing (`dotnet test`) - [x] Extended the README / documentation, if necessary - [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
- Loading branch information
1 parent
ca3153c
commit 6fceac1
Showing
27 changed files
with
837 additions
and
104 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingLogLevelTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2023 Snowflake Computing Inc. All rights reserved. | ||
*/ | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using Snowflake.Data.Configuration; | ||
using Snowflake.Data.Core; | ||
using Snowflake.Data.Log; | ||
|
||
namespace Snowflake.Data.Tests.UnitTests.Logger | ||
{ | ||
[TestFixture, NonParallelizable] | ||
public class EasyLoggerManagerTest | ||
{ | ||
|
||
private const string InfoMessage = "Easy logging Info message"; | ||
private const string DebugMessage = "Easy logging Debug message"; | ||
private const string WarnMessage = "Easy logging Warn message"; | ||
private const string ErrorMessage = "Easy logging Error message"; | ||
private const string FatalMessage = "Easy logging Fatal message"; | ||
private static readonly string s_logsDirectory = Path.GetTempPath(); | ||
|
||
[ThreadStatic] | ||
private static string t_directoryLogPath; | ||
|
||
[OneTimeTearDown] | ||
public static void CleanUp() | ||
{ | ||
RemoveEasyLoggingLogFiles(); | ||
} | ||
|
||
[SetUp] | ||
public void BeforeEach() | ||
{ | ||
t_directoryLogPath = RandomLogsDirectoryPath(); | ||
} | ||
|
||
[TearDown] | ||
public void AfterEach() | ||
{ | ||
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, t_directoryLogPath); | ||
} | ||
|
||
[Test] | ||
public void TestThatChangesLogLevel() | ||
{ | ||
// arrange | ||
var logger = SFLoggerFactory.GetLogger<SFBlockingChunkDownloaderV3>(); | ||
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, t_directoryLogPath); | ||
|
||
// assert | ||
Assert.IsFalse(logger.IsDebugEnabled()); | ||
Assert.IsFalse(logger.IsInfoEnabled()); | ||
Assert.IsTrue(logger.IsWarnEnabled()); | ||
Assert.IsTrue(logger.IsErrorEnabled()); | ||
Assert.IsTrue(logger.IsFatalEnabled()); | ||
|
||
// act | ||
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Debug, t_directoryLogPath); | ||
|
||
// assert | ||
Assert.IsTrue(logger.IsDebugEnabled()); | ||
Assert.IsTrue(logger.IsInfoEnabled()); | ||
Assert.IsTrue(logger.IsWarnEnabled()); | ||
Assert.IsTrue(logger.IsErrorEnabled()); | ||
Assert.IsTrue(logger.IsFatalEnabled()); | ||
} | ||
|
||
[Test] | ||
public void TestThatLogsToProperFileWithProperLogLevelOnly() | ||
{ | ||
// arrange | ||
var logger = SFLoggerFactory.GetLogger<SFBlockingChunkDownloaderV3>(); | ||
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Info, t_directoryLogPath); | ||
|
||
// act | ||
logger.Debug(DebugMessage); | ||
logger.Info(InfoMessage); | ||
logger.Warn(WarnMessage); | ||
logger.Error(ErrorMessage); | ||
logger.Fatal(FatalMessage); | ||
|
||
// assert | ||
var logLines = File.ReadLines(FindLogFilePath(t_directoryLogPath)); | ||
Assert.That(logLines, Has.Exactly(0).Matches<string>(s => s.Contains(DebugMessage))); | ||
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(InfoMessage))); | ||
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(WarnMessage))); | ||
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(ErrorMessage))); | ||
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(FatalMessage))); | ||
} | ||
|
||
private static string RandomLogsDirectoryPath() | ||
{ | ||
var randomName = Path.GetRandomFileName(); | ||
return Path.Combine(s_logsDirectory, $"easy_logging_logs_{randomName}", "dotnet"); | ||
} | ||
|
||
private static string FindLogFilePath(string directoryLogPath) | ||
{ | ||
Assert.IsTrue(Directory.Exists(directoryLogPath)); | ||
var files = Directory.GetFiles(directoryLogPath); | ||
Assert.AreEqual(1, files.Length); | ||
return files.First(); | ||
} | ||
|
||
private static void RemoveEasyLoggingLogFiles() | ||
{ | ||
Directory.GetFiles(s_logsDirectory) | ||
.Where(filePath => filePath.StartsWith(Path.Combine(s_logsDirectory, "easy_logging_logs"))) | ||
.AsParallel() | ||
.ForAll(filePath => File.Delete(filePath)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.