|
17 | 17 | package io.appium.java_client.service.local;
|
18 | 18 |
|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 20 | +import static org.slf4j.event.Level.DEBUG; |
| 21 | +import static org.slf4j.event.Level.INFO; |
20 | 22 |
|
| 23 | +import com.google.common.annotations.VisibleForTesting; |
21 | 24 | import com.google.common.collect.ImmutableList;
|
22 | 25 | import com.google.common.collect.ImmutableMap;
|
23 | 26 |
|
|
26 | 29 | import org.openqa.selenium.net.UrlChecker;
|
27 | 30 | import org.openqa.selenium.os.CommandLine;
|
28 | 31 | import org.openqa.selenium.remote.service.DriverService;
|
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | +import org.slf4j.event.Level; |
29 | 35 |
|
30 | 36 | import java.io.File;
|
31 | 37 | import java.io.IOException;
|
|
35 | 41 | import java.util.List;
|
36 | 42 | import java.util.concurrent.TimeUnit;
|
37 | 43 | import java.util.concurrent.locks.ReentrantLock;
|
| 44 | +import java.util.function.BiConsumer; |
| 45 | +import java.util.function.Consumer; |
| 46 | +import java.util.regex.Matcher; |
| 47 | +import java.util.regex.Pattern; |
| 48 | + |
38 | 49 | import javax.annotation.Nullable;
|
39 | 50 |
|
40 | 51 | public final class AppiumDriverLocalService extends DriverService {
|
41 | 52 |
|
42 | 53 | private static final String URL_MASK = "http://%s:%d/wd/hub";
|
| 54 | + private static final Logger LOG = LoggerFactory.getLogger(AppiumDriverLocalService.class); |
| 55 | + private static final Pattern LOG_MESSAGE_PATTERN = Pattern.compile("^(.*)\\R"); |
| 56 | + private static final Pattern LOGGER_CONTEXT_PATTERN = Pattern.compile("^(\\[debug\\] )?\\[(.+?)\\]"); |
| 57 | + private static final String APPIUM_SERVICE_SLF4J_LOGGER_PREFIX = "appium.service"; |
43 | 58 | private final File nodeJSExec;
|
44 | 59 | private final ImmutableList<String> nodeJSArgs;
|
45 | 60 | private final ImmutableMap<String, String> nodeJSEnvironment;
|
@@ -219,4 +234,134 @@ public void addOutPutStreams(List<OutputStream> outputStreams) {
|
219 | 234 | public boolean clearOutPutStreams() {
|
220 | 235 | return stream.clear();
|
221 | 236 | }
|
| 237 | + |
| 238 | + /** |
| 239 | + * Enables server output data logging through |
| 240 | + * <a href="http://slf4j.org">SLF4J</a> loggers. This allow server output |
| 241 | + * data to be configured with your preferred logging frameworks (e.g. |
| 242 | + * java.util.logging, logback, log4j). |
| 243 | + * |
| 244 | + * <p>NOTE1: You might want to call method {@link #clearOutPutStreams()} before |
| 245 | + * calling this method.<br> |
| 246 | + * NOTE2: it is required that {@code --log-timestamp} server flag is |
| 247 | + * {@code false}. |
| 248 | + * |
| 249 | + * <p>By default log messages are: |
| 250 | + * <ul> |
| 251 | + * <li>logged at {@code INFO} level, unless log message is pre-fixed by |
| 252 | + * {@code [debug]} then logged at {@code DEBUG} level.</li> |
| 253 | + * <li>logged by a <a href="http://slf4j.org">SLF4J</a> logger instance with |
| 254 | + * a name corresponding to the appium sub module as prefixed in log message |
| 255 | + * (logger name is transformed to lower case, no spaces and prefixed with |
| 256 | + * "appium.service.").</li> |
| 257 | + * </ul> |
| 258 | + * Example log-message: "[ADB] Cannot read version codes of " is logged by |
| 259 | + * logger: {@code appium.service.adb} at level {@code INFO}. |
| 260 | + * <br> |
| 261 | + * Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' " |
| 262 | + * is logged by logger {@code appium.service.xcuitest} at level |
| 263 | + * {@code DEBUG}. |
| 264 | + * <br> |
| 265 | + * |
| 266 | + * @see #addSlf4jLogMessageConsumer(BiConsumer) |
| 267 | + */ |
| 268 | + public void enableDefaultSlf4jLoggingOfOutputData() { |
| 269 | + addSlf4jLogMessageConsumer((logMessage, ctx) -> { |
| 270 | + if (ctx.getLevel().equals(DEBUG)) { |
| 271 | + ctx.getLogger().debug(logMessage); |
| 272 | + } else { |
| 273 | + ctx.getLogger().info(logMessage); |
| 274 | + } |
| 275 | + }); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * When a complete log message is available (from server output data) that |
| 280 | + * message is parsed for its slf4j context (logger name, logger level etc.) |
| 281 | + * and the specified {@code BiConsumer} is invoked with the log message and |
| 282 | + * slf4j context. |
| 283 | + * |
| 284 | + * <p>Use this method only if you want a behavior that differentiates from the |
| 285 | + * default behavior as enabled by method |
| 286 | + * {@link #enableDefaultSlf4jLoggingOfOutputData()}. |
| 287 | + * |
| 288 | + * <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before |
| 289 | + * calling this method. |
| 290 | + * |
| 291 | + * <p>implementation detail: |
| 292 | + * <ul> |
| 293 | + * <li>if log message begins with {@code [debug]} then log level is set to |
| 294 | + * {@code DEBUG}, otherwise log level is {@code INFO}.</li> |
| 295 | + * <li>the appium sub module name is parsed from the log message and used as |
| 296 | + * logger name (prefixed with "appium.service.", all lower case, spaces |
| 297 | + * removed). If no appium sub module is detected then "appium.service" is |
| 298 | + * used as logger name.</li> |
| 299 | + * </ul> |
| 300 | + * Example log-message: "[ADB] Cannot read version codes of " is logged by |
| 301 | + * {@code appium.service.adb} at level {@code INFO} <br> |
| 302 | + * Example log-message: "[debug] [XCUITest] Xcode version set to 'x.y.z' " |
| 303 | + * is logged by {@code appium.service.xcuitest} at level {@code DEBUG} |
| 304 | + * <br> |
| 305 | + * |
| 306 | + * @param slf4jLogMessageConsumer |
| 307 | + * BiConsumer block to be executed when a log message is |
| 308 | + * available. |
| 309 | + */ |
| 310 | + public void addSlf4jLogMessageConsumer(BiConsumer<String, Slf4jLogMessageContext> slf4jLogMessageConsumer) { |
| 311 | + checkNotNull(slf4jLogMessageConsumer, "slf4jLogMessageConsumer parameter is NULL!"); |
| 312 | + addLogMessageConsumer(logMessage -> { |
| 313 | + slf4jLogMessageConsumer.accept(logMessage, parseSlf4jContextFromLogMessage(logMessage)); |
| 314 | + }); |
| 315 | + } |
| 316 | + |
| 317 | + @VisibleForTesting |
| 318 | + static Slf4jLogMessageContext parseSlf4jContextFromLogMessage(String logMessage) { |
| 319 | + Matcher m = LOGGER_CONTEXT_PATTERN.matcher(logMessage); |
| 320 | + String loggerName = APPIUM_SERVICE_SLF4J_LOGGER_PREFIX; |
| 321 | + Level level = INFO; |
| 322 | + if (m.find()) { |
| 323 | + loggerName += "." + m.group(2).toLowerCase().replaceAll("\\s+", ""); |
| 324 | + if (m.group(1) != null) { |
| 325 | + level = DEBUG; |
| 326 | + } |
| 327 | + } |
| 328 | + return new Slf4jLogMessageContext(loggerName, level); |
| 329 | + } |
| 330 | + |
| 331 | + /** |
| 332 | + * When a complete log message is available (from server output data), the |
| 333 | + * specified {@code Consumer} is invoked with that log message. |
| 334 | + * |
| 335 | + * <p>NOTE: You might want to call method {@link #clearOutPutStreams()} before |
| 336 | + * calling this method. |
| 337 | + * |
| 338 | + * <p>If the Consumer fails and throws an exception the exception is logged (at |
| 339 | + * WARN level) and execution continues. |
| 340 | + * <br> |
| 341 | + * |
| 342 | + * @param consumer |
| 343 | + * Consumer block to be executed when a log message is available. |
| 344 | + * |
| 345 | + */ |
| 346 | + public void addLogMessageConsumer(Consumer<String> consumer) { |
| 347 | + checkNotNull(consumer, "consumer parameter is NULL!"); |
| 348 | + addOutPutStream(new OutputStream() { |
| 349 | + StringBuilder lineBuilder = new StringBuilder(); |
| 350 | + |
| 351 | + @Override |
| 352 | + public void write(int chr) throws IOException { |
| 353 | + try { |
| 354 | + lineBuilder.append((char) chr); |
| 355 | + Matcher matcher = LOG_MESSAGE_PATTERN.matcher(lineBuilder.toString()); |
| 356 | + if (matcher.matches()) { |
| 357 | + consumer.accept(matcher.group(1)); |
| 358 | + lineBuilder = new StringBuilder(); |
| 359 | + } |
| 360 | + } catch (Exception e) { |
| 361 | + // log error and continue |
| 362 | + LOG.warn("Log message consumer crashed!", e); |
| 363 | + } |
| 364 | + } |
| 365 | + }); |
| 366 | + } |
222 | 367 | }
|
0 commit comments