From c85c006e722fce1271795b2613e1dd2a96983046 Mon Sep 17 00:00:00 2001 From: jordigh Date: Thu, 1 Jun 2023 13:12:06 -0400 Subject: [PATCH] fix(logger): log execArgs at the debug level (#1654) --- index.js | 7 ++++- test/integration/logger-test-case/hello.js | 18 +++++++++++ test/integration/logger-test-case/newrelic.js | 15 ++++++++++ test/integration/logger.tap.js | 30 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/integration/logger-test-case/hello.js create mode 100644 test/integration/logger-test-case/newrelic.js diff --git a/index.js b/index.js index af4d6564cb..0bcbada6e3 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,12 @@ function initialize() { logger.debug('Current working directory at module load is %s.', process.cwd()) logger.debug('Process title is %s.', process.title) - logger.debug('Application was invoked as %s.', process.argv.join(' ')) + + // execArgv happens before the script name but after the original executable name + // https://nodejs.org/api/process.html#process_process_execargv + const cliArgs = [process.argv[0], ...process.execArgv, ...process.argv.slice(1)] + + logger.debug('Application was invoked as %s', cliArgs.join(' ')) const config = require('./lib/config').getOrCreateInstance() diff --git a/test/integration/logger-test-case/hello.js b/test/integration/logger-test-case/hello.js new file mode 100644 index 0000000000..02f7bf0ef9 --- /dev/null +++ b/test/integration/logger-test-case/hello.js @@ -0,0 +1,18 @@ +/* + * Copyright 2023 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const newrelic = require('../../../index.js') + +function greeter(name) { + return `Hello ${name}` +} + +if (newrelic.agent) { + /* eslint-disable no-console */ + console.log(greeter(newrelic.agent.config.app_name)) + /* eslint-enable no-console */ +} diff --git a/test/integration/logger-test-case/newrelic.js b/test/integration/logger-test-case/newrelic.js new file mode 100644 index 0000000000..d7f67e08da --- /dev/null +++ b/test/integration/logger-test-case/newrelic.js @@ -0,0 +1,15 @@ +/* + * Copyright 2023 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +exports.config = { + app_name: ['cool-app'], + license_key: 'nonsensical-balderdash', + logging: { + level: 'debug', + filepath: 'stderr' + } +} diff --git a/test/integration/logger.tap.js b/test/integration/logger.tap.js index 23a85c459a..2cc2ad44d3 100644 --- a/test/integration/logger.tap.js +++ b/test/integration/logger.tap.js @@ -9,6 +9,8 @@ const path = require('path') const fs = require('fs') const tap = require('tap') const rimraf = require('rimraf') +const util = require('util') +const exec = util.promisify(require('child_process').exec) const DIRNAME = 'XXXNOCONFTEST' @@ -29,6 +31,7 @@ tap.test('logger', function (t) { resolve() } }) + delete process.env.NEW_RELIC_LOG }) t.test('configuration from environment', function (t) { @@ -49,3 +52,30 @@ tap.test('logger', function (t) { }) }) }) + +tap.test('Logger output', (t) => { + t.autoend() + + const execArgs = [ + { opt: '-r', arg: '../../../index.js' }, + { opt: '--experimental-loader', arg: '../../../esm-loader.mjs' } + ] + for (const pair of execArgs) { + const { opt, arg } = pair + t.test(`Check for ${opt} in logger output at debug level`, async (t) => { + const { stdout, stderr } = await exec(`node ${opt} ${arg} hello.js`, { + cwd: `${__dirname}/logger-test-case` + }) + t.equal(stdout, 'Hello cool-app\n', 'should get the normal output') + t.match( + stderr, + // The actual output adds the full path to the node executable + // and the script path, so that's why we have .* in the regex + // here. + new RegExp(`Application was invoked as .*node ${opt} ${arg} .*hello.js`), + `should contain 'node ${opt}' in the logs` + ) + t.end() + }) + } +})