Skip to content

Commit a717e3f

Browse files
danielrozenbergharsha509diemol
authored
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 <diemol@users.noreply.github.com>
1 parent 1c89e31 commit a717e3f

9 files changed

Lines changed: 36 additions & 22 deletions

File tree

javascript/node/selenium-webdriver/common/seleniumManager.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const path = require('path')
2626
const fs = require('fs')
2727
const spawnSync = require('child_process').spawnSync
2828
const { Capability } = require('../lib/capabilities')
29+
const logging = require('../lib/logging')
2930

31+
const log_ = logging.getLogger(logging.Type.DRIVER)
3032
let debugMessagePrinted = false
3133

3234
/**
@@ -53,7 +55,7 @@ function getBinary() {
5355
}
5456

5557
if (!debugMessagePrinted) {
56-
console.debug(`Selenium Manager binary found at ${filePath}`)
58+
log_.debug(`Selenium Manager binary found at ${filePath}`)
5759
debugMessagePrinted = true // Set the flag to true after printing the debug message
5860
}
5961

@@ -140,10 +142,10 @@ function driverLocation(options) {
140142
function logOutput(output) {
141143
for (const key in output.logs) {
142144
if (output.logs[key].level === 'WARN') {
143-
console.warn(`${output.logs[key].message}`)
145+
log_.warning(`${output.logs[key].message}`)
144146
}
145147
if (['DEBUG', 'INFO'].includes(output.logs[key].level)) {
146-
console.debug(`${output.logs[key].message}`)
148+
log_.debug(`${output.logs[key].message}`)
147149
}
148150
}
149151
}

javascript/node/selenium-webdriver/devtools/CDPConnection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
const logging = require('../lib/logging')
19+
1820
const RESPONSE_TIMEOUT = 1000 * 30
1921
class CDPConnection {
2022
constructor(wsConnection) {
@@ -65,7 +67,9 @@ class CDPConnection {
6567
resolve(payload)
6668
}
6769
} catch (err) {
68-
console.error(`Failed parse message: ${err.message}`)
70+
logging
71+
.getLogger(logging.Type.BROWSER)
72+
.error(`Failed parse message: ${err.message}`)
6973
}
7074
}
7175

javascript/node/selenium-webdriver/example/logging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const edge = require('../edge')
2727
const { Builder, By, Key, logging, until } = require('..')
2828

2929
logging.installConsoleHandler()
30-
logging.getLogger('webdriver.http').setLevel(logging.Level.ALL)
30+
logging.getLogger(`${logging.Type.DRIVER}.http`).setLevel(logging.Level.ALL)
3131
;(async function () {
3232
let driver
3333
try {

javascript/node/selenium-webdriver/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function createDriver(ctor, ...args) {
194194
class Builder {
195195
constructor() {
196196
/** @private @const */
197-
this.log_ = logging.getLogger('webdriver.Builder')
197+
this.log_ = logging.getLogger(`${logging.Type.DRIVER}.Builder`)
198198

199199
/** @private {string} */
200200
this.url_ = ''

javascript/node/selenium-webdriver/lib/http.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const { Session } = require('./session')
3434
const webElement = require('./webelement')
3535
const { isObject } = require('./util')
3636

