Skip to content

Commit

Permalink
Add tests for new log functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Nov 30, 2024
1 parent 26ef937 commit 1c7afd3
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 23 deletions.
78 changes: 76 additions & 2 deletions Snowflake.Data.Tests/UnitTests/Logger/EasyLoggerManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ public void TestThatChangesLogLevel()
{
// arrange
var logger = SFLoggerFactory.GetSFLogger<SFBlockingChunkDownloaderV3>();

// act
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Off, t_directoryLogPath);

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

// act
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Fatal, t_directoryLogPath);

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

// act
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Error, t_directoryLogPath);

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

// act
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Warn, t_directoryLogPath);

// assert
Expand Down Expand Up @@ -91,6 +123,48 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly()
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)));

// arrange
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Debug, t_directoryLogPath);

// act
logger.Debug(DebugMessage);

// assert
logLines = File.ReadLines(FindLogFilePath(t_directoryLogPath, 2));
Assert.That(logLines, Has.Exactly(1).Matches<string>(s => s.Contains(DebugMessage)));
}

[Test]
public void TestThatRollsLogIfSizeIsTooBig()
{
// arrange
var logger = SFLoggerFactory.GetSFLogger<SFBlockingChunkDownloaderV3>();
EasyLoggerManager.Instance.ReconfigureEasyLogging(EasyLoggingLogLevel.Trace, t_directoryLogPath);

var appenders = SFLogRepository.s_rootLogger.GetAppenders();
appenders.Remove(appenders[0]);
var randomFileName = $"snowflake_dotnet_{Path.GetRandomFileName()}";
var logFileName = randomFileName.Substring(0, randomFileName.Length - 4) + ".log";
appenders.Add(new SFRollingFileAppender()
{
_name = "RollingFileAppender",
_logFilePath = Path.Combine(t_directoryLogPath, logFileName),
_maximumFileSize = 1,
_maxSizeRollBackups = 2,
_patternLayout = EasyLoggerManager.PatternLayout()
});

// act
for (int i = 0; i < 5; i++)
{
logger.Debug(DebugMessage);
System.Threading.Thread.Sleep(1000);
}
var backupLogs = Directory.GetFiles(t_directoryLogPath, $"{logFileName}.*.bak");

// assert
Assert.AreEqual(2, backupLogs.Length);
}

[Test]
Expand Down Expand Up @@ -131,11 +205,11 @@ private static string RandomLogsDirectoryPath()
return Path.Combine(s_logsDirectory, $"easy_logging_logs_{randomName}", "dotnet");
}

private static string FindLogFilePath(string directoryLogPath)
private static string FindLogFilePath(string directoryLogPath, int expectedFileCount = 1)
{
Assert.IsTrue(Directory.Exists(directoryLogPath));
var files = Directory.GetFiles(directoryLogPath);
Assert.AreEqual(1, files.Length);
Assert.AreEqual(expectedFileCount, files.Length);
return files.First();
}

Expand Down
8 changes: 4 additions & 4 deletions Snowflake.Data.Tests/UnitTests/Logger/ILoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ILoggerTest
private const string SerilogFileName = "test_serilog.log";
private const string NlogFileName = "test_nlog.log";

public abstract class SFBaseLoggerTest
public abstract class ILoggerBaseTest
{
protected ILogger _logger;
protected string _logFile;
Expand Down Expand Up @@ -154,7 +154,7 @@ public void TestThatLogsToProperFileWithProperLogLevelOnly()
}

[TestFixture]
public class Log4NetTest : SFBaseLoggerTest
public class Log4NetTest : ILoggerBaseTest
{
[OneTimeSetUp]
public void SetUp()
Expand All @@ -172,7 +172,7 @@ public void SetUp()
}

[TestFixture]
public class SerilogTest : SFBaseLoggerTest
public class SerilogTest : ILoggerBaseTest
{
[OneTimeSetUp]
public void SetUp()
Expand All @@ -190,7 +190,7 @@ public void SetUp()
}

