diff --git a/.eslintrc.js b/.eslintrc.js index de0502125..a153d0410 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,14 @@ module.exports = { 'node': true }, 'extends': 'eslint:recommended', - 'overrides': [], + 'overrides': [ + { + 'files': ['samples/*.js'], + 'rules': { + 'no-console': ['warn'], + } + } + ], 'parserOptions': { 'ecmaVersion': 'latest' }, @@ -25,7 +32,7 @@ module.exports = { 'keyword-spacing': ['warn'], 'linebreak-style': ['warn', 'unix'], 'no-async-promise-executor': ['warn'], - 'no-console': ['warn', { 'allow': ['warn', 'error'] }], + 'no-console': ['error', { 'allow': ['warn', 'error'] }], 'no-empty': ['warn'], 'no-ex-assign': ['warn'], 'no-extra-semi': ['warn'], diff --git a/lib/agent/https_proxy_ocsp_agent.js b/lib/agent/https_proxy_ocsp_agent.js index 2cfc1a676..1fa686190 100644 --- a/lib/agent/https_proxy_ocsp_agent.js +++ b/lib/agent/https_proxy_ocsp_agent.js @@ -78,7 +78,7 @@ function HttpsProxyOcspAgent(opts) if (!HttpsAgent.secureProxy) { - console.warn("Warning: connecting to an authenticated proxy server through HTTP. To use HTTPS, set 'proxyProtocol' to 'HTTPS'") + Logger.getInstance().warn("Warning: connecting to an authenticated proxy server through HTTP. To use HTTPS, set 'proxyProtocol' to 'HTTPS'") } } diff --git a/lib/connection/connection_config.js b/lib/connection/connection_config.js index d207a87fd..86a49212c 100644 --- a/lib/connection/connection_config.js +++ b/lib/connection/connection_config.js @@ -53,6 +53,7 @@ const DEFAULT_PARAMS = 'disableQueryContextCache', 'loginTimeout', ]; +const Logger = require('../logger'); function consolidateHostAndAccount(options) { @@ -499,7 +500,7 @@ function ConnectionConfig(options, validateCredentials, qaMode, clientInfo) if (!DEFAULT_PARAMS.includes(key)) { const result = levenshtein.closest(key, DEFAULT_PARAMS); - console.error(`"${key}" is an unknown connection parameter. Did you mean "${result}"?`); + Logger.getInstance().error(`'${key}' is an unknown connection parameter. Did you mean '${result}'?`); } } } diff --git a/lib/core.js b/lib/core.js index 5b27843a6..6f4ce46c4 100644 --- a/lib/core.js +++ b/lib/core.js @@ -272,7 +272,7 @@ function Core(options) { if (err) { - console.error('Unable to connect: ' + err.message); + Logger.getInstance().error('Unable to connect: ' + err.message); reject(new Error(err.message)); } else @@ -299,7 +299,7 @@ function Core(options) { if (err) { - console.error('Unable to disconnect: ' + err.message); + Logger.getInstance().error('Unable to disconnect: ' + err.message); } resolve(); }); diff --git a/test/integration/testConnection.js b/test/integration/testConnection.js index e75c0c5bb..d1d6fb6e4 100644 --- a/test/integration/testConnection.js +++ b/test/integration/testConnection.js @@ -8,7 +8,8 @@ const connOption = require("./connectionOptions"); const testUtil = require("./testUtil"); const Util = require("./../../lib/util"); const Core = require("./../../lib/core"); -const stderr = require("test-console").stderr; +const { stdout } = require('test-console'); +const { assertLogMessage } = require('./testUtil'); describe("Connection test", function () { it("return tokens in qaMode", function () { @@ -111,7 +112,7 @@ describe("Connection test", function () { describe("Connection test - validate default parameters", function () { it('Valid "warehouse" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -124,7 +125,7 @@ describe("Connection test - validate default parameters", function () { }); it('Invalid "warehouse" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -133,13 +134,11 @@ describe("Connection test - validate default parameters", function () { validateDefaultParameters: true, }); }); - assert.deepEqual(output, [ - '"waerhouse" is an unknown connection parameter. Did you mean "warehouse"?\n', - ]); + assertLogMessage('ERROR', '\'waerhouse\' is an unknown connection parameter. Did you mean \'warehouse\'?', output[0]); }); it('Valid "database" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -152,7 +151,7 @@ describe("Connection test - validate default parameters", function () { }); it('Invalid "db" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -161,11 +160,11 @@ describe("Connection test - validate default parameters", function () { validateDefaultParameters: true, }); }); - assert.deepEqual(output, ['"db" is an unknown connection parameter. Did you mean "host"?\n']); + assertLogMessage('ERROR', '\'db\' is an unknown connection parameter. Did you mean \'host\'?', output[0]); }); it('Invalid "database" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -174,13 +173,11 @@ describe("Connection test - validate default parameters", function () { validateDefaultParameters: true, }); }); - assert.deepEqual(output, [ - '"datbse" is an unknown connection parameter. Did you mean "database"?\n', - ]); + assertLogMessage('ERROR', '\'datbse\' is an unknown connection parameter. Did you mean \'database\'?', output[0]); }); it('Valid "schema" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -193,7 +190,7 @@ describe("Connection test - validate default parameters", function () { }); it('Invalid "schema" parameter', function () { - const output = stderr.inspectSync(() => { + const output = stdout.inspectSync(() => { snowflake.createConnection({ account: connOption.valid.account, username: connOption.valid.username, @@ -202,9 +199,7 @@ describe("Connection test - validate default parameters", function () { validateDefaultParameters: true, }); }); - assert.deepEqual(output, [ - '"shcema" is an unknown connection parameter. Did you mean "schema"?\n', - ]); + assertLogMessage('ERROR', '\'shcema\' is an unknown connection parameter. Did you mean \'schema\'?', output[0]); }); }); diff --git a/test/integration/testManualConnection.js b/test/integration/testManualConnection.js index 806db1bbb..d763f5e7a 100644 --- a/test/integration/testManualConnection.js +++ b/test/integration/testManualConnection.js @@ -3,6 +3,7 @@ const async = require("async"); const assert = require("assert"); const connOption = require("./connectionOptions"); const testUtil = require("./testUtil"); +const Logger = require('../../lib/logger'); if (process.env.RUN_MANUAL_TESTS_ONLY == "true") { describe.only("Run manual tests", function () { @@ -321,7 +322,7 @@ if (process.env.RUN_MANUAL_TESTS_ONLY == "true") { let time = await executeSingleQuery(); sumWithoutKeepAlive += time; } - console.log(`Sum of time without keep alive: ${sumWithoutKeepAlive}. Sum of time with keep alive:: ${sumWithKeepAlive}`); + Logger.getInstance().info(`Sum of time without keep alive: ${sumWithoutKeepAlive}. Sum of time with keep alive:: ${sumWithKeepAlive}`); assert.ok(sumWithoutKeepAlive * 0.66 > sumWithKeepAlive, 'With keep alive the queries should work faster'); }); }); diff --git a/test/integration/testMultiStatement.js b/test/integration/testMultiStatement.js index d00bcadd1..eead1f397 100644 --- a/test/integration/testMultiStatement.js +++ b/test/integration/testMultiStatement.js @@ -5,7 +5,7 @@ var async = require('async'); var assert = require('assert'); var testUtil = require('./testUtil'); var Util = require('./../../lib/util'); - +const Logger = require('../../lib/logger'); describe('Test multi statement', function () { @@ -41,9 +41,9 @@ describe('Test multi statement', function () connection.execute({ sqlText: 'select current_version()', complete: function (err, stmt, rows) { - console.log('=== driver version = ' + Util.driverVersion); - console.log('=== server version ='); - console.log(rows); + Logger.getInstance().info('=== driver version = ' + Util.driverVersion); + Logger.getInstance().info('=== server version ='); + Logger.getInstance().info(rows); callback(); } }); @@ -61,14 +61,14 @@ describe('Test multi statement', function () testUtil.checkError(err); }); stream.on('data', function (row) { - console.log(row); + Logger.getInstance().info(row); count += Object.values(row).length; if (stmt.hasNext()) { - console.log('==== hasNext'); + Logger.getInstance().info('==== hasNext'); stmt.NextResult(); } else { - console.log('==== close connection'); + Logger.getInstance().info('==== close connection'); assert.strictEqual(6, count); done(); } diff --git a/test/integration/testOcsp.js b/test/integration/testOcsp.js index 7c01685db..a9b26039f 100644 --- a/test/integration/testOcsp.js +++ b/test/integration/testOcsp.js @@ -169,7 +169,7 @@ describe('OCSP validation', function () { if (!err.hasOwnProperty('code')) { - console.log(err); + Logger.getInstance().error(err); } assert.equal(err['code'], '390100'); } diff --git a/test/integration/testUtil.js b/test/integration/testUtil.js index a57131660..9409e1966 100644 --- a/test/integration/testUtil.js +++ b/test/integration/testUtil.js @@ -6,6 +6,7 @@ const connOptions = require('./connectionOptions'); const assert = require('assert'); const fs = require('fs'); const crypto = require('crypto'); +const Logger = require('../../lib/logger'); module.exports.createConnection = function (validConnectionOptionsOverride = {}) { return snowflake.createConnection({ @@ -97,7 +98,7 @@ module.exports.dropTablesIgnoringErrorsAsync = async (connection, tableNames) => try { await executeCmdAsync(connection, `DROP TABLE IF EXISTS ${tableName}`); } catch (e) { - console.warn(`Cannot drop table ${tableName}: ${JSON.stringify(e)}`); + Logger.getInstance().warn(`Cannot drop table ${tableName}: ${JSON.stringify(e)}`); } } }; @@ -113,7 +114,7 @@ module.exports.dropDBsIgnoringErrorsAsync = async (connection, dbNames) => { try { await executeCmdAsync(connection, `DROP DATABASE IF EXISTS ${dbName}`); } catch (e) { - console.warn(`Cannot drop database ${dbName}: ${JSON.stringify(e)}`); + Logger.getInstance().warn(`Cannot drop database ${dbName}: ${JSON.stringify(e)}`); } } }; @@ -226,7 +227,7 @@ module.exports.deleteFileSyncIgnoringErrors = function (file) { fs.closeSync(file.fd); fs.unlinkSync(file.name); } catch (e) { - console.warn(`Cannot remove file ${file.name}: ${JSON.stringify(e)}`); + Logger.getInstance().warn(`Cannot remove file ${file.name}: ${JSON.stringify(e)}`); } } }; @@ -242,7 +243,7 @@ module.exports.deleteFolderSyncIgnoringErrors = function (directory) { fs.rmdirSync(directory, { recursive: true }); } } catch (e) { - console.warn(`Cannot delete folder ${directory}: ${JSON.stringify(e)}`); + Logger.getInstance().warn(`Cannot delete folder ${directory}: ${JSON.stringify(e)}`); } }; @@ -256,3 +257,13 @@ module.exports.randomizeName = function (name) { const randomString = crypto.randomBytes(4).toString('hex'); return name.concat(randomString); }; + +/** + * @param expectedLevel string + * @param expectedMessage string + * @param actualMessage string + */ +module.exports.assertLogMessage = function (expectedLevel, expectedMessage, actualMessage) { + const regexPattern = `^{"level":"${expectedLevel}","message":"\\[.*\\]: ${expectedMessage}`; + return assert.match(actualMessage, new RegExp(regexPattern)); +}; \ No newline at end of file