Skip to content

Commit c590ac7

Browse files
committed
Add support for Lambda log level
1 parent 654de19 commit c590ac7

File tree

4 files changed

+142
-7
lines changed

4 files changed

+142
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ internal static class Constants
5454
/// Constant for POWERTOOLS_LOG_LEVEL environment variable
5555
/// </summary>
5656
internal const string LogLevelNameEnv = "POWERTOOLS_LOG_LEVEL";
57+
58+
/// <summary>
59+
/// Constant for POWERTOOLS_LOG_LEVEL environment variable
60+
/// </summary>
61+
internal const string AwsLogLevelNameEnv = "AWS_LAMBDA_LOG_LEVEL";
5762

5863
/// <summary>
5964
/// Constant for POWERTOOLS_LOGGER_SAMPLE_RATE environment variable

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

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

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

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

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
66
* A copy of the License is located at
7-
*
7+
*
88
* http://aws.amazon.com/apache2.0
9-
*
9+
*
1010
* or in the "license" file accompanying this file. This file is distributed
1111
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1212
* express or implied. See the License for the specific language governing
@@ -21,8 +21,11 @@
2121
using System.Text;
2222
using AWS.Lambda.Powertools.Common;
2323
using AWS.Lambda.Powertools.Logging.Internal;
24+
using AWS.Lambda.Powertools.Logging.Tests.Utilities;
2425
using Microsoft.Extensions.Logging;
2526
using NSubstitute;
27+
using NSubstitute.Extensions;
28+
using NSubstitute.ReceivedExtensions;
2629
using Xunit;
2730

2831
namespace AWS.Lambda.Powertools.Logging.Tests
@@ -34,7 +37,7 @@ public PowertoolsLoggerTest()
3437
{
3538
Logger.UseDefaultFormatter();
3639
}
37-
40+
3841
private static void Log_WhenMinimumLevelIsBelowLogLevel_Logs(LogLevel logLevel, LogLevel minimumLevel)
3942
{
4043
// Arrange
@@ -1219,7 +1222,7 @@ public void Log_Set_Execution_Environment_Context()
12191222
$"{Constants.FeatureContextIdentifier}/Logger/{assemblyVersion}");
12201223
env.Received(1).GetEnvironmentVariable("AWS_EXECUTION_ENV");
12211224
}
1222-
1225+
12231226
[Fact]
12241227
public void Log_Should_Serialize_DateOnly()
12251228
{
@@ -1260,7 +1263,7 @@ public void Log_Should_Serialize_DateOnly()
12601263
)
12611264
);
12621265
}
1263-
1266+
12641267
[Fact]
12651268
public void Log_Should_Serialize_TimeOnly()
12661269
{
@@ -1301,5 +1304,74 @@ public void Log_Should_Serialize_TimeOnly()
13011304
)
13021305
);
13031306
}
1307+
1308+
[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)
1312+
{
1313+
// Arrange
1314+
var loggerName = Guid.NewGuid().ToString();
1315+
1316+
var environment = Substitute.For<IPowertoolsEnvironment>();
1317+
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns("Error");
1318+
environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_LEVEL").Returns(logLevel.ToString());
1319+
1320+
var systemWrapper = new SystemWrapperMock(environment);
1321+
var configuration = new PowertoolsConfigurations(systemWrapper);
1322+
1323+
var logger = new PowertoolsLogger(loggerName, configuration, systemWrapper, () =>
1324+
new LoggerConfiguration
1325+
{
1326+
LoggerOutputCase = LoggerOutputCase.CamelCase
1327+
});
1328+
1329+
var message = new
1330+
{
1331+
PropOne = "Value 1",
1332+
PropTwo = "Value 2",
1333+
Time = new TimeOnly(12, 0, 0)
1334+
};
1335+
1336+
logger.LogWarning(message);
1337+
1338+
Assert.True(logger.IsEnabled(logLevel));
1339+
Assert.Equal(logLevel.ToString(), configuration.LogLevel);
1340+
Assert.Equal(willLog, systemWrapper.LogMethodCalled);
1341+
}
1342+
1343+
[Theory]
1344+
[InlineData(true, LogLevel.Warning)]
1345+
[InlineData(false, LogLevel.Critical)]
1346+
public void Log_Should_Use_Powertools_Log_Level_When_Lambda_Log_Level_Unavailable(bool willLog, LogLevel logLevel)
1347+
{
1348+
// Arrange
1349+
var loggerName = Guid.NewGuid().ToString();
1350+
var environment = Substitute.For<IPowertoolsEnvironment>();
1351+
environment.GetEnvironmentVariable("POWERTOOLS_LOG_LEVEL").Returns(logLevel.ToString());
1352+
1353+
var systemWrapper = new SystemWrapperMock(environment);
1354+
var configuration = new PowertoolsConfigurations(systemWrapper);
1355+
1356+
var logger = new PowertoolsLogger(loggerName, configuration, systemWrapper, () =>
1357+
new LoggerConfiguration
1358+
{
1359+
LoggerOutputCase = LoggerOutputCase.CamelCase
1360+
});
1361+
1362+
var message = new
1363+
{
1364+
PropOne = "Value 1",
1365+
PropTwo = "Value 2",
1366+
Time = new TimeOnly(12, 0, 0)
1367+
};
1368+
1369+
logger.LogWarning(message);
1370+
1371+
// Assert
1372+
Assert.True(logger.IsEnabled(logLevel));
1373+
Assert.Equal(logLevel.ToString(), configuration.LogLevel);
1374+
Assert.Equal(willLog, systemWrapper.LogMethodCalled);
1375+
}
13041376
}
13051377
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using AWS.Lambda.Powertools.Common;
17+
18+
namespace AWS.Lambda.Powertools.Logging.Tests.Utilities;
19+
20+
public class SystemWrapperMock : ISystemWrapper
21+
{
22+
private readonly IPowertoolsEnvironment _powertoolsEnvironment;
23+
public bool LogMethodCalled { get; private set; }
24+
25+
public SystemWrapperMock(IPowertoolsEnvironment powertoolsEnvironment)
26+
{
27+
_powertoolsEnvironment = powertoolsEnvironment;
28+
}
29+
30+
public string GetEnvironmentVariable(string variable)
31+
{
32+
return _powertoolsEnvironment.GetEnvironmentVariable(variable);
33+
}
34+
35+
public void Log(string value)
36+
{
37+
LogMethodCalled = true;
38+
}
39+
40+
public void LogLine(string value)
41+
{
42+
LogMethodCalled = true;
43+
}
44+
45+
public double GetRandom()
46+
{
47+
throw new System.NotImplementedException();
48+
}
49+
50+
public void SetEnvironmentVariable(string variable, string value)
51+
{
52+
throw new System.NotImplementedException();
53+
}
54+
55+
public void SetExecutionEnvironment<T>(T type)
56+
{
57+
}
58+
}

0 commit comments

Comments
 (0)