4646import java .io .ByteArrayOutputStream ;
4747import java .io .File ;
4848import java .io .IOException ;
49+ import java .io .PrintStream ;
50+ import java .io .UnsupportedEncodingException ;
4951import java .lang .reflect .InvocationTargetException ;
5052import java .lang .reflect .Method ;
5153import java .nio .channels .FileChannel ;
5456import java .nio .file .NoSuchFileException ;
5557import java .nio .file .Paths ;
5658import java .nio .file .StandardOpenOption ;
59+ import java .util .ArrayList ;
5760import java .util .Collections ;
61+ import java .util .List ;
5862import org .junit .jupiter .api .AfterEach ;
5963import org .junit .jupiter .api .Assertions ;
6064import org .junit .jupiter .api .BeforeEach ;
@@ -108,7 +112,7 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
108112 resetLogLevel (Level .INFO );
109113 writeStaticField (LoggingConstants .class , "LAMBDA_LOG_LEVEL" , null , true );
110114 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 );
112116 writeStaticField (LoggingConstants .class , "POWERTOOLS_SAMPLING_RATE" , null , true );
113117 try {
114118 FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
@@ -475,7 +479,7 @@ void shouldLogEventForHandlerWithLogEventAnnotation() {
475479 void shouldLogEventForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
476480 try {
477481 // GIVEN
478- LoggingConstants .POWERTOOLS_LOG_EVENT = " true" ;
482+ LoggingConstants .POWERTOOLS_LOG_EVENT = true ;
479483
480484 requestHandler = new PowertoolsLogEnabled ();
481485
@@ -491,14 +495,14 @@ void shouldLogEventForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessExce
491495 File logFile = new File ("target/logfile.json" );
492496 assertThat (contentOf (logFile )).contains ("\" body\" :\" body\" " ).contains ("\" messageId\" :\" 1234abcd\" " ).contains ("\" awsRegion\" :\" eu-west-1\" " );
493497 } finally {
494- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_EVENT" , "false" , true ) ;
498+ LoggingConstants .POWERTOOLS_LOG_EVENT = false ;
495499 }
496500 }
497501
498502 @ Test
499503 void shouldNotLogEventForHandlerWhenEnvVariableSetToFalse () throws IOException {
500504 // GIVEN
501- LoggingConstants .POWERTOOLS_LOG_EVENT = " false" ;
505+ LoggingConstants .POWERTOOLS_LOG_EVENT = false ;
502506
503507 // WHEN
504508 requestHandler = new PowertoolsLogEventDisabled ();
@@ -543,7 +547,7 @@ void shouldLogResponseForHandlerWithLogResponseAnnotation() {
543547 void shouldLogResponseForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
544548 try {
545549 // GIVEN
546- LoggingConstants .POWERTOOLS_LOG_RESPONSE = " true" ;
550+ LoggingConstants .POWERTOOLS_LOG_RESPONSE = true ;
547551
548552 requestHandler = new PowertoolsLogEnabled ();
549553
@@ -554,7 +558,7 @@ void shouldLogResponseForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessE
554558 File logFile = new File ("target/logfile.json" );
555559 assertThat (contentOf (logFile )).contains ("Bonjour le monde" );
556560 } finally {
557- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_RESPONSE" , "false" , true ) ;
561+ LoggingConstants .POWERTOOLS_LOG_RESPONSE = false ;
558562 }
559563 }
560564
@@ -597,7 +601,7 @@ void shouldLogErrorForHandlerWithLogErrorAnnotation() {
597601 void shouldLogErrorForHandlerWhenEnvVariableSetToTrue () throws IllegalAccessException {
598602 try {
599603 // GIVEN
600- LoggingConstants .POWERTOOLS_LOG_ERROR = " true" ;
604+ LoggingConstants .POWERTOOLS_LOG_ERROR = true ;
601605
602606 requestHandler = new PowertoolsLogEnabled (true );
603607
@@ -611,7 +615,7 @@ void shouldLogErrorForHandlerWhenEnvVariableSetToTrue() throws IllegalAccessExce
611615 File logFile = new File ("target/logfile.json" );
612616 assertThat (contentOf (logFile )).contains ("Something went wrong" );
613617 } finally {
614- writeStaticField ( LoggingConstants .class , " POWERTOOLS_LOG_ERROR" , "false" , true ) ;
618+ LoggingConstants .POWERTOOLS_LOG_ERROR = false ;
615619 }
616620 }
617621
@@ -694,6 +698,48 @@ void shouldLogCorrelationIdOnAppSyncEvent() throws IOException {
694698 .containsEntry ("correlation_id" , eventId );
695699 }
696700
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+
697743 private void setupContext () {
698744 when (context .getFunctionName ()).thenReturn ("testFunction" );
699745 when (context .getInvokedFunctionArn ()).thenReturn ("testArn" );
0 commit comments