diff --git a/core/src/common.ts b/core/src/common.ts index 5a86d491a7..41d881b264 100644 --- a/core/src/common.ts +++ b/core/src/common.ts @@ -393,6 +393,7 @@ export {getClientLabels, runWithClientLabel} from './utils/client_labels.js'; export { LogLevel, getLogger, + logger, resetLogger, setLogLevel, setLogger, diff --git a/core/src/utils/logger.ts b/core/src/utils/logger.ts index 348815e1c1..3d8ec41f27 100644 --- a/core/src/utils/logger.ts +++ b/core/src/utils/logger.ts @@ -101,7 +101,11 @@ export function setLogger(customLogger: Logger | null): void { } /** - * Gets the current logger instance. + * Gets the logger instance that is currently installed. + * + * The result is a snapshot. A later {@link setLogger} call swaps the instance, + * and this reference keeps writing to the old one. The {@link logger} facade + * resolves the installed instance on every call. */ export function getLogger(): Logger { return currentLogger; @@ -132,7 +136,11 @@ export function setLogLevel(level: LogLevel) { } /** - * The logger instance for ADK. + * The ADK logger. + * + * Every method forwards to the instance installed by {@link setLogger} at call + * time, so this object is safe to capture at module scope, including across a + * {@link setLogger} call with `null` to disable logging. */ export const logger: Logger = { setLogLevel(level: LogLevel): void { diff --git a/core/test/utils/logger_test.ts b/core/test/utils/logger_test.ts index 6256adbe2a..de723ed3d7 100644 --- a/core/test/utils/logger_test.ts +++ b/core/test/utils/logger_test.ts @@ -6,6 +6,7 @@ import { getLogger, + logger, Logger, LogLevel, resetLogger, @@ -24,6 +25,18 @@ function readCoreSource(name: string): Promise { ); } +/** Builds a logger that appends each call it receives to `sink`. */ +function recordingLogger(sink: string[]): Logger { + return { + setLogLevel: (level) => sink.push(`setLogLevel:${LogLevel[level]}`), + log: (level, ...args) => sink.push(`log:${LogLevel[level]}:${args[0]}`), + debug: (...args) => sink.push(`debug:${args[0]}`), + info: (...args) => sink.push(`info:${args[0]}`), + warn: (...args) => sink.push(`warn:${args[0]}`), + error: (...args) => sink.push(`error:${args[0]}`), + }; +} + describe('setLogger', () => { beforeEach(() => { resetLogger(); @@ -321,3 +334,97 @@ describe('browser safety', () => { expect(source).toMatch(/from 'winston'/); }); }); + +describe('logger (public export)', () => { + beforeEach(() => { + resetLogger(); + setLogLevel(LogLevel.DEBUG); + }); + + afterEach(() => { + resetLogger(); + }); + + it('is reachable from the package entry point', () => { + expect(logger).toBeDefined(); + for (const method of [ + 'log', + 'debug', + 'info', + 'warn', + 'error', + 'setLogLevel', + ] as const) { + expect(typeof logger[method]).toBe('function'); + } + }); + + it('routes to a logger installed after the reference was taken', () => { + const sink: string[] = []; + + setLogger(recordingLogger(sink)); + logger.warn('late bound'); + + expect(sink).toEqual(['warn:late bound']); + }); + + it('forwards every level', () => { + const sink: string[] = []; + setLogger(recordingLogger(sink)); + + logger.debug('d'); + logger.info('i'); + logger.warn('w'); + logger.error('e'); + logger.log(LogLevel.INFO, 'l'); + + expect(sink).toEqual([ + 'debug:d', + 'info:i', + 'warn:w', + 'error:e', + 'log:INFO:l', + ]); + }); + + it('forwards setLogLevel', () => { + const sink: string[] = []; + setLogger(recordingLogger(sink)); + + logger.setLogLevel(LogLevel.ERROR); + + expect(sink).toEqual(['setLogLevel:ERROR']); + }); + + it('goes silent when logging is disabled after the reference was taken', () => { + const sink: string[] = []; + setLogger(recordingLogger(sink)); + setLogger(null); + + expect(() => logger.warn('dropped')).not.toThrow(); + expect(sink).toEqual([]); + }); + + it('follows setLogger where a cached getLogger() result does not', () => { + const first: string[] = []; + const second: string[] = []; + setLogger(recordingLogger(first)); + const cached = getLogger(); + setLogger(recordingLogger(second)); + + cached.warn('stale'); + logger.warn('fresh'); + + expect(first).toEqual(['warn:stale']); + expect(second).toEqual(['warn:fresh']); + }); + + it('reaches a spy installed on the getLogger() instance', () => { + const warnSpy = vi.spyOn(getLogger(), 'warn').mockImplementation(() => {}); + + logger.warn('spied'); + + expect(warnSpy).toHaveBeenCalledWith('spied'); + warnSpy.mockRestore(); + }); +});