Skip to content

Commit

Permalink
Replace calls to console.log with managed loggers (#12909)
Browse files Browse the repository at this point in the history
Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 11, 2023
1 parent 1c89e31 commit a717e3f
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 22 deletions.
8 changes: 5 additions & 3 deletions javascript/node/selenium-webdriver/common/seleniumManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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
}

Expand Down Expand Up @@ -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}`)
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion javascript/node/selenium-webdriver/devtools/CDPConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/example/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_ = ''
Expand Down
16 changes: 8 additions & 8 deletions javascript/node/selenium-webdriver/lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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` +
Expand Down Expand Up @@ -153,8 +155,6 @@ const Atom = {
FIND_ELEMENTS: findElements,
}

const LOG = logging.getLogger('webdriver.http')

function post(path) {
return resource('POST', path)
}
Expand Down Expand Up @@ -428,15 +428,15 @@ 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)
}

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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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`)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/lib/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,8 @@ class Window {
constructor(driver) {
/** @private {!WebDriver} */
this.driver_ = driver
/** @private {!Logger} */
this.log_ = logging.getLogger(logging.Type.DRIVER)
}

/**
Expand Down Expand Up @@ -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.`
)
}
Expand All @@ -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.`
)
}
Expand Down Expand Up @@ -2547,6 +2549,9 @@ class WebElement {

/** @private {!Promise<string>} */
this.id_ = Promise.resolve(id)

/** @private {!Logger} */
this.log_ = logging.getLogger(logging.Type.DRIVER)
}

/**
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions javascript/node/selenium-webdriver/remote/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit a717e3f

Please sign in to comment.