Skip to content

Commit 7a94aae

Browse files
authored
Merge pull request #58 from aquality-automation/log4j_update
Log4j update
2 parents db30415 + b99ce3c commit 7a94aae

File tree

3 files changed

+69
-38
lines changed

3 files changed

+69
-38
lines changed

pom.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.aquality-automation</groupId>
88
<artifactId>aquality-selenium-core</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010

1111
<packaging>jar</packaging>
1212
<name>Aquality Selenium Core</name>
@@ -68,13 +68,23 @@
6868
<id>knysh</id>
6969
<name>Sergey Knysh</name>
7070
</developer>
71+
<developer>
72+
<id>runets</id>
73+
<name>Kiril Runets</name>
74+
</developer>
7175
</developers>
7276

7377
<dependencies>
7478
<dependency>
75-
<groupId>log4j</groupId>
79+
<groupId>org.apache.logging.log4j</groupId>
7680
<artifactId>log4j</artifactId>
77-
<version>1.2.17</version>
81+
<version>2.15.0</version>
82+
<type>pom</type>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.apache.logging.log4j</groupId>
86+
<artifactId>log4j-api</artifactId>
87+
<version>2.15.0</version>
7888
</dependency>
7989
<dependency>
8090
<groupId>org.testng</groupId>
@@ -115,6 +125,11 @@
115125
<version>7.2.0</version>
116126
<scope>test</scope>
117127
</dependency>
128+
<dependency>
129+
<groupId>org.apache.logging.log4j</groupId>
130+
<artifactId>log4j-core</artifactId>
131+
<version>2.15.0</version>
132+
</dependency>
118133
</dependencies>
119134

120135
<build>

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
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() {
1517
}
1618

19+
public void removeLog4jLocalThread() {
20+
log4J.remove();
21+
}
22+
1723
/**
1824
* Gets logger instance
1925
*
@@ -30,7 +36,8 @@ public static Logger getInstance() {
3036
* @return logger instance
3137
*/
3238
public Logger addAppender(Appender appender) {
33-
log4J.get().addAppender(appender);
39+
appender.start();
40+
LoggerContext.getContext(false).getRootLogger().addAppender(appender);
3441
return getInstance();
3542
}
3643

@@ -41,7 +48,8 @@ public Logger addAppender(Appender appender) {
4148
* @return logger instance
4249
*/
4350
public Logger removeAppender(Appender appender) {
44-
log4J.get().removeAppender(appender);
51+
appender.stop();
52+
LoggerContext.getContext(false).getRootLogger().removeAppender(appender);
4553
return getInstance();
4654
}
4755

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
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.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;
712
import org.openqa.selenium.By;
813
import org.openqa.selenium.NoSuchElementException;
914
import 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

Comments
 (0)