37+
const log_ = logging.getLogger(`${logging.Type.DRIVER}.http`)
38+
3739
const getAttribute = requireAtom(
3840
'get-attribute.js',
3941
'//javascript/node/selenium-webdriver/lib/atoms:get-attribute.js'
@@ -58,10 +60,10 @@ function requireAtom(module, bazelTarget) {
5860
} catch (ex) {
5961
try {
6062
const file = bazelTarget.slice(2).replace(':', '/')
61-
console.log(`../../../bazel-bin/${file}`)
63+
log_.log(`../../../bazel-bin/${file}`)
6264
return require(path.resolve(`../../../bazel-bin/${file}`))
6365
} catch (ex2) {
64-
console.log(ex2)
66+
log_.error(ex2)
6567
throw Error(
6668
`Failed to import atoms module ${module}. If running in dev mode, you` +
6769
` need to run \`bazel build ${bazelTarget}\` from the project` +
@@ -153,8 +155,6 @@ const Atom = {
153155
FIND_ELEMENTS: findElements,
154156
}
155157

156-
const LOG = logging.getLogger('webdriver.http')
157-
158158
function post(path) {
159159
return resource('POST', path)
160160
}
@@ -428,15 +428,15 @@ class Client {
428428
* command to execute.
429429
*/
430430
function buildRequest(customCommands, command) {
431-
LOG.finest(() => `Translating command: ${command.getName()}`)
431+
log_.finest(() => `Translating command: ${command.getName()}`)
432432
let spec = customCommands && customCommands.get(command.getName())
433433
if (spec) {
434434
return toHttpRequest(spec)
435435
}
436436

437437
spec = W3C_COMMAND_MAP.get(command.getName())
438438
if (typeof spec === 'function') {
439-
LOG.finest(() => `Transforming command for W3C: ${command.getName()}`)
439+
log_.finest(() => `Transforming command for W3C: ${command.getName()}`)
440440
let newCommand = spec(command)
441441
return buildRequest(customCommands, newCommand)
442442
} else if (spec) {
@@ -451,7 +451,7 @@ function buildRequest(customCommands, command) {
451451
* @return {!Request}
452452
*/
453453
function toHttpRequest(resource) {
454-
LOG.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`)
454+
log_.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`)
455455
let parameters = command.getParameters()
456456
let path = buildPath(resource.path, parameters)
457457
return new Request(resource.method, path, parameters)
@@ -487,7 +487,7 @@ class Executor {
487487
this.customCommands_ = null
488488

489489
/** @private {!logging.Logger} */
490-
this.log_ = logging.getLogger('webdriver.http.Executor')
490+
this.log_ = logging.getLogger(`${logging.Type.DRIVER}.http.Executor`)
491491
}
492492

493493
/**

javascript/node/selenium-webdriver/lib/test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ process.on('unhandledRejection', (reason) => {
5555

5656
if (/^1|true$/i.test(process.env['SELENIUM_VERBOSE'])) {
5757
logging.installConsoleHandler()
58-
logging.getLogger('webdriver.http').setLevel(logging.Level.ALL)
58+
logging.getLogger(`${logging.Type.DRIVER}.http`).setLevel(logging.Level.ALL)
5959
}
6060

6161
testing.init()

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,8 @@ class Window {
21582158
constructor(driver) {
21592159
/** @private {!WebDriver} */
21602160
this.driver_ = driver
2161+
/** @private {!Logger} */
2162+
this.log_ = logging.getLogger(logging.Type.DRIVER)
21612163
}
21622164

21632165
/**
@@ -2252,7 +2254,7 @@ class Window {
22522254
*/
22532255
async getSize(windowHandle = 'current') {
22542256
if (windowHandle !== 'current') {
2255-
console.warn(
2257+
this.log_.warning(
22562258
`Only 'current' window is supported for W3C compatible browsers.`
22572259
)
22582260
}
@@ -2275,7 +2277,7 @@ class Window {
22752277
windowHandle = 'current'
22762278
) {
22772279
if (windowHandle !== 'current') {
2278-
console.warn(
2280+
this.log_.warning(
22792281
`Only 'current' window is supported for W3C compatible browsers.`
22802282
)
22812283
}
@@ -2547,6 +2549,9 @@ class WebElement {
25472549

25482550
/** @private {!Promise<string>} */
25492551
this.id_ = Promise.resolve(id)
2552+
2553+
/** @private {!Logger} */
2554+
this.log_ = logging.getLogger(logging.Type.DRIVER)
25502555
}
25512556

25522557
/**
@@ -2794,7 +2799,7 @@ class WebElement {
27942799
keys.join('')
27952800
)
27962801
} catch (ex) {
2797-
console.log(
2802+
this.log_.error(
27982803
'Error trying parse string as a file with file detector; sending keys instead' +
27992804
ex
28002805
)

javascript/node/selenium-webdriver/remote/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class DriverService {
120120
*/
121121
constructor(executable, options) {
122122
/** @private @const */
123-
this.log_ = logging.getLogger('webdriver.DriverService')
123+
this.log_ = logging.getLogger(`${logging.Type.DRIVER}.DriverService`)
124124
/** @private {string} */
125125
this.executable_ = executable
126126

javascript/node/selenium-webdriver/remote/util.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
const path = require('path')
2121
const cp = require('child_process')
22+
const logging = require('../lib/logging')
2223

2324
/**
2425
* returns path to java or 'java' string if JAVA_HOME does not exist in env obj
@@ -54,9 +55,11 @@ function isSelenium3x(seleniumStandalonePath) {
5455
*/
5556
function formatSpawnArgs(seleniumStandalonePath, args) {
5657
if (isSelenium3x(seleniumStandalonePath)) {
57-
console.warn(
58-
'Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x'
59-
)
58+
logging
59+
.getLogger(logging.Type.SERVER)
60+
.warning(
61+
'Deprecation: Support for Standalone Server 3.x will be removed soon. Please update to version 4.x',
62+
)
6063
return args
6164
}
6265

0 commit comments

Comments
 (0)