Skip to content

Commit dc4114f

Browse files
Improve logging errors (#13327)
* Refactored log capture and formatting code in org.openqa.selenium.testing and org.openqa.selenium.grid.log packages. * Add logging capability to various classes across the project The commit introduces a Logger in various classes to handle exceptions more efficiently. Instead of simply outputting exceptions via printStackTrace, they are now logged using java.util.logging.Logger. This improvement allows for enhanced understanding and handling of exception occurrences during execution. --------- Co-authored-by: Diego Molina <[email protected]>
1 parent e01be4a commit dc4114f

File tree

8 files changed

+32
-13
lines changed

8 files changed

+32
-13
lines changed

java/src/org/openqa/selenium/grid/Bootstrap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static void runMain(ClassLoader loader, String[] args) {
7676
Method main = clazz.getMethod("main", String[].class);
7777
main.invoke(null, new Object[] {args});
7878
} catch (ReflectiveOperationException e) {
79-
e.printStackTrace();
79+
LOG.severe("Error during execution: " + e.getMessage());
8080
System.exit(1);
8181
}
8282
}

java/src/org/openqa/selenium/grid/jmx/JMXHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
package org.openqa.selenium.grid.jmx;
1919

2020
import java.lang.management.ManagementFactory;
21+
import java.util.logging.Logger;
2122
import javax.management.InstanceAlreadyExistsException;
2223
import javax.management.MBeanServer;
2324
import javax.management.ObjectName;
2425

2526
public class JMXHelper {
2627

28+
private static final Logger LOG = Logger.getLogger(JMXHelper.class.getName());
29+
2730
public MBean register(Object bean) {
2831
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
2932
MBean mBean = new MBean(bean);
@@ -33,7 +36,7 @@ public MBean register(Object bean) {
3336
} catch (InstanceAlreadyExistsException t) {
3437
return mBean;
3538
} catch (Throwable t) {
36-
t.printStackTrace();
39+
LOG.severe("Error during execution: " + t.getMessage());
3740
return null;
3841
}
3942
}

java/src/org/openqa/selenium/grid/jmx/MBean.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.logging.Logger;
2526
import java.util.stream.Collectors;
2627
import java.util.stream.Stream;
2728
import javax.management.Attribute;
@@ -36,6 +37,7 @@
3637

3738
public class MBean implements DynamicMBean {
3839

40+
private static final Logger LOG = Logger.getLogger(MBean.class.getName());
3941
private final Object bean;
4042
private final MBeanInfo beanInfo;
4143
private final Map<String, AttributeInfo> attributeMap = new HashMap<>();
@@ -59,7 +61,7 @@ MBeanAttributeInfo getMBeanAttributeInfo() {
5961
try {
6062
return new MBeanAttributeInfo(name, description, getter, setter);
6163
} catch (IntrospectionException e) {
62-
e.printStackTrace();
64+
LOG.severe("Error during execution: " + e.getMessage());
6365
return null;
6466
}
6567
}
@@ -123,7 +125,7 @@ private AttributeInfo getAttributeInfo(Method m) {
123125
String name = "".equals(ma.name()) ? m.getName() : ma.name();
124126
return new AttributeInfo(name, ma.description(), findGetter(m), findSetter(m));
125127
} catch (Throwable t) {
126-
t.printStackTrace();
128+
LOG.severe("Error during execution: " + t.getMessage());
127129
return null;
128130
}
129131
}
@@ -144,7 +146,7 @@ private Method findGetter(Method annotatedMethod) {
144146
}
145147
return null;
146148
} catch (NoSuchMethodException e) {
147-
e.printStackTrace();
149+
LOG.severe("Error during execution: " + e.getMessage());
148150
return null;
149151
}
150152
}
@@ -223,7 +225,7 @@ public Object getAttribute(String attribute) {
223225
return res.toString();
224226
}
225227
} catch (IllegalAccessException | InvocationTargetException e) {
226-
e.printStackTrace();
228+
LOG.severe("Error during execution: " + e.getMessage());
227229
return null;
228230
}
229231
}
@@ -233,7 +235,7 @@ public void setAttribute(Attribute attribute) {
233235
try {
234236
attributeMap.get(attribute.getName()).setter.invoke(bean, attribute.getValue());
235237
} catch (IllegalAccessException | InvocationTargetException e) {
236-
e.printStackTrace();
238+
LOG.severe("Error during execution: " + e.getMessage());
237239
}
238240
}
239241

@@ -252,7 +254,7 @@ public Object invoke(String actionName, Object[] params, String[] signature) {
252254
try {
253255
return operationMap.get(actionName).method.invoke(bean, params);
254256
} catch (IllegalAccessException | InvocationTargetException e) {
255-
e.printStackTrace();
257+
LOG.severe("Error during execution: " + e.getMessage());
256258
return null;
257259
}
258260
}

java/test/org/openqa/selenium/ElementAttributeTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2828

2929
import java.util.List;
30+
import java.util.logging.Logger;
3031
import org.junit.jupiter.api.Test;
3132
import org.openqa.selenium.environment.webserver.Page;
3233
import org.openqa.selenium.support.ui.ExpectedConditions;
@@ -35,6 +36,8 @@
3536

