From a717e3feba8b04289bdfc1e5efb9282e46560217 Mon Sep 17 00:00:00 2001 From: Daniel Rozenberg Date: Wed, 11 Oct 2023 06:54:01 -0400 Subject: [PATCH] Replace calls to console.log with managed loggers (#12909) Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com> Co-authored-by: Diego Molina --- .../selenium-webdriver/common/seleniumManager.js | 8 +++++--- .../selenium-webdriver/devtools/CDPConnection.js | 6 +++++- .../node/selenium-webdriver/example/logging.js | 2 +- javascript/node/selenium-webdriver/index.js | 2 +- javascript/node/selenium-webdriver/lib/http.js | 16 ++++++++-------- .../node/selenium-webdriver/lib/test/index.js | 2 +- .../node/selenium-webdriver/lib/webdriver.js | 11 ++++++++--- .../node/selenium-webdriver/remote/index.js | 2 +- .../node/selenium-webdriver/remote/util.js | 9 ++++++--- 9 files changed, 36 insertions(+), 22 deletions(-) diff --git a/javascript/node/selenium-webdriver/common/seleniumManager.js b/javascript/node/selenium-webdriver/common/seleniumManager.js index 1c063aa06bc39..f93b363c5a4d9 100644 --- a/javascript/node/selenium-webdriver/common/seleniumManager.js +++ b/javascript/node/selenium-webdriver/common/seleniumManager.js @@ -26,7 +26,9 @@ const path = require('path') const fs = require('fs') const spawnSync = require('child_process').spawnSync const { Capability } = require('../lib/capabilities') +const logging = require('../lib/logging') +const log_ = logging.getLogger(logging.Type.DRIVER) let debugMessagePrinted = false /** @@ -53,7 +55,7 @@ function getBinary() { } if (!debugMessagePrinted) { - console.debug(`Selenium Manager binary found at ${filePath}`) + log_.debug(`Selenium Manager binary found at ${filePath}`) debugMessagePrinted = true // Set the flag to true after printing the debug message } @@ -140,10 +142,10 @@ function driverLocation(options) { function logOutput(output) { for (const key in output.logs) { if (output.logs[key].level === 'WARN') { - console.warn(`${output.logs[key].message}`) + log_.warning(`${output.logs[key].message}`) } if (['DEBUG', 'INFO'].includes(output.logs[key].level)) { - console.debug(`${output.logs[key].message}`) + log_.debug(`${output.logs[key].message}`) } } } diff --git a/javascript/node/selenium-webdriver/devtools/CDPConnection.js b/javascript/node/selenium-webdriver/devtools/CDPConnection.js index 7cf54b9644103..0440c62c155e1 100644 --- a/javascript/node/selenium-webdriver/devtools/CDPConnection.js +++ b/javascript/node/selenium-webdriver/devtools/CDPConnection.js @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +const logging = require('../lib/logging') + const RESPONSE_TIMEOUT = 1000 * 30 class CDPConnection { constructor(wsConnection) { @@ -65,7 +67,9 @@ class CDPConnection { resolve(payload) } } catch (err) { - console.error(`Failed parse message: ${err.message}`) + logging + .getLogger(logging.Type.BROWSER) + .error(`Failed parse message: ${err.message}`) } } diff --git a/javascript/node/selenium-webdriver/example/logging.js b/javascript/node/selenium-webdriver/example/logging.js index 80ba1b7e3f294..c396cead5fead 100644 --- a/javascript/node/selenium-webdriver/example/logging.js +++ b/javascript/node/selenium-webdriver/example/logging.js @@ -27,7 +27,7 @@ const edge = require('../edge') const { Builder, By, Key, logging, until } = require('..') logging.installConsoleHandler() -logging.getLogger('webdriver.http').setLevel(logging.Level.ALL) +logging.getLogger(`${logging.Type.DRIVER}.http`).setLevel(logging.Level.ALL) ;(async function () { let driver try { diff --git a/javascript/node/selenium-webdriver/index.js b/javascript/node/selenium-webdriver/index.js index 5b33eb1f828c2..7cad3ff3c0a01 100644 --- a/javascript/node/selenium-webdriver/index.js +++ b/javascript/node/selenium-webdriver/index.js @@ -194,7 +194,7 @@ function createDriver(ctor, ...args) { class Builder { constructor() { /** @private @const */ - this.log_ = logging.getLogger('webdriver.Builder') + this.log_ = logging.getLogger(`${logging.Type.DRIVER}.Builder`) /** @private {string} */ this.url_ = '' diff --git a/javascript/node/selenium-webdriver/lib/http.js b/javascript/node/selenium-webdriver/lib/http.js index bf9791501e14c..58bcf51bca2e1 100644 --- a/javascript/node/selenium-webdriver/lib/http.js +++ b/javascript/node/selenium-webdriver/lib/http.js @@ -34,6 +34,8 @@ const { Session } = require('./session') const webElement = require('./webelement') const { isObject } = require('./util') +const log_ = logging.getLogger(`${logging.Type.DRIVER}.http`) + const getAttribute = requireAtom( 'get-attribute.js', '//javascript/node/selenium-webdriver/lib/atoms:get-attribute.js' @@ -58,10 +60,10 @@ function requireAtom(module, bazelTarget) { } catch (ex) { try { const file = bazelTarget.slice(2).replace(':', '/') - console.log(`../../../bazel-bin/${file}`) + log_.log(`../../../bazel-bin/${file}`) return require(path.resolve(`../../../bazel-bin/${file}`)) } catch (ex2) { - console.log(ex2) + log_.error(ex2) throw Error( `Failed to import atoms module ${module}. If running in dev mode, you` + ` need to run \`bazel build ${bazelTarget}\` from the project` + @@ -153,8 +155,6 @@ const Atom = { FIND_ELEMENTS: findElements, } -const LOG = logging.getLogger('webdriver.http') - function post(path) { return resource('POST', path) } @@ -428,7 +428,7 @@ class Client { * command to execute. */ function buildRequest(customCommands, command) { - LOG.finest(() => `Translating command: ${command.getName()}`) + log_.finest(() => `Translating command: ${command.getName()}`) let spec = customCommands && customCommands.get(command.getName()) if (spec) { return toHttpRequest(spec) @@ -436,7 +436,7 @@ function buildRequest(customCommands, command) { spec = W3C_COMMAND_MAP.get(command.getName()) if (typeof spec === 'function') { - LOG.finest(() => `Transforming command for W3C: ${command.getName()}`) + log_.finest(() => `Transforming command for W3C: ${command.getName()}`) let newCommand = spec(command) return buildRequest(customCommands, newCommand) } else if (spec) { @@ -451,7 +451,7 @@ function buildRequest(customCommands, command) { * @return {!Request} */ function toHttpRequest(resource) { - LOG.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`) + log_.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`) let parameters = command.getParameters() let path = buildPath(resource.path, parameters) return new Request(resource.method, path, parameters) @@ -487,7 +487,7 @@ class Executor { this.customCommands_ = null /** @private {!logging.Logger} */ - this.log_ = logging.getLogger('webdriver.http.Executor') + this.log_ = logging.getLogger(`${logging.Type.DRIVER}.http.Executor`) } /** diff --git a/javascript/node/selenium-webdriver/lib/test/index.js b/javascript/node/selenium-webdriver/lib/test/index.js index 41b35819bc543..2edea2a62d57d 100644 --- a/javascript/node/selenium-webdriver/lib/test/index.js +++ b/javascript/node/selenium-webdriver/lib/test/index.js @@ -55,7 +55,7 @@ process.on('unhandledRejection', (reason) => { if (/^1|true$/i.test(process.env['SELENIUM_VERBOSE'])) { logging.installConsoleHandler() - logging.getLogger('webdriver.http').setLevel(logging.Level.ALL) + logging.getLogger(`${logging.Type.DRIVER}.http`).setLevel(logging.Level.ALL) } testing.init() diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 20c417e05611d..b8d7bca7adff1 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -2158,6 +2158,8 @@ class Window { constructor(driver) { /** @private {!WebDriver} */ this.driver_ = driver + /** @private {!Logger} */ + this.log_ = logging.getLogger(logging.Type.DRIVER) } /** @@ -2252,7 +2254,7 @@ class Window { */ async getSize(windowHandle = 'current') { if (windowHandle !== 'current') { - console.warn( + this.log_.warning( `Only 'current' window is supported for W3C compatible browsers.` ) } @@ -2275,7 +2277,7 @@ class Window { windowHandle = 'current' ) { if (windowHandle !== 'current') { - console.warn( + this.log_.warning( `Only 'current' window is supported for W3C compatible browsers.` ) } @@ -2547,6 +2549,9 @@ class WebElement { /** @private {!Promise} */ this.id_ = Promise.resolve(id) + + /** @private {!Logger} */ + this.log_ = logging.getLogger(logging.Type.DRIVER) } /** @@ -2794,7 +2799,7 @@ class WebElement { keys.join('') ) } catch (ex) { - console.log( + this.log_.error( 'Error trying parse string as a file with file detector; sending keys instead' + ex ) diff --git a/javascript/node/selenium-webdriver/remote/index.js b/javascript/node/selenium-webdriver/remote/index.js index 8d826793487df..3132b04c2aad8 100644 --- a/javascript/node/selenium-webdriver/remote/index.js +++ b/javascript/node/selenium-webdriver/remote/index.js @@ -120,7 +120,7 @@ class DriverService { */ constructor(executable, options) { /** @private @const */ - this.log_ = logging.getLogger('webdriver.DriverService') + this.log_ = logging.getLogger(`${logging.Type.DRIVER}.DriverService`) /** @private {string} */ this.executable_ = executable diff --git a/javascript/node/selenium-webdriver/remote/util.js b/javascript/node/selenium-webdriver/remote/util.js index 2cc52eed6250b..411ed962956e6 100644 --- a/javascript/node/selenium-webdriver/remote/util.js +++ b/javascript/node/selenium-webdriver/remote/util.js @@ -19,6 +19,7 @@ const path = require('path') const cp = require('child_process') +const logging = require('../lib/logging') /** * returns path to java or 'java' string if JAVA_HOME does not exist in env obj @@ -54,9 +55,11 @@ function isSelenium3x(seleniumStandalonePath) { */ function formatSpawnArgs(seleniumStandalonePath, args) { if (isSelenium3x(seleniumStandalonePath)) { - console.warn( - 'Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x' - ) + logging + .getLogger(logging.Type.SERVER) + .warning( + 'Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x', + ) return args }