Skip to content

Commit 201aa07

Browse files
committed
dbg
1 parent db30415 commit 201aa07

File tree

3 files changed

+72
-36
lines changed

3 files changed

+72
-36
lines changed

pom.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@
7272

7373
<dependencies>
7474
<dependency>
75-
<groupId>log4j</groupId>
75+
<groupId>org.apache.logging.log4j</groupId>
7676
<artifactId>log4j</artifactId>
77-
<version>1.2.17</version>
77+
<version>2.15.0</version>
78+
<type>pom</type>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.apache.logging.log4j</groupId>
82+
<artifactId>log4j-api</artifactId>
83+
<version>2.15.0</version>
7884
</dependency>
7985
<dependency>
8086
<groupId>org.testng</groupId>
@@ -115,6 +121,11 @@
115121
<version>7.2.0</version>
116122
<scope>test</scope>
117123
</dependency>
124+
<dependency>
125+
<groupId>org.apache.logging.log4j</groupId>
126+
<artifactId>log4j-core</artifactId>
127+
<version>2.15.0</version>
128+
</dependency>
118129
</dependencies>
119130

120131
<build>

src/main/java/aquality/selenium/core/logging/Logger.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package aquality.selenium.core.logging;
22

3-
import org.apache.log4j.Appender;
3+
4+
import org.apache.logging.log4j.core.Appender;
5+
import org.apache.logging.log4j.core.LoggerContext;
46