[TestFixture]
public class NlogTest : SFBaseLoggerTest
public class NlogTest : ILoggerBaseTest
{
[OneTimeSetUp]
public void SetUp()
Expand Down
80 changes: 77 additions & 3 deletions Snowflake.Data.Tests/UnitTests/Logger/SFLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using NUnit.Framework;
using Snowflake.Data.Configuration;
using Snowflake.Data.Log;
using System;

namespace Snowflake.Data.Tests.UnitTests
{
Expand Down Expand Up @@ -35,15 +36,15 @@ public void AfterTest()
}

[Test]
public void TestUsingSimpleLogger()
public void TestUsingSFLogger()
{
SFLoggerFactory.EnableSFLogger();
_logger = SFLoggerFactory.GetSFLogger<ILoggerTest>();
Assert.IsInstanceOf<SFLoggerImpl>(_logger);
}

[Test]
public void TestSettingCustomLogger()
public void TestUsingEmptyLogger()
{
SFLoggerFactory.DisableSFLogger();
_logger = SFLoggerFactory.GetSFLogger<ILoggerTest>();
Expand All @@ -57,6 +58,7 @@ public void TestIsDebugEnabled(
_logger = GetLogger(isEnabled);

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

[Test]
Expand All @@ -66,6 +68,7 @@ public void TestIsInfoEnabled(
_logger = GetLogger(isEnabled);

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

[Test]
Expand All @@ -75,6 +78,7 @@ public void TestIsWarnEnabled(
_logger = GetLogger(isEnabled);

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

[Test]
Expand All @@ -84,6 +88,7 @@ public void TestIsErrorEnabled(
_logger = GetLogger(isEnabled);

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

[Test]
Expand All @@ -93,6 +98,75 @@ public void TestIsFatalEnabled(
_logger = GetLogger(isEnabled);

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

[Test]
public void TestGetAppenders(
[Values(false, true)] bool isEnabled)
{
_logger = GetLogger(isEnabled);
if (isEnabled)
{
var appenders = _logger.GetAppenders();
Assert.IsInstanceOf<SFConsoleAppender>(appenders[0]);
}
else
{
Assert.Throws<NotImplementedException>(() => _logger.GetAppenders());
}
}

[Test]
public void TestAddAppender(
[Values(false, true)] bool isEnabled)
{
_logger = GetLogger(isEnabled);
if (isEnabled)
{
var appenders = _logger.GetAppenders();
Assert.AreEqual(1, appenders.Count);
_logger.AddAppender(new SFConsoleAppender());
Assert.AreEqual(2, appenders.Count);
}
else
{
Assert.Throws<NotImplementedException>(() => _logger.AddAppender(new SFConsoleAppender()));
}
}

[Test]
public void TestRemoveAppender(
[Values(false, true)] bool isEnabled)
{
_logger = GetLogger(isEnabled);
if (isEnabled)
{
var appenders = _logger.GetAppenders();
Assert.AreEqual(1, appenders.Count);
_logger.RemoveAppender(appenders[0]);
Assert.AreEqual(0, appenders.Count);
}
else
{
Assert.Throws<NotImplementedException>(() => _logger.RemoveAppender(new SFConsoleAppender()));
}
}

[Test]
public void TestSetLevel(
[Values(false, true)] bool isEnabled)
{
_logger = GetLogger(isEnabled);
if (isEnabled)
{
_logger.SetLevel(LoggingEvent.DEBUG);
Assert.AreEqual(LoggingEvent.DEBUG, ((SFLoggerImpl)_logger)._level);
}
else
{
Assert.Throws<NotImplementedException>(() => _logger.SetLevel(LoggingEvent.DEBUG));
}
}

private SFLogger GetLogger(bool isEnabled)
Expand All @@ -106,7 +180,7 @@ private SFLogger GetLogger(bool isEnabled)
SFLoggerFactory.DisableSFLogger();
}

return SFLoggerFactory.GetSFLogger<ILoggerTest>();
return SFLoggerFactory.GetSFLogger<ILoggerTest>(false);
}
}
}
3 changes: 2 additions & 1 deletion Snowflake.Data/Configuration/EasyLoggingLogLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

namespace Snowflake.Data.Configuration
{
internal enum EasyLoggingLogLevel
public enum EasyLoggingLogLevel
{
Off,
Fatal,
Error,
Warn,
Info,
Expand Down
1 change: 1 addition & 0 deletions Snowflake.Data/Logger/EasyLoggingLevelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public LoggingEvent ToLoggingEventLevel(EasyLoggingLogLevel level)
switch (level)
{
case EasyLoggingLogLevel.Off: return LoggingEvent.OFF;
case EasyLoggingLogLevel.Fatal: return LoggingEvent.FATAL;
case EasyLoggingLogLevel.Error: return LoggingEvent.ERROR;
case EasyLoggingLogLevel.Warn: return LoggingEvent.WARN;
case EasyLoggingLogLevel.Info: return LoggingEvent.INFO;
Expand Down
30 changes: 21 additions & 9 deletions Snowflake.Data/Logger/SFLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,33 @@ public static void SetCustomLogger(ILogger customLogger)
SFLoggerFactory.s_customLogger = customLogger;
}

internal static SFLogger GetSFLogger<T>()
internal static SFLogger GetSFLogger<T>(bool useFileAppender = true)
{
// If true, return the default/specified logger
if (s_isSFLoggerEnabled)
{
var logger = new SFLoggerImpl(typeof(T));
var fileAppender = new SFRollingFileAppender()
if (useFileAppender)
{
_name = "RollingFileAppender",
_logFilePath = Path.Combine(Directory.GetCurrentDirectory(), "test_snowflake_log.log"),
_maximumFileSize = 1000000000, // "1GB"
_maxSizeRollBackups = 0,
_patternLayout = EasyLoggerManager.PatternLayout()
};
logger.AddAppender(fileAppender);
var fileAppender = new SFRollingFileAppender()
{
_name = "RollingFileAppender",
_logFilePath = Path.Combine(Directory.GetCurrentDirectory(), "test_snowflake_log.log"),
_maximumFileSize = 1000000000, // "1GB"
_maxSizeRollBackups = 0,
_patternLayout = EasyLoggerManager.PatternLayout()
};
logger.AddAppender(fileAppender);
}
else
{
var consoleAppender = new SFConsoleAppender()
{
_name = "ConsoleAppender",
_patternLayout = EasyLoggerManager.PatternLayout()
};
logger.AddAppender(consoleAppender);
}
return logger;
}
// Else, return the empty logger implementation which outputs nothing
Expand Down
4 changes: 0 additions & 4 deletions Snowflake.Data/Logger/SFLoggerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

public static class SFLogRepository
{
//internal static Dictionary<string, SFLogger> _logRepository = new Dictionary<string, SFLogger>();

internal static SFLogger s_rootLogger = s_rootLogger = new SFLoggerImpl(typeof(SFLogRepository));

internal static SFLogger GetRootLogger()
Expand Down Expand Up @@ -184,8 +182,6 @@ private void SetEnableValues()
case LoggingEvent.INFO:
_isDebugEnabled = false;
break;
default:
break;
}
}
}
Expand Down

0 comments on commit 1c7afd3

Please sign in to comment.