|
4 | 4 | import aquality.selenium.core.elements.ElementState;
|
5 | 5 | import aquality.selenium.core.logging.Logger;
|
6 | 6 | import org.apache.logging.log4j.Level;
|
7 |
| -import org.apache.logging.log4j.LogManager; |
8 | 7 | import org.apache.logging.log4j.core.Appender;
|
9 | 8 | import org.apache.logging.log4j.core.Layout;
|
10 |
| -import org.apache.logging.log4j.core.LoggerContext; |
11 | 9 | import org.apache.logging.log4j.core.appender.FileAppender;
|
12 |
| -import org.apache.logging.log4j.core.appender.RollingFileAppender; |
13 |
| -import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy; |
14 |
| -import org.apache.logging.log4j.core.config.Configuration; |
| 10 | +import org.apache.logging.log4j.core.config.Configurator; |
15 | 11 | import org.apache.logging.log4j.core.layout.PatternLayout;
|
16 | 12 | import org.openqa.selenium.By;
|
17 | 13 | import org.openqa.selenium.NoSuchElementException;
|
@@ -67,7 +63,7 @@ private void cleanUpLogPageSourceAndBrowser() {
|
67 | 63 | }
|
68 | 64 | if (log4j != null) {
|
69 | 65 |
|
70 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG); |
| 66 | + Configurator.setRootLevel(Level.DEBUG); |
71 | 67 | }
|
72 | 68 | }
|
73 | 69 |
|
@@ -116,109 +112,104 @@ public void testShouldBePossibleToRemoveAppender() throws IOException {
|
116 | 112 |
|
117 | 113 | @Test(groups = "messages")
|
118 | 114 | public void testInfoMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
119 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL); |
| 115 | + Configurator.setRootLevel(Level.FATAL); |
120 | 116 | logger.info(TEST_MESSAGE);
|
121 | 117 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
122 | 118 |
|
123 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.INFO); |
| 119 | + Configurator.setRootLevel(Level.INFO); |
124 | 120 | logger.info(TEST_MESSAGE);
|
125 | 121 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
126 | 122 | }
|
127 | 123 |
|
128 | 124 | @Test(groups = "messages")
|
129 | 125 | public void testInfoMessageWithParametersShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
130 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL); |
| 126 | + Configurator.setRootLevel(Level.FATAL); |
131 | 127 | logger.info("%s", TEST_MESSAGE);
|
132 | 128 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
133 | 129 |
|
134 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.INFO); |
| 130 | + Configurator.setRootLevel(Level.INFO); |
135 | 131 | logger.info("%s", TEST_MESSAGE);
|
136 | 132 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
137 | 133 | }
|
138 | 134 |
|
139 | 135 | @Test(groups = "messages")
|
140 | 136 | public void testDebugMessageWithParametersShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
141 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.WARN); |
| 137 | + Configurator.setRootLevel(Level.WARN); |
142 | 138 | logger.debug("%s", TEST_MESSAGE);
|
143 | 139 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
144 | 140 |
|
145 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG); |
| 141 | + Configurator.setRootLevel(Level.DEBUG); |
146 | 142 | logger.debug("%s", TEST_MESSAGE);
|
147 | 143 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
148 | 144 | }
|
149 | 145 |
|
150 | 146 | @Test(groups = "messages")
|
151 | 147 | public void testDebugMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
152 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.WARN); |
153 |
| - LoggerContext.getContext().updateLoggers(); |
154 |
| - logger.debug(TEST_MESSAGE); |
| 148 | + Configurator.setRootLevel(Level.WARN); |
| 149 | + logger.debug(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT)); |
155 | 150 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
| 151 | + assertFalse(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT)); |
156 | 152 |
|
157 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG); |
158 |
| - LoggerContext.getContext().updateLoggers(); |
| 153 | + Configurator.setRootLevel(Level.DEBUG); |
159 | 154 | logger.debug(TEST_MESSAGE);
|
160 | 155 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
161 | 156 | }
|
162 | 157 |
|
163 | 158 | @Test(groups = "messages")
|
164 | 159 | public void testDebugMessageWithThrowableShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
165 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.WARN); |
| 160 | + Configurator.setRootLevel(Level.WARN); |
166 | 161 | logger.debug(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
|
167 | 162 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
168 | 163 | assertFalse(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
|
169 | 164 |
|
170 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG); |
| 165 | + Configurator.setRootLevel(Level.DEBUG); |
171 | 166 | logger.debug(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
|
172 | 167 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
173 | 168 | assertTrue(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
|
174 | 169 | }
|
175 | 170 |
|
176 | 171 | @Test(groups = "messages")
|
177 | 172 | public void testWarnMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
178 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.ERROR); |
| 173 | + Configurator.setRootLevel(Level.ERROR); |
179 | 174 | logger.warn(TEST_MESSAGE);
|
180 | 175 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
181 | 176 |
|
182 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.WARN); |
| 177 | + Configurator.setRootLevel(Level.WARN); |
183 | 178 | logger.warn(TEST_MESSAGE);
|
184 | 179 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
185 | 180 | }
|
186 | 181 |
|
187 | 182 | @Test(groups = "messages")
|
188 | 183 | public void testFatalMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
189 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.OFF); |
| 184 | + Configurator.setRootLevel(Level.OFF); |
190 | 185 | logger.fatal(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
|
191 | 186 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
192 | 187 | assertFalse(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
|
193 | 188 |
|
194 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL); |
| 189 | + Configurator.setRootLevel(Level.FATAL); |
195 | 190 | logger.fatal(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
|
196 | 191 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
197 | 192 | assertTrue(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
|
198 | 193 | }
|
199 | 194 |
|
200 | 195 | @Test(groups = "messages")
|
201 | 196 | public void testErrorMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
|
202 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL); |
| 197 | + Configurator.setRootLevel(Level.FATAL); |
203 | 198 | logger.error(TEST_MESSAGE);
|
204 | 199 | assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
205 | 200 |
|
206 |
| - LoggerContext.getContext().getRootLogger().setLevel(Level.ERROR); |
| 201 | + Configurator.setRootLevel(Level.ERROR); |
207 | 202 | logger.error(TEST_MESSAGE);
|
208 | 203 | assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
|
209 | 204 | }
|
210 | 205 |
|
211 | 206 | private Appender getFileAppender(File file) throws IOException {
|
212 | 207 | Layout layout = PatternLayout.newBuilder().withPattern("%m%n").build();
|
213 |
| - final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); |
214 |
| - final Configuration config = ctx.getConfiguration(); |
215 |
| - FileAppender fileAppender = FileAppender.createAppender(file.getPath(),"true","false","test","true", |
216 |
| - "false", "false", "4000", layout, null, "false", null, config); |
217 |
| -// FileAppender fileAppender = FileAppender.newBuilder().setName("test") |
218 |
| -// .setLayout(layout) |
219 |
| -// .withFileName(file.getPath()) |
220 |
| -// .withAppend(true) |
221 |
| -// .build(); |
| 208 | + FileAppender fileAppender = FileAppender.newBuilder().setName("test") |
| 209 | + .setLayout(layout) |
| 210 | + .withFileName(file.getPath()) |
| 211 | + .withAppend(true) |
| 212 | + .build(); |
222 | 213 | return fileAppender;
|
223 | 214 | }
|
224 | 215 |
|
|
0 commit comments