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
2121using System . Text ;
2222using AWS . Lambda . Powertools . Common ;
2323using AWS . Lambda . Powertools . Logging . Internal ;
24+ using AWS . Lambda . Powertools . Logging . Tests . Utilities ;
2425using Microsoft . Extensions . Logging ;
2526using NSubstitute ;
27+ using NSubstitute . Extensions ;
28+ using NSubstitute . ReceivedExtensions ;
2629using Xunit ;
2730
2831namespace 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}
0 commit comments