Skip to content

Commit cab672d

Browse files
SNOW-1899127 Fix possibility to configure easy logging log_path as STDOUT (#1087)
Co-authored-by: Eric Finch <[email protected]>
1 parent 4167b76 commit cab672d

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

Snowflake.Data.Tests/UnitTests/Logger/EasyLoggingStarterTest.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class EasyLoggingStarterTest
2424
private const string ConfigPath = "/some-path/config.json";
2525
private const string AnotherConfigPath = "/another/path";
2626
private static readonly string s_expectedLogPath = Path.Combine(LogPath, "dotnet");
27-
27+
2828
private static readonly ClientConfig s_configWithErrorLevel = new ClientConfig
2929
{
3030
CommonProps = new ClientConfigCommonProps
@@ -33,7 +33,7 @@ public class EasyLoggingStarterTest
3333
LogPath = LogPath
3434
}
3535
};
36-
36+
3737
private static readonly ClientConfig s_configWithInfoLevel = new ClientConfig
3838
{
3939
CommonProps = new ClientConfigCommonProps
@@ -51,12 +51,21 @@ public class EasyLoggingStarterTest
5151
}
5252
};
5353

54+
private static readonly ClientConfig s_configWithStdoutAsLogPath = new ClientConfig
55+
{
56+
CommonProps = new ClientConfigCommonProps
57+
{
58+
LogLevel = "Info",
59+
LogPath = "STDOUT"
60+
}
61+
};
62+
5463
[ThreadStatic]
5564
private static Mock<EasyLoggingConfigProvider> t_easyLoggingProvider;
56-
65+
5766
[ThreadStatic]
5867
private static Mock<EasyLoggerManager> t_easyLoggerManager;
59-
68+
6069
[ThreadStatic]
6170
private static Mock<UnixOperations> t_unixOperations;
6271

@@ -68,7 +77,7 @@ public class EasyLoggingStarterTest
6877

6978
[ThreadStatic]
7079
private static EasyLoggingStarter t_easyLoggerStarter;
71-
80+
7281
[SetUp]
7382
public void BeforeEach()
7483
{
@@ -158,7 +167,7 @@ public void TestFailIfDirectoryCreationFails()
158167
{
159168
Assert.Ignore("skip test on Windows");
160169
}
161-
170+
162171
// arrange
163172
t_easyLoggingProvider
164173
.Setup(provider => provider.ProvideConfig(ConfigPath))
@@ -169,7 +178,7 @@ public void TestFailIfDirectoryCreationFails()
169178

170179
// act
171180
var thrown = Assert.Throws<Exception>(() => t_easyLoggerStarter.Init(ConfigPath));
172-
181+
173182
// assert
174183
Assert.That(thrown.Message, Does.Contain("Failed to create logs directory"));
175184
}
@@ -187,10 +196,10 @@ public void TestThatConfiguresEasyLoggingOnlyOnceWhenInitializedWithConfigPath()
187196
t_easyLoggingProvider
188197
.Setup(provider => provider.ProvideConfig(AnotherConfigPath))
189198
.Returns(s_configWithInfoLevel);
190-
199+
191200
// act
192201
t_easyLoggerStarter.Init(ConfigPath);
193-
202+
194203
// assert
195204
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
196205
{
@@ -202,24 +211,24 @@ public void TestThatConfiguresEasyLoggingOnlyOnceWhenInitializedWithConfigPath()
202211
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR), Times.Once);
203212
}
204213
t_easyLoggerManager.Verify(manager => manager.ReconfigureEasyLogging(EasyLoggingLogLevel.Error, s_expectedLogPath), Times.Once);
205-
214+
206215
// act
207216
t_easyLoggerStarter.Init(null);
208217
t_easyLoggerStarter.Init(ConfigPath);
209218
t_easyLoggerStarter.Init(AnotherConfigPath);
210-
219+
211220
// assert
212221
t_easyLoggerManager.VerifyNoOtherCalls();
213222
}
214-
223+
215224
[Test]
216225
public void TestThatConfiguresEasyLoggingOnlyOnceForInitializationsWithoutConfigPath()
217226
{
218227
// arrange
219228
t_easyLoggingProvider
220229
.Setup(provider => provider.ProvideConfig(null))
221230
.Returns(s_configWithErrorLevel);
222-
231+
223232
// act
224233
t_easyLoggerStarter.Init(null);
225234
t_easyLoggerStarter.Init(null);
@@ -247,10 +256,10 @@ public void TestThatReconfiguresEasyLoggingWithConfigPathIfNotGivenForTheFirstTi
247256
t_easyLoggingProvider
248257
.Setup(provider => provider.ProvideConfig(ConfigPath))
249258
.Returns(s_configWithInfoLevel);
250-
259+
251260
// act
252261
t_easyLoggerStarter.Init(null);
253-
262+
254263
// assert
255264
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
256265
{
@@ -265,10 +274,25 @@ public void TestThatReconfiguresEasyLoggingWithConfigPathIfNotGivenForTheFirstTi
265274

266275
// act
267276
t_easyLoggerStarter.Init(ConfigPath);
268-
277+
269278
// assert
270279
t_easyLoggerManager.Verify(manager => manager.ReconfigureEasyLogging(EasyLoggingLogLevel.Info, s_expectedLogPath), Times.Once);
271280
t_easyLoggerManager.VerifyNoOtherCalls();
272281
}
282+
283+
[Test]
284+
public void TestConfigureStdout()
285+
{
286+
// arrange
287+
t_easyLoggingProvider
288+
.Setup(provider => provider.ProvideConfig(null))
289+
.Returns(s_configWithStdoutAsLogPath);
290+
291+
// act
292+
t_easyLoggerStarter.Init(null);
293+
294+
// assert
295+
t_easyLoggerManager.Verify(manager => manager.ReconfigureEasyLogging(EasyLoggingLogLevel.Info, "STDOUT"), Times.Once);
296+
}
273297
}
274298
}

