|
| 1 | +package com.github.kklisura.cdt.examples; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * cdt-examples |
| 6 | + * %% |
| 7 | + * Copyright (C) 2018 - 2021 Kenan Klisura |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import ch.qos.logback.classic.Level; |
| 24 | +import ch.qos.logback.classic.LoggerContext; |
| 25 | +import com.github.kklisura.cdt.launch.ChromeArguments; |
| 26 | +import com.github.kklisura.cdt.launch.ChromeLauncher; |
| 27 | +import com.github.kklisura.cdt.services.ChromeDevToolsService; |
| 28 | +import com.github.kklisura.cdt.services.ChromeService; |
| 29 | +import com.github.kklisura.cdt.services.types.ChromeTab; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * By setting logger `com.github.kklisura.cdt.launch.chrome.output` to DEBUG level, either |
| 34 | + * programmatically or via logging configuration (see logback.xml in the example) and by introducing |
| 35 | + * arguments to chrome to enable more verbose logging one can observe detailed chrome output. This |
| 36 | + * should be used to debug some issues with chrome and should not generally not be used in normal |
| 37 | + * course of operations. Note that, when you set this logger to DEBUG, a |
| 38 | + * chrome-launcher:read-line-thread thread will be alive, until browser gets closed. |
| 39 | + * |
| 40 | + * <p>Logger `com.github.kklisura.cdt.launch.chrome.output` can be set to DEBUG level |
| 41 | + * programmatically as in an example below (enableDebugChromeOutput method) or by introducing |
| 42 | + * |
| 43 | + * <pre> |
| 44 | + * <logger name="com.github.kklisura.cdt.launch.chrome.output" level="DEBUG" /> |
| 45 | + * </pre> |
| 46 | + * |
| 47 | + * logger to logging configuration. |
| 48 | + * |
| 49 | + * @author Kenan Klisura |
| 50 | + */ |
| 51 | +public class ChromeLoggingExample { |
| 52 | + |
| 53 | + /** |
| 54 | + * Programmatically set `com.github.kklisura.cdt.launch.chrome.output` to DEBUG level so chrome |
| 55 | + * output can be observed. |
| 56 | + */ |
| 57 | + private static void enableDebugChromeOutput() { |
| 58 | + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 59 | + loggerContext.getLogger("com.github.kklisura.cdt.launch.chrome.output").setLevel(Level.DEBUG); |
| 60 | + } |
| 61 | + |
| 62 | + public static void main(String[] args) throws InterruptedException { |
| 63 | + enableDebugChromeOutput(); |
| 64 | + |
| 65 | + // Create chrome launcher. |
| 66 | + final ChromeLauncher launcher = new ChromeLauncher(); |
| 67 | + |
| 68 | + // Launch chrome either as headless (true) or regular (false). |
| 69 | + final ChromeService chromeService = |
| 70 | + launcher.launch( |
| 71 | + ChromeArguments.defaults(false) |
| 72 | + // Sets the correct arguments: enable-logging and logging level |
| 73 | + .enableLogging("stderr") |
| 74 | + .additionalArguments("v", "1") |
| 75 | + .build()); |
| 76 | + |
| 77 | + // Create empty tab ie about:blank. |
| 78 | + final ChromeTab tab = chromeService.createTab(); |
| 79 | + |
| 80 | + // Get DevTools service to this tab |
| 81 | + final ChromeDevToolsService devToolsService = chromeService.createDevToolsService(tab); |
| 82 | + |
| 83 | + // Wait a bit and then close the browser |
| 84 | + Thread.sleep(2000); |
| 85 | + |
| 86 | + devToolsService.close(); |
| 87 | + launcher.close(); |
| 88 | + } |
| 89 | +} |
0 commit comments