Skip to content

Commit 5fb2d77

Browse files
committed
Map AWS Lambda LogLevel to Microsoft LogLevel
1 parent c590ac7 commit 5fb2d77

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public bool GetEnvironmentVariableOrDefault(string variable, bool defaultValue)
149149

150150
/// <summary>
151151
/// Gets the log level.
152+
/// Dont' use this method directly use GetLogLevel extension method instead
152153
/// </summary>
153154
/// <value>The log level.</value>
154155
public string LogLevel =>

libraries/src/AWS.Lambda.Powertools.Logging/Internal/PowertoolsConfigurations.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using AWS.Lambda.Powertools.Common;
1819
using Microsoft.Extensions.Logging;
1920

@@ -36,7 +37,12 @@ internal static LogLevel GetLogLevel(this IPowertoolsConfigurations powertoolsCo
3637
if (logLevel.HasValue)
3738
return logLevel.Value;
3839

39-
if (Enum.TryParse((powertoolsConfigurations.LogLevel ?? "").Trim(), true, out LogLevel result))
40+
var logFromEnv = (powertoolsConfigurations.LogLevel ?? "").Trim();
41+
42+
if (AwsLogLevelMapper.TryGetValue(logFromEnv.ToUpper(), out var awsLogLevel))
43+
logFromEnv = awsLogLevel;
44+
45+
if (Enum.TryParse(logFromEnv, true, out LogLevel result))
4046
return result;
4147

4248
return LoggingConstants.DefaultLogLevel;
@@ -53,4 +59,14 @@ internal static LoggerOutputCase GetLoggerOutputCase(this IPowertoolsConfigurati
5359

5460
return LoggingConstants.DefaultLoggerOutputCase;
5561
}
62+
63+
private static Dictionary<string, string> AwsLogLevelMapper = new()
64+
{
65+
{ "TRACE", "TRACE" },
66+
{ "DEBUG", "DEBUG" },
67+
{ "INFO", "INFORMATION" },
68+
{ "WARN", "WARNING" },
69+
{ "ERROR", "ERROR" },
70+
{ "FATAL", "CRITICAL" }
71+
};
5672
}

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,16 +1306,16 @@ public void Log_Should_Serialize_TimeOnly()
13061306
}
13071307

13081308
[Theory]
1309-
[InlineData(true, LogLevel.Warning)]
1310-
[InlineData(false, LogLevel.Critical)]
1311-
public void Log_Should_Use_Lambda_Log_Level_When_Enabled(bool willLog, LogLevel logLevel)
1309+
[InlineData(true, "WARN", LogLevel.Warning)]
1310+
[InlineData(false, "Fatal", LogLevel.Critical)]
1311+
public void Log_Should_Use_Lambda_Log_Level_When_Enabled(bool willLog, string awsLogLevel, LogLevel logLevel)
13121312
{
13131313
// Arrange
13141314
var loggerName = Guid.NewGuid().ToString();
13151315

13161316
var environment = Substitute.For<IPowertoolsEnvironment>();
13171317
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns("Error");
1318-
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(logLevel.ToString());
1318+
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(awsLogLevel);
13191319

13201320
var systemWrapper = new SystemWrapperMock(environment);
13211321
var configuration = new PowertoolsConfigurations(systemWrapper);
@@ -1333,12 +1333,38 @@ public void Log_Should_Use_Lambda_Log_Level_When_Enabled(bool willLog, LogLevel
13331333
Time = new TimeOnly(12, 0, 0)
13341334
};
13351335

1336+
// Act
13361337
logger.LogWarning(message);
13371338

1339+
// Assert
13381340
Assert.True(logger.IsEnabled(logLevel));
1339-
Assert.Equal(logLevel.ToString(), configuration.LogLevel);
1341+
Assert.Equal(logLevel, configuration.GetLogLevel());
13401342
Assert.Equal(willLog, systemWrapper.LogMethodCalled);
13411343
}
1344+
1345+
[Theory]
1346+
[InlineData("TRACE", LogLevel.Trace)]
1347+
[InlineData("debug", LogLevel.Debug)]
1348+
[InlineData("Info", LogLevel.Information)]
1349+
[InlineData("WARN", LogLevel.Warning)]
1350+
[InlineData("ERROR", LogLevel.Error)]
1351+
[InlineData("Fatal", LogLevel.Critical)]
1352+
[InlineData("DoesNotExist", LogLevel.Information)]
1353+
public void Should_Map_AWS_Log_Level_And_Default_To_Information(string awsLogLevel, LogLevel logLevel)
1354+
{
1355+
// Arrange
1356+
var environment = Substitute.For<IPowertoolsEnvironment>();
1357+
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(awsLogLevel);
1358+
1359+
var systemWrapper = new SystemWrapperMock(environment);
1360+
var configuration = new PowertoolsConfigurations(systemWrapper);
1361+
1362+
// Act
1363+
var logLvl = configuration.GetLogLevel();
1364+
1365+
// Assert
1366+
Assert.Equal(logLevel, logLvl);
1367+
}
13421368

13431369
[Theory]
13441370
[InlineData(true, LogLevel.Warning)]
@@ -1366,6 +1392,7 @@ public void Log_Should_Use_Powertools_Log_Level_When_Lambda_Log_Level_Unavailabl
13661392
Time = new TimeOnly(12, 0, 0)
13671393
};
13681394

1395+
// Act
13691396
logger.LogWarning(message);
13701397

13711398
// Assert

0 commit comments

Comments
 (0)