Snowflake.Data/Core/Session/EasyLoggingStarter.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Snowflake.Data.Core
1616
internal class EasyLoggingStarter
1717
{
1818
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<EasyLoggingStarter>();
19-
19+
2020
private readonly EasyLoggingConfigProvider _easyLoggingConfigProvider;
2121

2222
private readonly EasyLoggerManager _easyLoggerManager;
@@ -28,12 +28,12 @@ internal class EasyLoggingStarter
2828
private readonly EnvironmentOperations _environmentOperations;
2929

3030
private readonly object _lockForExclusiveInit = new object();
31-
31+
3232
private EasyLoggingInitTrialParameters _initTrialParameters = null;
3333

3434
public static readonly EasyLoggingStarter Instance = new EasyLoggingStarter(EasyLoggingConfigProvider.Instance,
3535
EasyLoggerManager.Instance, UnixOperations.Instance, DirectoryOperations.Instance, EnvironmentOperations.Instance);
36-
36+
3737
internal EasyLoggingStarter(
3838
EasyLoggingConfigProvider easyLoggingConfigProvider,
3939
EasyLoggerManager easyLoggerManager,
@@ -91,7 +91,7 @@ internal void Reset(EasyLoggingLogLevel logLevel)
9191
_easyLoggerManager.ResetEasyLogging(logLevel);
9292
}
9393
}
94-
94+
9595
private bool AllowedToInitialize(string configFilePathFromConnectionString)
9696
{
9797
var everTriedToInitialize = _initTrialParameters != null;
@@ -128,6 +128,10 @@ private string GetLogPath(string logPath)
128128
throw new Exception("No log path found for easy logging. Home directory is not configured and log path is not provided");
129129
}
130130
}
131+
if (EasyLoggerManager.IsStdout(logPath))
132+
{
133+
return logPath;
134+
}
131135
var pathWithDotnetSubdirectory = Path.Combine(logPathOrDefault, "dotnet");
132136
if (!_directoryOperations.Exists(pathWithDotnetSubdirectory))
133137
{
@@ -184,7 +188,7 @@ public bool IsConfigFilePathGiven()
184188
{
185189
return _configFilePathFromConnectionString != null;
186190
}
187-
191+
188192
public bool HasDifferentConfigPath(string configFilePath)
189193
{
190194
return IsConfigFilePathGiven()

Snowflake.Data/Logger/EasyLoggerManager.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class EasyLoggerManager
2121
private const string AppenderPrefix = "SFEasyLogging";
2222

2323
private readonly EasyLoggingLevelMapper _levelMapper = EasyLoggingLevelMapper.Instance;
24-
24+
2525
public virtual void ReconfigureEasyLogging(EasyLoggingLogLevel easyLoggingLogLevel, string logsPath)
2626
{
2727
var log4netLevel = _levelMapper.ToLog4NetLevel(easyLoggingLogLevel);
@@ -30,14 +30,19 @@ public virtual void ReconfigureEasyLogging(EasyLoggingLogLevel easyLoggingLogLev
3030
var repository = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
3131
var rootLogger = (log4net.Repository.Hierarchy.Logger)repository.GetLogger("Snowflake.Data");
3232
rootLogger.Level = log4netLevel;
33-
var appender = string.Equals(logsPath, "STDOUT", StringComparison.OrdinalIgnoreCase)
33+
var appender = IsStdout(logsPath)
3434
? AddConsoleAppender(rootLogger)
3535
: AddRollingFileAppender(rootLogger, logsPath);
3636
RemoveOtherEasyLoggingAppenders(rootLogger, appender);
3737
repository.RaiseConfigurationChanged(EventArgs.Empty);
3838
}
3939
}
4040

41+
internal static bool IsStdout(string logsPath)
42+
{
43+
return string.Equals(logsPath, "STDOUT", StringComparison.OrdinalIgnoreCase);
44+
}
45+
4146
internal void ResetEasyLogging(EasyLoggingLogLevel easyLoggingLogLevel)
4247
{
4348
var log4netLevel = _levelMapper.ToLog4NetLevel(easyLoggingLogLevel);
@@ -57,7 +62,7 @@ internal static bool HasEasyLoggingAppender()
5762
var rootLogger = (log4net.Repository.Hierarchy.Logger)repository.GetLogger("Snowflake.Data");
5863
return rootLogger.Appenders.ToArray().Any(IsEasyLoggingAppender);
5964
}
60-
65+
6166
private static void RemoveOtherEasyLoggingAppenders(log4net.Repository.Hierarchy.Logger logger, IAppender appender)
6267
{
6368
var existingAppenders = logger.Appenders.ToArray();

0 commit comments

Comments
 (0)