diff --git a/src/cli/utils/logger.spec.ts b/src/cli/utils/logger.spec.ts index 0db7df6c0..02f535909 100644 --- a/src/cli/utils/logger.spec.ts +++ b/src/cli/utils/logger.spec.ts @@ -7,6 +7,9 @@ describe('cli/utils/logger', () => { beforeEach(() => { vi.resetModules(); delete env.CONTAINERBASE_LOG_LEVEL; + delete env.CONTAINERBASE_LOG_FORMAT; + delete env.LOG_LEVEL; + delete env.LOG_FORMAT; delete env.CONTAINERBASE_LOG_FILE; delete env.CONTAINERBASE_LOG_FILE_LEVEL; delete env.CONTAINERBASE_DEBUG; @@ -32,4 +35,39 @@ describe('cli/utils/logger', () => { }, ); }); + + test('works - stdout with json', async () => { + env.CONTAINERBASE_LOG_FORMAT = 'json'; + const { pino } = await import('pino'); + const mod = await import('./logger'); + expect(mod.logger).toBeDefined(); + expect(pino).toHaveBeenCalledWith( + { level: 'info' }, + { + targets: [{ target: 'pino/file', level: 'info', options: {} }], + }, + ); + }); + + test('works - debug stdout with json with file', async () => { + env.LOG_FORMAT = 'json'; + env.CONTAINERBASE_LOG_LEVEL = 'warn'; + env.CONTAINERBASE_LOG_FILE = 'test.ndjson'; + const { pino } = await import('pino'); + const mod = await import('./logger'); + expect(mod.logger).toBeDefined(); + expect(pino).toHaveBeenCalledWith( + { level: 'debug' }, + { + targets: [ + { target: 'pino/file', level: 'warn', options: {} }, + { + target: 'pino/file', + level: 'debug', + options: { destination: 'test.ndjson' }, + }, + ], + }, + ); + }); }); diff --git a/src/cli/utils/logger.ts b/src/cli/utils/logger.ts index 98cad6675..aa9719140 100644 --- a/src/cli/utils/logger.ts +++ b/src/cli/utils/logger.ts @@ -11,10 +11,18 @@ const level = .filter(isNonEmptyStringAndNotWhitespace) .shift() ?? 'info'; +const format = + [env.CONTAINERBASE_LOG_FORMAT, env.LOG_FORMAT] + .filter(isNonEmptyStringAndNotWhitespace) + .shift() + ?.toLowerCase() ?? 'pretty'; + +const stdoutTransportTarget = format === 'json' ? 'pino/file' : 'pino-pretty'; + let fileLevel = 'silent'; const targets: TransportTargetOptions[] = [ - { target: 'pino-pretty', level, options: {} }, + { target: stdoutTransportTarget, level, options: {} }, ]; if (isNonEmptyStringAndNotWhitespace(env.CONTAINERBASE_LOG_FILE)) {