46
46
import java .io .ByteArrayOutputStream ;
47
47
import java .io .File ;
48
48
import java .io .IOException ;
49
+ import java .io .PrintStream ;
50
+ import java .io .UnsupportedEncodingException ;
49
51
import java .lang .reflect .InvocationTargetException ;
50
52
import java .lang .reflect .Method ;
51
53
import java .nio .channels .FileChannel ;
54
56
import java .nio .file .NoSuchFileException ;
55
57
import java .nio .file .Paths ;
56
58
import java .nio .file .StandardOpenOption ;
59
+ import java .util .ArrayList ;
57
60
import java .util .Collections ;
61
+ import java .util .List ;
58
62
import org .junit .jupiter .api .AfterEach ;
59
63
import org .junit .jupiter .api .Assertions ;
60
64
import org .junit .jupiter .api .BeforeEach ;
@@ -108,7 +112,7 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
108
112
resetLogLevel (Level .INFO );
109
113
writeStaticField (LoggingConstants .class , "LAMBDA_LOG_LEVEL" , null , true );
110
114
writeStaticField (LoggingConstants .class , "POWERTOOLS_LOG_LEVEL" , null , true );
111
- writeStaticField (LoggingConstants .class , "POWERTOOLS_LOG_EVENT" , null , true );
115
+ writeStaticField (LoggingConstants .class , "POWERTOOLS_LOG_EVENT" , false , true );
112
116
writeStaticField (LoggingConstants .class , "POWERTOOLS_SAMPLING_RATE" , null , true );
113
117
try {
114
118
FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
@@ -475,7 +479,7 @@ void shouldLogEventForHandlerWithLogEventAnnotation() {
475
479
void shouldLogEventForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
476
480
try {
477
481
// GIVEN
478
- LoggingConstants .POWERTOOLS_LOG_EVENT = " true" ;
482
+ LoggingConstants .POWERTOOLS_LOG_EVENT = true ;
479
483
480
484
requestHandler = new PowertoolsLogEnabled ();
481
485
@@ -491,14 +495,14 @@ void shouldLogEventForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessExce
491
495
File logFile = new File ("target/logfile.json" );
492
496
assertThat (contentOf (logFile )).contains ("\" body\" :\" body\" " ).contains ("\" messageId\" :\" 1234abcd\" " ).contains ("\" awsRegion\" :\" eu-west-1\" " );
493
497
} finally {
494
- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_EVENT" , "false" , true ) ;
498
+ LoggingConstants .POWERTOOLS_LOG_EVENT = false ;
495
499
}
496
500
}
497
501
498
502
@ Test
499
503
void shouldNotLogEventForHandlerWhenEnvVariableSetToFalse () throws IOException {
500
504
// GIVEN
501
- LoggingConstants .POWERTOOLS_LOG_EVENT = " false" ;
505
+ LoggingConstants .POWERTOOLS_LOG_EVENT = false ;
502
506
503
507
// WHEN
504
508
requestHandler = new PowertoolsLogEventDisabled ();
@@ -543,7 +547,7 @@ void shouldLogResponseForHandlerWithLogResponseAnnotation() {
543
547
void shouldLogResponseForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
544
548
try {
545
549
// GIVEN
546
- LoggingConstants .POWERTOOLS_LOG_RESPONSE = " true" ;
550
+ LoggingConstants .POWERTOOLS_LOG_RESPONSE = true ;
547
551
548
552
requestHandler = new PowertoolsLogEnabled ();
549
553
@@ -554,7 +558,7 @@ void shouldLogResponseForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessE
554
558
File logFile = new File ("target/logfile.json" );
555
559
assertThat (contentOf (logFile )).contains ("Bonjour le monde" );
556
560
} finally {
557
- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_RESPONSE" , "false" , true ) ;
561
+ LoggingConstants .POWERTOOLS_LOG_RESPONSE = false ;
558
562
}
559
563
}
560
564
@@ -597,7 +601,7 @@ void shouldLogErrorForHandlerWithLogErrorAnnotation() {
597
601
void shouldLogErrorForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
598
602
try {
599
603
// GIVEN
600
- LoggingConstants .POWERTOOLS_LOG_ERROR = " true" ;
604
+ LoggingConstants .POWERTOOLS_LOG_ERROR = true ;
601
605
602
606
requestHandler = new PowertoolsLogEnabled (true );
603
607
@@ -611,7 +615,7 @@ void shouldLogErrorForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessExce
611
615
File logFile = new File ("target/logfile.json" );
612
616
assertThat (contentOf (logFile )).contains ("Something went wrong" );
613
617
} finally {
614
- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_ERROR" , "false" , true ) ;
618
+ LoggingConstants .POWERTOOLS_LOG_ERROR = false ;
615
619
}
616
620
}
617
621
@@ -694,6 +698,48 @@ void shouldLogCorrelationIdOnAppSyncEvent() throws IOException {
694
698
.containsEntry ("correlation_id" , eventId );
695
699
}
696
700
701
+ @ Test
702
+ void testMultipleLoggingManagers_shouldWarnAndSelectFirstOne () throws UnsupportedEncodingException {
703
+ // GIVEN
704
+ List <LoggingManager > list = new ArrayList <>();
705
+ list .add (new TestLoggingManager ());
706
+ list .add (new DefautlLoggingManager ());
707
+
708
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
709
+ PrintStream stream = new PrintStream (outputStream );
710
+
711
+ // WHEN
712
+ LambdaLoggingAspect .getLoggingManager (list , stream );
713
+
714
+ // THEN
715
+ String output = outputStream .toString ("UTF-8" );
716
+ assertThat (output )
717
+ .contains ("WARN. Multiple LoggingManagers were found on the classpath" )
718
+ .contains ("WARN. Make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies" )
719
+ .contains ("WARN. Using the first LoggingManager found on the classpath: [" + list .get (0 ) + "]" );
720
+ }
721
+
722
+ @ Test
723
+ void testNoLoggingManagers_shouldWarnAndCreateDefault () throws UnsupportedEncodingException {
724
+ // GIVEN
725
+ List <LoggingManager > list = new ArrayList <>();
726
+
727
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
728
+ PrintStream stream = new PrintStream (outputStream );
729
+
730
+ // WHEN
731
+ LoggingManager loggingManager = LambdaLoggingAspect .getLoggingManager (list , stream );
732
+
733
+ // THEN
734
+ String output = outputStream .toString ("UTF-8" );
735
+ assertThat (output )
736
+ .contains ("ERROR. No LoggingManager was found on the classpath" )
737
+ .contains ("ERROR. Applying default LoggingManager: POWERTOOLS_LOG_LEVEL variable is ignored" )
738
+ .contains ("ERROR. Make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies" );
739
+
740
+ assertThat (loggingManager ).isExactlyInstanceOf (DefautlLoggingManager .class );
741
+ }
742
+
697
743
private void setupContext () {
698
744
when (context .getFunctionName ()).thenReturn ("testFunction" );
699
745
when (context .getInvokedFunctionArn ()).thenReturn ("testArn" );
0 commit comments