33import aquality .selenium .core .applications .AqualityModule ;
44import aquality .selenium .core .elements .ElementState ;
55import aquality .selenium .core .logging .Logger ;
6- import org .apache .log4j .*;
6+ import org .apache .logging .log4j .Level ;
7+ import org .apache .logging .log4j .core .Appender ;
8+ import org .apache .logging .log4j .core .Layout ;
9+ import org .apache .logging .log4j .core .appender .FileAppender ;
10+ import org .apache .logging .log4j .core .config .Configurator ;
11+ import org .apache .logging .log4j .core .layout .PatternLayout ;
712import org .openqa .selenium .By ;
813import org .openqa .selenium .NoSuchElementException ;
914import org .testng .Assert ;
@@ -32,7 +37,7 @@ public class LoggerTests {
3237 private static final String LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE = "logger.logPageSource" ;
3338 private static final String PAGE_SOURCE_MESSAGE = "Page source:" ;
3439 private Logger logger = Logger .getInstance ();
35- private org .apache .log4j .Logger log4j ;
40+ private org .apache .logging . log4j .Logger log4j ;
3641 private Appender appender ;
3742 private File appenderFile ;
3843
@@ -47,17 +52,17 @@ private void addMessagesAppender() throws IOException {
4752 private void initializeLog4jField () throws NoSuchFieldException , IllegalAccessException {
4853 Field log4jField = Logger .class .getDeclaredField (LOG_4_J_FIELD_NAME );
4954 log4jField .setAccessible (true );
50- log4j = ((ThreadLocal <org .apache .log4j .Logger >) log4jField .get (logger )).get ();
55+ log4j = ((ThreadLocal <org .apache .logging . log4j .Logger >) log4jField .get (logger )).get ();
5156 }
5257
5358 @ AfterMethod
54- private void cleanUpLogPageSourceAndBrowser () {
59+ private void cleanUpLogPageSourceAndBrowser () {
5560 System .clearProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE );
56- if (AqualityServices .isApplicationStarted ()){
61+ if (AqualityServices .isApplicationStarted ()) {
5762 AqualityServices .getApplication ().getDriver ().quit ();
5863 }
59- if (log4j != null ){
60- log4j . setLevel (Level .DEBUG );
64+ if (log4j != null ) {
65+ Configurator . setRootLevel (Level .DEBUG );
6166 }
6267 }
6368
@@ -66,7 +71,7 @@ public void cleanUpInjector() {
6671 AqualityServices .initInjector (new AqualityModule <>(AqualityServices ::getApplication ));
6772 }
6873
69- @ Test
74+ @ Test ( enabled = false )
7075 public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent () throws IOException {
7176 System .setProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE , "true" );
7277 CustomWebElement label = new CustomWebElement (By .name ("Absent element" ), "Absent element" ,
@@ -76,7 +81,7 @@ public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent() thr
7681 String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), PAGE_SOURCE_MESSAGE ));
7782 }
7883
79- @ Test
84+ @ Test ( enabled = false )
8085 public void testShouldBePossibleNotLogPageSourceWhenIsDisabledAndElementAbsent () throws IOException {
8186 System .setProperty (LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE , "false" );
8287 CustomWebElement label = new CustomWebElement (By .name ("Absent element" ), "Absent element" ,
@@ -106,101 +111,104 @@ public void testShouldBePossibleToRemoveAppender() throws IOException {
106111
107112 @ Test (groups = "messages" )
108113 public void testInfoMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
109- log4j . setLevel (Level .FATAL );
114+ Configurator . setRootLevel (Level .FATAL );
110115 logger .info (TEST_MESSAGE );
111116 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
112117
113- log4j . setLevel (Level .INFO );
118+ Configurator . setRootLevel (Level .INFO );
114119 logger .info (TEST_MESSAGE );
115120 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
116121 }
117122
118123 @ Test (groups = "messages" )
119124 public void testInfoMessageWithParametersShouldBeDisplayedAccordingToLogLevel () throws IOException {
120- log4j . setLevel (Level .FATAL );
125+ Configurator . setRootLevel (Level .FATAL );
121126 logger .info ("%s" , TEST_MESSAGE );
122127 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
123128
124- log4j . setLevel (Level .INFO );
129+ Configurator . setRootLevel (Level .INFO );
125130 logger .info ("%s" , TEST_MESSAGE );
126131 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
127132 }
128133
129134 @ Test (groups = "messages" )
130135 public void testDebugMessageWithParametersShouldBeDisplayedAccordingToLogLevel () throws IOException {
131- log4j . setLevel (Level .WARN );
136+ Configurator . setRootLevel (Level .WARN );
132137 logger .debug ("%s" , TEST_MESSAGE );
133138 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
134139
135- log4j . setLevel (Level .DEBUG );
140+ Configurator . setRootLevel (Level .DEBUG );
136141 logger .debug ("%s" , TEST_MESSAGE );
137142 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
138143 }
139144
140145 @ Test (groups = "messages" )
141146 public void testDebugMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
142- log4j . setLevel (Level .WARN );
143- logger .debug (TEST_MESSAGE );
147+ Configurator . setRootLevel (Level .WARN );
148+ logger .debug (TEST_MESSAGE , new Exception ( TEST_EXCEPTION_TEXT ) );
144149 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
150+ assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
145151
146- log4j . setLevel (Level .DEBUG );
152+ Configurator . setRootLevel (Level .DEBUG );
147153 logger .debug (TEST_MESSAGE );
148154 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
149155 }
150156
151157 @ Test (groups = "messages" )
152158 public void testDebugMessageWithThrowableShouldBeDisplayedAccordingToLogLevel () throws IOException {
153- log4j . setLevel (Level .WARN );
159+ Configurator . setRootLevel (Level .WARN );
154160 logger .debug (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
155161 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
156162 assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
157163
158- log4j . setLevel (Level .DEBUG );
164+ Configurator . setRootLevel (Level .DEBUG );
159165 logger .debug (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
160166 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
161167 assertTrue (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
162168 }
163169
164170 @ Test (groups = "messages" )
165171 public void testWarnMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
166- log4j . setLevel (Level .ERROR );
172+ Configurator . setRootLevel (Level .ERROR );
167173 logger .warn (TEST_MESSAGE );
168174 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
169175
170- log4j . setLevel (Level .WARN );
176+ Configurator . setRootLevel (Level .WARN );
171177 logger .warn (TEST_MESSAGE );
172178 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
173179 }
174180
175181 @ Test (groups = "messages" )
176182 public void testFatalMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
177- log4j . setLevel (Level .OFF );
183+ Configurator . setRootLevel (Level .OFF );
178184 logger .fatal (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
179185 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
180186 assertFalse (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
181187
182- log4j . setLevel (Level .FATAL );
188+ Configurator . setRootLevel (Level .FATAL );
183189 logger .fatal (TEST_MESSAGE , new Exception (TEST_EXCEPTION_TEXT ));
184190 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
185191 assertTrue (isFileContainsText (appenderFile , TEST_EXCEPTION_TEXT ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_EXCEPTION_TEXT ));
186192 }
187193
188194 @ Test (groups = "messages" )
189195 public void testErrorMessageShouldBeDisplayedAccordingToLogLevel () throws IOException {
190- log4j . setLevel (Level .FATAL );
196+ Configurator . setRootLevel (Level .FATAL );
191197 logger .error (TEST_MESSAGE );
192198 assertFalse (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' shouldn't contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
193199
194- log4j . setLevel (Level .ERROR );
200+ Configurator . setRootLevel (Level .ERROR );
195201 logger .error (TEST_MESSAGE );
196202 assertTrue (isFileContainsText (appenderFile , TEST_MESSAGE ), String .format ("Log '%s' should contain message '%s'." , appenderFile .getPath (), TEST_MESSAGE ));
197203 }
198204
199205 private Appender getFileAppender (File file ) throws IOException {
200- Layout layout = new PatternLayout ("%m%n" );
201- RollingFileAppender fileAppender = new RollingFileAppender (layout , file .getPath ());
202- fileAppender .setName ("test" );
203- fileAppender .setAppend (true );
206+ Layout layout = PatternLayout .newBuilder ().withPattern ("%m%n" ).build ();
207+ FileAppender fileAppender = FileAppender .newBuilder ().setName ("test" )
208+ .setLayout (layout )
209+ .withFileName (file .getPath ())
210+ .withAppend (true )
211+ .build ();
204212 return fileAppender ;
205213 }
206214
0 commit comments