Skip to content

Commit

Permalink
fix(logger): log execArgs at the debug level (#1654)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordigh authored Jun 1, 2023
1 parent c1e81a7 commit c85c006
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
18 changes: 18 additions & 0 deletions test/integration/logger-test-case/hello.js
Original file line number Diff line number Diff line change
@@ -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 */
}
15 changes: 15 additions & 0 deletions test/integration/logger-test-case/newrelic.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
30 changes: 30 additions & 0 deletions test/integration/logger.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -29,6 +31,7 @@ tap.test('logger', function (t) {
resolve()
}
})
delete process.env.NEW_RELIC_LOG
})

t.test('configuration from environment', function (t) {
Expand All @@ -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()
})
}
})

0 comments on commit c85c006

Please sign in to comment.