Skip to content

Commit 72a112c

Browse files
authored
fix(logging): logger functions do not handle varargs (#5680)
Problem: Regression b2ebc10 in the logger: `debug('%s %s', foo, bar')` sends `foo, bar` as a single `[foo, bar]` list to the first `%s`. Example: 2024-09-26 12:18:29.631 [debug] getClientId: determined clientId as: [ '96be923b-a00d-46d4-add1-c877abfcb01c', undefined, '96be923b-a00d-46d4-add1-c877abfcb01c' ], process.env clientId was: { logID: 1 }, stored clientId was: %s Solution: Spread the args when passing to sendToLog().
1 parent 0c4289b commit 72a112c

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

packages/core/src/shared/logger/logger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ export interface Logger {
2828

2929
export abstract class BaseLogger implements Logger {
3030
debug(message: string | Error, ...meta: any[]): number {
31-
return this.sendToLog('debug', message, meta)
31+
return this.sendToLog('debug', message, ...meta)
3232
}
3333
verbose(message: string | Error, ...meta: any[]): number {
34-
return this.sendToLog('verbose', message, meta)
34+
return this.sendToLog('verbose', message, ...meta)
3535
}
3636
info(message: string | Error, ...meta: any[]): number {
37-
return this.sendToLog('info', message, meta)
37+
return this.sendToLog('info', message, ...meta)
3838
}
3939
warn(message: string | Error, ...meta: any[]): number {
40-
return this.sendToLog('warn', message, meta)
40+
return this.sendToLog('warn', message, ...meta)
4141
}
4242
error(message: string | Error, ...meta: any[]): number {
43-
return this.sendToLog('error', message, meta)
43+
return this.sendToLog('error', message, ...meta)
4444
}
4545
log(logLevel: LogLevel, message: string | Error, ...meta: any[]): number {
4646
return this.sendToLog(logLevel, message, ...meta)

packages/core/src/test/globalSetup.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function writeLogsToFile(testName: string) {
157157
appendFileSync(testLogOutput, entries?.join('\n') ?? '', 'utf8')
158158
}
159159

160+
// TODO: merge this with `toolkitLogger.test.ts:checkFile`
160161
export function assertLogsContain(text: string, exactMatch: boolean, severity: LogLevel) {
161162
assert.ok(
162163
getTestLogger()

packages/core/src/test/shared/logger/toolkitLogger.test.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { ToolkitError } from '../../../shared/errors'
1616
* Disposes the logger then waits for the write streams to flush. The `expected` and `unexpected` arrays just look
1717
* for any occurence within the file, passing if all expected strings were found or failing if any unexpected strings
1818
* were found.
19+
*
20+
* TODO: merge this with `globalSetup.test.ts:assertLogsContain`
1921
*/
2022
async function checkFile(
2123
logger: ToolkitLogger,
@@ -34,18 +36,22 @@ async function checkFile(
3436
const contents = fs.readFileSync(logPath.fsPath)
3537

3638
// Error if unexpected messages are in the log file
37-
const explicitUnexpectedMessages = unexpected
39+
const foundUnexpected = unexpected
3840
.filter((t) => contents.includes(t))
3941
.reduce((a, b) => a + `Unexpected message in log: ${b}\n`, '')
40-
if (explicitUnexpectedMessages) {
41-
return reject(new Error(explicitUnexpectedMessages))
42+
if (foundUnexpected) {
43+
return reject(new Error(foundUnexpected))
4244
}
4345

44-
// Error if any of the expected messages are not in the log file
45-
const expectedMessagesNotInLogFile = expected.filter((t) => !contents.includes(t))
46-
if (expectedMessagesNotInLogFile.length > 0) {
46+
// Fail if any of the expected messages are not in the log file
47+
const notFound = expected.filter((t) => !contents.includes(t))
48+
if (notFound.length > 0) {
49+
const last10Lines = contents.toString().split('\n').slice(-10)
4750
reject(
48-
new Error(expectedMessagesNotInLogFile.reduce((a, b) => a + `Unexpected message in log: ${b}\n`, ''))
51+
new Error(
52+
notFound.reduce((a, b) => a + `Expected message not found in log: ${b}\n`, '') +
53+
`\n\n Last 10 log lines:\n${last10Lines.join('\n')}`
54+
)
4955
)
5056
}
5157

@@ -106,32 +112,32 @@ describe('ToolkitLogger', function () {
106112
const happyLogScenarios = [
107113
{
108114
name: 'logs debug',
109-
logMessage: (logger: ToolkitLogger, message: string) => {
110-
logger.debug(message)
115+
logMessage: (logger: ToolkitLogger, msg: string, args: any[]) => {
116+
logger.debug(msg, ...args)
111117
},
112118
},
113119
{
114120
name: 'logs verbose',
115-
logMessage: (logger: ToolkitLogger, message: string) => {
116-
logger.verbose(message)
121+
logMessage: (logger: ToolkitLogger, msg: string, args: any[]) => {
122+
logger.verbose(msg, ...args)
117123
},
118124
},
119125
{
120126
name: 'logs info',
121-
logMessage: (logger: ToolkitLogger, message: string) => {
122-
logger.info(message)
127+
logMessage: (logger: ToolkitLogger, msg: string, args: any[]) => {
128+
logger.info(msg, ...args)
123129
},
124130
},
125131
{
126132
name: 'logs warn',
127-
logMessage: (logger: ToolkitLogger, message: string) => {
128-
logger.warn(message)
133+
logMessage: (logger: ToolkitLogger, msg: string, args: any[]) => {
134+
logger.warn(msg, ...args)
129135
},
130136
},
131137
{
132138
name: 'logs error',
133-
logMessage: (logger: ToolkitLogger, message: string) => {
134-
logger.error(message)
139+
logMessage: (logger: ToolkitLogger, msg: string, args: any[]) => {
140+
logger.error(msg, ...args)
135141
},
136142
},
137143
]
@@ -174,13 +180,15 @@ describe('ToolkitLogger', function () {
174180

175181
happyLogScenarios.forEach((scenario) => {
176182
it(scenario.name, async () => {
177-
const message = `message for ${scenario.name}`
183+
const msg = `message for ${scenario.name} arg1 %s, arg2 %O`
184+
const args = [42, ['a', 'b']]
185+
const expectedMsg = `message for ${scenario.name} arg1 42, arg2 [ 'a', 'b' ]`
178186
testLogger = new ToolkitLogger('debug')
179187
testLogger.logToFile(tempLogPath)
180188

181-
scenario.logMessage(testLogger, message)
189+
scenario.logMessage(testLogger, msg, args)
182190

183-
await checkFile(testLogger, tempLogPath, [message])
191+
await checkFile(testLogger, tempLogPath, [expectedMsg])
184192
})
185193
})
186194
})
@@ -297,7 +305,7 @@ describe('ToolkitLogger', function () {
297305

298306
const waitForMessage = waitForLoggedTextByCount(1)
299307

300-
scenario.logMessage(testLogger, message)
308+
scenario.logMessage(testLogger, message, [])
301309

302310
assert.ok((await waitForMessage).includes(message), 'Expected error message to be logged')
303311
})

0 commit comments

Comments
 (0)