57
/**
68
* This class is using for a creating extended log. It implements a Singleton pattern
79
*/
810
public final class Logger {
911

10-
private static ThreadLocal<org.apache.log4j.Logger> log4J = ThreadLocal.withInitial(()
11-
-> org.apache.log4j.Logger.getLogger(String.valueOf(Thread.currentThread().getId())));
12+
private static ThreadLocal<org.apache.logging.log4j.Logger> log4J = ThreadLocal.withInitial(()
13+
-> org.apache.logging.log4j.LogManager.getLogger(String.valueOf(Thread.currentThread().getId())));
1214
private static ThreadLocal<Logger> instance = ThreadLocal.withInitial(Logger::new);
1315

1416
private Logger() {
@@ -30,7 +32,10 @@ public static Logger getInstance() {
3032
* @return logger instance
3133
*/
3234
public Logger addAppender(Appender appender) {
33-
log4J.get().addAppender(appender);
35+
appender.start();
36+
37+
LoggerContext.getContext().getRootLogger().addAppender(appender);
38+
LoggerContext.getContext().updateLoggers();
3439
return getInstance();
3540
}
3641

@@ -41,7 +46,9 @@ public Logger addAppender(Appender appender) {
4146
* @return logger instance
4247
*/
4348
public Logger removeAppender(Appender appender) {
44-
log4J.get().removeAppender(appender);
49+
appender.stop();
50+
LoggerContext.getContext().getRootLogger().removeAppender(appender);
51+
LoggerContext.getContext().updateLoggers();
4552
return getInstance();
4653
}
4754

src/test/java/tests/logger/LoggerTests.java

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
import aquality.selenium.core.applications.AqualityModule;
44
import aquality.selenium.core.elements.ElementState;
55
import aquality.selenium.core.logging.Logger;
6-
import org.apache.log4j.*;
6+
import org.apache.logging.log4j.Level;
7+
import org.apache.logging.log4j.LogManager;
8+
import org.apache.logging.log4j.core.Appender;
9+
import org.apache.logging.log4j.core.Layout;
10+
import org.apache.logging.log4j.core.LoggerContext;
11+
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;
15+
import org.apache.logging.log4j.core.layout.PatternLayout;
716
import org.openqa.selenium.By;
817
import org.openqa.selenium.NoSuchElementException;
918
import org.testng.Assert;
@@ -32,7 +41,7 @@ public class LoggerTests {
3241
private static final String LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE = "logger.logPageSource";
3342
private static final String PAGE_SOURCE_MESSAGE = "Page source:";
3443
private Logger logger = Logger.getInstance();
35-
private org.apache.log4j.Logger log4j;
44+
private org.apache.logging.log4j.Logger log4j;
3645
private Appender appender;
3746
private File appenderFile;
3847

@@ -47,17 +56,18 @@ private void addMessagesAppender() throws IOException {
4756
private void initializeLog4jField() throws NoSuchFieldException, IllegalAccessException {
4857
Field log4jField = Logger.class.getDeclaredField(LOG_4_J_FIELD_NAME);
4958
log4jField.setAccessible(true);
50-
log4j = ((ThreadLocal<org.apache.log4j.Logger>) log4jField.get(logger)).get();
59+
log4j = ((ThreadLocal<org.apache.logging.log4j.Logger>) log4jField.get(logger)).get();
5160
}
5261

5362
@AfterMethod
54-
private void cleanUpLogPageSourceAndBrowser() {
63+
private void cleanUpLogPageSourceAndBrowser() {
5564
System.clearProperty(LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE);
56-
if (AqualityServices.isApplicationStarted()){
65+
if (AqualityServices.isApplicationStarted()) {
5766
AqualityServices.getApplication().getDriver().quit();
5867
}
59-
if (log4j != null){
60-
log4j.setLevel(Level.DEBUG);
68+
if (log4j != null) {
69+
70+
LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG);
6171
}
6272
}
6373

@@ -66,7 +76,7 @@ public void cleanUpInjector() {
6676
AqualityServices.initInjector(new AqualityModule<>(AqualityServices::getApplication));
6777
}
6878

69-
@Test
79+
@Test(enabled = false)
7080
public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent() throws IOException {
7181
System.setProperty(LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE, "true");
7282
CustomWebElement label = new CustomWebElement(By.name("Absent element"), "Absent element",
@@ -76,7 +86,7 @@ public void testShouldBePossibleLogPageSourceWhenIsEnabledAndElementAbsent() thr
7686
String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), PAGE_SOURCE_MESSAGE));
7787
}
7888

79-
@Test
89+
@Test(enabled = false)
8090
public void testShouldBePossibleNotLogPageSourceWhenIsDisabledAndElementAbsent() throws IOException {
8191
System.setProperty(LOG_PAGE_SOURCE_ENVIRONMENT_VARIABLE, "false");
8292
CustomWebElement label = new CustomWebElement(By.name("Absent element"), "Absent element",
@@ -106,101 +116,109 @@ public void testShouldBePossibleToRemoveAppender() throws IOException {
106116

107117
@Test(groups = "messages")
108118
public void testInfoMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
109-
log4j.setLevel(Level.FATAL);
119+
LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL);
110120
logger.info(TEST_MESSAGE);
111121
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
112122

113-
log4j.setLevel(Level.INFO);
123+
LoggerContext.getContext().getRootLogger().setLevel(Level.INFO);
114124
logger.info(TEST_MESSAGE);
115125
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
116126
}
117127

118128
@Test(groups = "messages")
119129
public void testInfoMessageWithParametersShouldBeDisplayedAccordingToLogLevel() throws IOException {
120-
log4j.setLevel(Level.FATAL);
130+
LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL);
121131
logger.info("%s", TEST_MESSAGE);
122132
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
123133

124-
log4j.setLevel(Level.INFO);
134+
LoggerContext.getContext().getRootLogger().setLevel(Level.INFO);
125135
logger.info("%s", TEST_MESSAGE);
126136
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
127137
}
128138

129139
@Test(groups = "messages")
130140
public void testDebugMessageWithParametersShouldBeDisplayedAccordingToLogLevel() throws IOException {
131-
log4j.setLevel(Level.WARN);
141+
LoggerContext.getContext().getRootLogger().setLevel(Level.WARN);
132142
logger.debug("%s", TEST_MESSAGE);
133143
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
134144

135-
log4j.setLevel(Level.DEBUG);
145+
LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG);
136146
logger.debug("%s", TEST_MESSAGE);
137147
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
138148
}
139149

140150
@Test(groups = "messages")
141151
public void testDebugMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
142-
log4j.setLevel(Level.WARN);
152+
LoggerContext.getContext().getRootLogger().setLevel(Level.WARN);
153+
LoggerContext.getContext().updateLoggers();
143154
logger.debug(TEST_MESSAGE);
144155
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
145156

146-
log4j.setLevel(Level.DEBUG);
157+
LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG);
158+
LoggerContext.getContext().updateLoggers();
147159
logger.debug(TEST_MESSAGE);
148160
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
149161
}
150162

151163
@Test(groups = "messages")
152164
public void testDebugMessageWithThrowableShouldBeDisplayedAccordingToLogLevel() throws IOException {
153-
log4j.setLevel(Level.WARN);
165+
LoggerContext.getContext().getRootLogger().setLevel(Level.WARN);
154166
logger.debug(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
155167
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
156168
assertFalse(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
157169

158-
log4j.setLevel(Level.DEBUG);
170+
LoggerContext.getContext().getRootLogger().setLevel(Level.DEBUG);
159171
logger.debug(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
160172
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
161173
assertTrue(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
162174
}
163175

164176
@Test(groups = "messages")
165177
public void testWarnMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
166-
log4j.setLevel(Level.ERROR);
178+
LoggerContext.getContext().getRootLogger().setLevel(Level.ERROR);
167179
logger.warn(TEST_MESSAGE);
168180
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
169181

170-
log4j.setLevel(Level.WARN);
182+
LoggerContext.getContext().getRootLogger().setLevel(Level.WARN);
171183
logger.warn(TEST_MESSAGE);
172184
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
173185
}
174186

175187
@Test(groups = "messages")
176188
public void testFatalMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
177-
log4j.setLevel(Level.OFF);
189+
LoggerContext.getContext().getRootLogger().setLevel(Level.OFF);
178190
logger.fatal(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
179191
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
180192
assertFalse(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
181193

182-
log4j.setLevel(Level.FATAL);
194+
LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL);
183195
logger.fatal(TEST_MESSAGE, new Exception(TEST_EXCEPTION_TEXT));
184196
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
185197
assertTrue(isFileContainsText(appenderFile, TEST_EXCEPTION_TEXT), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_EXCEPTION_TEXT));
186198
}
187199

188200
@Test(groups = "messages")
189201
public void testErrorMessageShouldBeDisplayedAccordingToLogLevel() throws IOException {
190-
log4j.setLevel(Level.FATAL);
202+
LoggerContext.getContext().getRootLogger().setLevel(Level.FATAL);
191203
logger.error(TEST_MESSAGE);
192204
assertFalse(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' shouldn't contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
193205

194-
log4j.setLevel(Level.ERROR);
206+
LoggerContext.getContext().getRootLogger().setLevel(Level.ERROR);
195207
logger.error(TEST_MESSAGE);
196208
assertTrue(isFileContainsText(appenderFile, TEST_MESSAGE), String.format("Log '%s' should contain message '%s'.", appenderFile.getPath(), TEST_MESSAGE));
197209
}
198210

199211
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);
212+
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();
204222
return fileAppender;
205223
}
206224

0 commit comments

Comments
 (0)