import { execa } from 'execa' import { fastify } from 'fastify' import { deepStrictEqual, ok } from 'node:assert' import { writeFile } from 'node:fs/promises' import { join, resolve } from 'node:path' import { afterEach, test } from 'node:test' import { Agent, getGlobalDispatcher, request, setGlobalDispatcher } from 'undici' import { transform } from '../index.js' import { startPath } from './cli/helper.js' import { createRuntime, execRuntime, isWindows, requestAndDump, stdioOutputToLogs, updateFile } from './helpers.js' import { prepareRuntime } from './multiple-workers/helper.js' setGlobalDispatcher(new Agent({ keepAliveTimeout: 10, keepAliveMaxTimeout: 10 })) afterEach(async () => { await getGlobalDispatcher().close() setGlobalDispatcher(new Agent({ keepAliveTimeout: 10, keepAliveMaxTimeout: 10 })) }) test('should use full logger options - formatters, timestamp, redaction', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options', 'platformatic.json') let requested = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/logs' }) requested = true }, done: message => { return requested } }) const logs = stdioOutputToLogs(stdout) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg === 'Starting the worker 0 of the application "app"...' ) ) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg === 'Started the worker 0 of the application "app"...' ) ) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg.startsWith('Platformatic is now listening at http://127.0.0.1:') ) ) }) test('should inherit full logger options from runtime to a platformatic/application', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options', 'platformatic.json') let requested = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/logs' }) requested = true }, done: message => { return requested && message.includes('call route /logs') } }) const logs = stdioOutputToLogs(stdout) ok( logs.find( log => log.stdout.level === 'DEBUG' && log.stdout.time.length === 24 && // isotime log.stdout.name === 'service' && log.stdout.msg === 'Loading envfile...' ) ) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg === 'Starting the worker 0 of the application "app"...' ) ) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg === 'Started the worker 0 of the application "app"...' ) ) ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg.startsWith('Platformatic is now listening at http://127.0.0.1:') ) ) ok( logs.find(log => { if ( log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'app' ) { return ( log.stdout.level === 'DEBUG' && log.stdout.time.length === 24 && // isotime log.stdout.name === 'service' && log.stdout.secret === '***HIDDEN***' && log.stdout.msg === 'call route /logs' ) } return false }) ) }) test('should inherit full logger options from runtime to different applications', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options-all', 'platformatic.json') let requested = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/logs' }) requested = true }, done: message => { return requested } }) const logs = stdioOutputToLogs(stdout) for (const t of ['composer', 'service', 'node']) { ok( logs.find( log => log.level === 'INFO' && log.time.length === 24 && // isotime log.name === 'service' && log.msg === `Started the worker 0 of the application "${t}"...` ) ) } }) test('should get json logs from thread applications when they are not pino default config', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options-all', 'platformatic.json') let incomingRequest = false let requestCompleted = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/' }) }, done: message => { for (const log of stdioOutputToLogs([message]).filter(log => log.caller === 'STDOUT')) { incomingRequest ||= log.stdout.msg === 'incoming request' requestCompleted ||= log.stdout.msg === 'request completed' } return incomingRequest && requestCompleted } }) const logs = stdioOutputToLogs(stdout).filter(log => log.caller === 'STDOUT') ok( logs.find(log => { return ( log.stdout.level === 'INFO' && log.stdout.time.length === 24 && // isotime log.stdout.name === 'service' && log.stdout.msg === 'incoming request' ) }) ) ok( logs.find(log => { return ( log.stdout.level === 'INFO' && log.stdout.time.length === 24 && // isotime log.stdout.name === 'service' && log.stdout.msg === 'request completed' ) }) ) }) test( 'should handle logs from thread applications as they are with captureStdio: false', { skip: isWindows }, async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-no-capture', 'platformatic.json') let serviceResponded = false let nodeResponded = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { serviceResponded ||= message.includes('call route / on service') nodeResponded ||= message.includes('call route / on node') return serviceResponded && nodeResponded } }) const logs = stdioOutputToLogs(stdout) ok( logs.find(log => { return log.nodeLevel === 'debug' && log.name === 'node' && log.msg === 'call route / on node' }) ) ok( logs.find(log => { return log.applicationLevel === 'debug' && log.name === 'service' && log.msg === 'call route / on service' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "node"...' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "service"...' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "composer"...' }) ) } ) test('should handle logs from thread applications as they are with captureStdio: false and managementApi: false', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-no-capture-no-mgmt-api', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('call route / on service')) { responses++ } else if (message.includes('call route / on node')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) ok( logs.find(log => { return log.nodeLevel === 'debug' && log.name === 'node' && log.msg === 'call route / on node' }) ) ok( logs.find(log => { return log.applicationLevel === 'debug' && log.name === 'service' && log.msg === 'call route / on service' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "node"...' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "service"...' }) ) ok( logs.find(log => { return log.customLevelName === 'info' && log.msg === 'Starting the worker 0 of the application "composer"...' }) ) }) test('should use base and messageKey options with an explicit default pino message key', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options-base-message-key', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('call route / on service')) { responses++ } else if (message.includes('call route / on node')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) ok( logs.every(log => { return ( log.customBaseName === 'a' && log.customBaseItem === 'b' && (log.theMessage ? log.theMessage.length > 0 : true) && (log.stdout ? log.stdout.theMessage.length > 0 : true) ) }) ) ok(logs.some(log => log.stdout?.theMessage === 'call route / on service')) }) test('should use configured pino message key to detect thread application logs', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options-base-message-key-pino', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('call route / on service')) { responses++ } else if (message.includes('call route / on node')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) ok( logs.find(log => { return log.name === 'service' && log.theMessage === 'call route / on service' && !log.stdout }) ) ok( logs.find(log => { return log.name === 'node' && log.theMessage === 'call route / on node' && !log.stdout }) ) }) test('should inherit output keys when detecting runtime, worker and application logs', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-custom-output-keys', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('call route / on service') || message.includes('call route / on node')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) for (const message of ['Loading envfile...', 'call route / on service', 'call route / on node']) { const log = logs.find(log => log.message === message) ok(log, message) ok(!log.stdout) deepStrictEqual(log.severity, 20) deepStrictEqual(new Date(log.timestamp).toISOString(), log.timestamp) ok(!Object.hasOwn(log, 'time')) ok(!Object.hasOwn(log, 'level')) } ok(logs.some(log => log.message?.startsWith('Started the worker') && log.severity === 30)) }) for (const fixture of ['logger-options-base-message-key', 'logger-options-null-base']) { test(`should omit missing PIDs in pretty logs with ${fixture}`, async () => { const configPath = join(import.meta.dirname, '..', 'fixtures', fixture, 'platformatic.json') const { stdout } = await execRuntime({ configPath, env: { FORCE_TTY: 'true', PLT_PRETTY_PRINT: 'true', NO_COLOR: 'true' }, done: message => message.includes('Platformatic is now listening at') }) const output = stdout.join('') ok(output.includes('INFO')) ok(!output.includes('(undefined)')) ok(!output.includes('(null)')) const listeningLine = output.split('\n').find(line => line.includes('Platformatic is now listening at')) ok(!/\(\d+\)/.test(listeningLine)) }) } test('should pretty print custom output keys', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-custom-output-keys', 'platformatic.json') const { stdout } = await execRuntime({ configPath, env: { FORCE_TTY: 'true', PLT_PRETTY_PRINT: 'true', NO_COLOR: 'true' }, onReady: async ({ url }) => { await requestAndDump(url, { path: '/node/' }) }, done: message => message.includes('call route / on node') }) const output = stdout.join('') ok(output.includes('INFO')) ok(output.includes('call route / on node')) ok(output.includes('node:0')) ok(/\[\d{2}:\d{2}:\d{2}\.\d+\]/.test(output)) ok(!output.includes('stdout:')) ok(!output.includes('severity:')) }) test('should prefer explicit pino keys over output keys when detecting thread application logs', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-custom-pino-keys', 'platformatic.json') let requested = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/custom-pino-keys' }) requested = true }, done: message => { return requested && message.includes('custom pino keys') } }) const logs = stdioOutputToLogs(stdout) ok(logs.some(log => log.outputMessage?.startsWith('Started the worker') && log.outputLevel === 30 && typeof log.outputTime === 'number')) ok( logs.find(log => { return log.severity === 'INFO' && typeof log.timestamp === 'number' && log.message === 'custom pino keys' && !log.stdout }) ) }) test('should use null base in options', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-options-null-base', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('call route / on service')) { responses++ } else if (message.includes('call route / on node')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) ok( logs.every(log => { const keys = Object.keys(log) return !keys.includes('pid') && !keys.includes('hostname') }) ) }) test('should use custom config', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-custom-config', 'platformatic.json') let responses = 0 const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/service/' }) await requestAndDump(url, { path: '/node/' }) }, done: message => { if (message.includes('request completed')) { responses++ } return responses > 1 } }) const logs = stdioOutputToLogs(stdout) ok( logs.every(log => { const keys = Object.keys(log) return ( typeof log.severity === 'string' && log.message.length > 0 && !keys.includes('pid') && !keys.includes('hostname') ) }) ) }) test('should use colors when printing applications logs', async t => { const root = await prepareRuntime(t, 'multiple-workers', { node: ['node'] }) const configFile = resolve(root, './platformatic.json') await updateFile(configFile, data => { const config = JSON.parse(data) config.logger.level = 'info' return JSON.stringify(config, null, 2) }) const child = execa(process.execPath, [startPath, configFile], { env: { FORCE_TTY: 'true', FORCE_COLOR: 'true', PLT_USE_PLAIN_CREATE: true }, cwd: root }) child.catch(() => {}) const promise = Promise.withResolvers() let stdout = '' child.stdout.on('data', chunk => { stdout += chunk.toString() if (stdout.includes('Platformatic is now listening')) { child.kill('SIGKILL') promise.resolve() } }) await promise.promise ok( stdout.match( // eslint-disable-next-line no-control-regex, no-regex-spaces /\n\u001b\[38;5;\d+m\[\d{2}:\d{2}:\d{2}\.\d+\] \(\d+\) \w+:\d+ \|\u001b\[0m\u001b\[32m INFO\u001b\[39m: \u001b\[36m/ ) ) }) test('should use pretty logs when FORCE_TTY is set in .env', { skip: isWindows }, async t => { const root = await prepareRuntime(t, 'multiple-workers', { node: ['node'] }) const configFile = resolve(root, './platformatic.json') await writeFile(resolve(root, '.env'), 'FORCE_TTY=true', 'utf-8') const child = execa(process.execPath, [startPath, configFile], { env: { PLT_USE_PLAIN_CREATE: true }, cwd: root }) child.catch(() => {}) t.after(() => { child.kill('SIGKILL') }) const promise = Promise.withResolvers() let stdout = '' child.stdout.on('data', chunk => { stdout += chunk.toString() if (stdout.includes('Platformatic is now listening')) { child.kill('SIGKILL') promise.resolve() } }) await promise.promise ok(!stdout.trimStart().startsWith('{')) }) test('should use colors when FORCE_COLOR is set in .env', { skip: isWindows }, async t => { const root = await prepareRuntime(t, 'multiple-workers', { node: ['node'] }) const configFile = resolve(root, './platformatic.json') await updateFile(configFile, data => { const config = JSON.parse(data) config.logger.level = 'info' return JSON.stringify(config, null, 2) }) await writeFile(resolve(root, '.env'), 'FORCE_TTY=true\nFORCE_COLOR=true', 'utf-8') const child = execa(process.execPath, [startPath, configFile], { env: { PLT_USE_PLAIN_CREATE: true }, cwd: root }) child.catch(() => {}) t.after(() => { child.kill('SIGKILL') }) const promise = Promise.withResolvers() let stdout = '' child.stdout.on('data', chunk => { stdout += chunk.toString() if (stdout.includes('Platformatic is now listening')) { child.kill('SIGKILL') promise.resolve() } }) await promise.promise ok( stdout.match( // eslint-disable-next-line no-control-regex /\n\u001b\[38;5;\d+m\[\d{2}:\d{2}:\d{2}\.\d+\] \(\d+\) \w+:\d+ \|\u001b\[0m/ ) ) }) // Regression test for: TypeError: Cannot read properties of null (reading 'level') // This happens when a worker outputs the literal string "null" to stdout. // JSON.parse("null") returns null, and typeof null === 'object' (JS quirk), // so the code enters the object branch and tries to access null.level test('should handle literal null output from workers without crashing', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-null-output', 'platformatic.json') let requested = false const { stderr } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/null-output' }) requested = true }, done: message => { return requested && message.includes('null') } }) // The runtime should not crash - if we got here, the test passed ok(requested, 'Request was made successfully') // Verify no crash error in stderr const stderrText = stderr.join('') ok(!stderrText.includes('Cannot read properties of null'), 'No null property access error') }) // Regression test for logger level inheritance from runtime to applications. // Previously, the logger schema had `default: 'info'` which would override // the runtime-level logger setting when merging configs. test('should inherit logger level from runtime when service does not specify logger level', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-level-inheritance', 'platformatic.json') let requested = false let levelResponse = null await execRuntime({ configPath, onReady: async ({ url }) => { // Check what level the application's logger is using const levelRes = await request(url, { path: '/get-level' }) levelResponse = await levelRes.body.json() requested = true }, done: message => { return requested } }) // The service should have inherited 'debug' level from runtime, // not the old default 'info' from schema ok(levelResponse, 'Level response received') ok( levelResponse.level === 'debug', `Expected logger level to be 'debug' (inherited from runtime), but got '${levelResponse.level}'` ) }) // Same test but for @platformatic/node applications test('should inherit logger level from runtime when node app does not specify logger level', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-level-inheritance-node', 'platformatic.json') let requested = false let levelResponse = null await execRuntime({ configPath, onReady: async ({ url }) => { // Check what level the node application's logger is using const levelRes = await request(url, { path: '/get-level' }) levelResponse = await levelRes.body.json() requested = true }, done: message => { return requested } }) // The node app should have inherited 'debug' level from runtime, // not the old default 'info' from schema ok(levelResponse, 'Level response received') ok( levelResponse.level === 'debug', `Expected logger level to be 'debug' (inherited from runtime), but got '${levelResponse.level}'` ) }) test('should support additional pino options and inherit them in the applications', async t => { const configPath = join(import.meta.dirname, '..', 'fixtures', 'logger-pino-options', 'platformatic.json') let requested = false const { stdout } = await execRuntime({ configPath, onReady: async ({ url }) => { await requestAndDump(url, { path: '/logs' }) requested = true }, done: message => { return requested && message.includes('call route /logs') } }) const logs = stdioOutputToLogs(stdout) // The runtime logger uses msgPrefix ok(logs.find(log => log.msg?.startsWith('[PLT] Platformatic is now listening at http://127.0.0.1:'))) // The application logger inherits msgPrefix, nestedKey, customLevels and redact.remove const applicationLog = logs.find(log => log.msg === '[PLT] call route /logs') ok(applicationLog) deepStrictEqual(applicationLog.level, 25) deepStrictEqual(applicationLog.name, 'node-app') deepStrictEqual(applicationLog.payload, { hello: 'world' }) }) // Same test but for @platformatic/node applications test('should export logs to OpenTelemetry', async t => { function findAttribute (log, name) { return log.resourceLogs[0].resource.attributes.find(attr => attr.key === name) } const configFile = join(import.meta.dirname, '..', 'fixtures', 'logger-opentelemetry', 'platformatic.json') const { promise, resolve } = Promise.withResolvers() // Create a sample OpenTelemetry collector that listens for logs on a specific port and stores them in memory for assertions const server = fastify() t.after(() => server.close()) await server.post('/v1/logs', async request => { for (const resourceLog of request.body.resourceLogs) { for (const scopeLog of resourceLog.scopeLogs) { for (const logRecord of scopeLog.logRecords) { if (logRecord.body && logRecord.body.stringValue === 'Serving request') { resolve(request.body) } } } } return {} }) await server.listen({ port: 0 }) const app = await createRuntime(configFile, null, { async transform (config, ...args) { config = await transform(config, ...args) const url = new URL(config.logger.openTelemetryExporter.url) url.port = server.server.address().port config.logger.openTelemetryExporter.url = url.toString() return config } }) t.after(() => app.close()) const entryUrl = await app.start() { const { statusCode, body } = await request(entryUrl, { path: '/' }) deepStrictEqual(await statusCode, 200) deepStrictEqual(await body.text(), 'ok') } const log = await promise // Check that some attributes are present deepStrictEqual(findAttribute(log, 'service.name').value.stringValue, 'logger-opentelemetry') deepStrictEqual(findAttribute(log, 'service.version').value.stringValue, '1.0.0') deepStrictEqual(findAttribute(log, 'process.runtime.name').value.stringValue, 'nodejs') deepStrictEqual(findAttribute(log, 'process.runtime.description').value.stringValue, 'Node.js') // Check that OpenTelemetry trace and spans are present const scope = log.resourceLogs[0].scopeLogs[0].logRecords[0] deepStrictEqual(scope.body.stringValue, 'Serving request') ok(scope.traceId.match(/^[0-9a-f]+$/)) ok(scope.spanId.match(/^[0-9a-f]+$/)) deepStrictEqual(scope.flags, '01') })