Skip to content

Commit fc742d7

Browse files
committed
Log level precedence
1 parent 5fb2d77 commit fc742d7

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsConfigurations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
153153
/// </summary>
154154
/// <value>The log level.</value>
155155
public string LogLevel =>
156-
GetEnvironmentVariableOrDefault(Constants.AwsLogLevelNameEnv, GetEnvironmentVariable(Constants.LogLevelNameEnv));
156+
GetEnvironmentVariableOrDefault(Constants.LogLevelNameEnv, GetEnvironmentVariable(Constants.AwsLogLevelNameEnv));
157157

158158
/// <summary>
159159
/// Gets the logger sample rate.

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/PowertoolsLoggerTest.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,16 +1305,59 @@ public void Log_Should_Serialize_TimeOnly()
13051305
);
13061306
}
13071307

1308+
1309+
[Theory]
1310+
[InlineData(true, "WARN", LogLevel.Warning)]
1311+
[InlineData(false, "Fatal", LogLevel.Critical)]
1312+
[InlineData(false, "NotValid", LogLevel.Critical)]
1313+
[InlineData(true, "NotValid", LogLevel.Warning)]
1314+
public void Log_Should_Use_Powertools_Log_Level_When_Lambda_Log_Level_Enabled(bool willLog, string awsLogLevel, LogLevel logLevel)
1315+
{
1316+
// Arrange
1317+
var loggerName = Guid.NewGuid().ToString();
1318+
1319+
var environment = Substitute.For<IPowertoolsEnvironment>();
1320+
// Powertools Log Level takes precedence
1321+
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns(logLevel.ToString());
1322+
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(awsLogLevel);
1323+
1324+
var systemWrapper = new SystemWrapperMock(environment);
1325+
var configuration = new PowertoolsConfigurations(systemWrapper);
1326+
1327+
var logger = new PowertoolsLogger(loggerName, configuration, systemWrapper, () =>
1328+
new LoggerConfiguration
1329+
{
1330+
LoggerOutputCase = LoggerOutputCase.CamelCase
1331+
});
1332+
1333+
var message = new
1334+
{
1335+
PropOne = "Value 1",
1336+
PropTwo = "Value 2",
1337+
Time = new TimeOnly(12, 0, 0)
1338+
};
1339+
1340+
// Act
1341+
logger.LogWarning(message);
1342+
1343+
// Assert
1344+
Assert.True(logger.IsEnabled(logLevel));
1345+
Assert.Equal(logLevel, configuration.GetLogLevel());
1346+
Assert.Equal(willLog, systemWrapper.LogMethodCalled);
1347+
}
1348+
13081349
[Theory]
13091350
[InlineData(true, "WARN", LogLevel.Warning)]
13101351
[InlineData(false, "Fatal", LogLevel.Critical)]
1311-
public void Log_Should_Use_Lambda_Log_Level_When_Enabled(bool willLog, string awsLogLevel, LogLevel logLevel)
1352+
[InlineData(true, "NotValid", LogLevel.Information)]
1353+
public void Log_Should_Use_AWS_Lambda_Log_Level_When_Enabled(bool willLog, string awsLogLevel, LogLevel logLevel)
13121354
{
13131355
// Arrange
13141356
var loggerName = Guid.NewGuid().ToString();
13151357

13161358
var environment = Substitute.For<IPowertoolsEnvironment>();
1317-
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns("Error");
1359+
// Powertools Log Level not set, AWS Lambda Log Level is used
1360+
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns(string.Empty);
13181361
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(awsLogLevel);
13191362

13201363
var systemWrapper = new SystemWrapperMock(environment);
@@ -1369,7 +1412,7 @@ public void Should_Map_AWS_Log_Level_And_Default_To_Information(string awsLogLev
13691412
[Theory]
13701413
[InlineData(true, LogLevel.Warning)]
13711414
[InlineData(false, LogLevel.Critical)]
1372-
public void Log_Should_Use_Powertools_Log_Level_When_Lambda_Log_Level_Unavailable(bool willLog, LogLevel logLevel)
1415+
public void Log_Should_Use_Powertools_Log_Level_When_Set(bool willLog, LogLevel logLevel)
13731416
{
13741417
// Arrange
13751418
var loggerName = Guid.NewGuid().ToString();

0 commit comments

Comments
 (0)