3637
class ElementAttributeTest extends JupiterTestBase {
3738

39+
private static final Logger LOG = Logger.getLogger(ElementAttributeTest.class.getName());
40+
3841
@Test
3942
void testShouldReturnNullWhenGettingTheValueOfAnAttributeThatIsNotListed() {
4043
driver.get(pages.simpleTestPage);
@@ -265,7 +268,7 @@ void testShouldCorrectlyReportValueOfColspan() {
265268
try {
266269
Thread.sleep(1000);
267270
} catch (InterruptedException e) {
268-
e.printStackTrace();
271+
LOG.severe("Error during execution: " + e.getMessage());
269272
}
270273

271274
WebElement th1 = driver.findElement(By.id("th1"));

java/test/org/openqa/selenium/firefox/ExecutableTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2222

2323
import java.io.File;
24+
import java.util.logging.Logger;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Tag;
2627
import org.junit.jupiter.api.Test;
@@ -29,14 +30,16 @@
2930
@Tag("UnitTests")
3031
class ExecutableTest {
3132

33+
private static final Logger LOG = Logger.getLogger(ExecutableTest.class.getName());
34+
3235
private String binaryPath;
3336

3437
@BeforeEach
3538
public void setUp() {
3639
try {
3740
binaryPath = new FirefoxBinary().getPath();
3841
} catch (WebDriverException ex) {
39-
ex.printStackTrace();
42+
LOG.severe("Error during execution: " + ex.getMessage());
4043
assumeTrue(false);
4144
}
4245
}

java/test/org/openqa/selenium/grid/distributor/local/LocalDistributorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.concurrent.Executors;
4444
import java.util.concurrent.Future;
4545
import java.util.concurrent.TimeUnit;
46+
import java.util.logging.Logger;
4647
import org.junit.jupiter.api.BeforeEach;
4748
import org.junit.jupiter.api.Test;
4849
import org.openqa.selenium.Capabilities;
@@ -81,6 +82,7 @@
8182

8283
class LocalDistributorTest {
8384

85+
private static final Logger LOG = Logger.getLogger(LocalDistributorTest.class.getName());
8486
private final Secret registrationSecret = new Secret("bavarian smoked");
8587
private static final int newSessionThreadPoolSize = Runtime.getRuntime().availableProcessors();
8688
private Tracer tracer;
@@ -421,7 +423,7 @@ void slowStartingNodesShouldNotCauseReservationsToBeSerialized() {
421423
try {
422424
Thread.sleep(delay);
423425
} catch (InterruptedException e) {
424-
e.printStackTrace();
426+
LOG.severe("Error during execution: " + e.getMessage());
425427
}
426428
return new Handler(c);
427429
}))

java/test/org/openqa/selenium/remote/internal/HttpClientTestBase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636
import java.util.Map;
3737
import java.util.TreeMap;
38+
import java.util.logging.Logger;
3839
import java.util.stream.StreamSupport;
3940
import org.junit.jupiter.api.AfterAll;
4041
import org.junit.jupiter.api.BeforeAll;
@@ -59,6 +60,8 @@ public abstract class HttpClientTestBase {
5960
static HttpHandler delegate;
6061
static AppServer server;
6162

63+
private static final Logger LOG = Logger.getLogger(HttpClientTestBase.class.getName());
64+
6265
@BeforeAll
6366
public static void setUp() {
6467
server = new NettyAppServer(req -> delegate.execute(req));
@@ -192,7 +195,7 @@ void shouldAllowConfigurationOfRequestTimeout() {
192195
try {
193196
Thread.sleep(1000);
194197
} catch (InterruptedException e) {
195-
e.printStackTrace();
198+
LOG.severe("Error during execution: " + e.getMessage());
196199
}
197200
return new HttpResponse()
198201
.setContent(Contents.utf8String(req.getHeader("user-agent")));

java/test/org/openqa/selenium/testing/IgnoreComparator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.HashSet;
2222
import java.util.Set;
23+
import java.util.logging.Logger;
2324
import java.util.regex.Matcher;
2425
import java.util.regex.Pattern;
2526
import java.util.stream.Stream;
@@ -30,6 +31,8 @@
3031
class IgnoreComparator {
3132
private final Set<Browser> ignored = new HashSet<>();
3233

34+
private static final Logger LOG = Logger.getLogger(IgnoreComparator.class.getName());
35+
3336
// TODO(simon): reduce visibility
3437
public void addDriver(Browser driverToIgnore) {
3538
ignored.add(driverToIgnore);
@@ -80,7 +83,7 @@ private boolean isOpenGitHubIssue(String owner, String repo, String issueId) {
8083
Issue issue = service.getIssue(owner, repo, issueId);
8184
return "open".equals(issue.getState());
8285
} catch (IOException e) {
83-
e.printStackTrace();
86+
LOG.severe("Error during execution: " + e.getMessage());
8487
}
8588
return true;
8689
}

0 commit comments

Comments
 (0)