From 3ca9661447a2da767ae12e33ad0f52dd755c1dbc Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:16:59 -0700 Subject: [PATCH 1/3] feat(core): export the logger facade from the public API getLogger() returns a snapshot, so a consumer who caches it in a module-level const never sees a later setLogger(). The logger facade resolves the installed instance on every call, but it was only reachable from inside the package. Export it, and document the lifetime rule on both symbols. --- core/src/common.ts | 1 + core/src/utils/logger.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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..443c3f0ffc 100644 --- a/core/src/utils/logger.ts +++ b/core/src/utils/logger.ts @@ -101,7 +101,12 @@ 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 a reference captured before that call keeps writing to the old one. Do + * not hold the result in a module-level `const`. Use the {@link logger} facade + * instead, which resolves the installed instance on every call. */ export function getLogger(): Logger { return currentLogger; @@ -132,7 +137,12 @@ export function setLogLevel(level: LogLevel) { } /** - * The logger instance for ADK. + * The ADK logger, and the recommended way to log through ADK. + * + * Every method forwards to the instance installed by {@link setLogger} at the + * moment it is called, so this object is safe to capture once at module scope. + * It keeps tracking the installed logger, including {@link setLogger} with + * `null` to disable logging. */ export const logger: Logger = { setLogLevel(level: LogLevel): void { From f5baf1b5c69c0079ddbf4429117bf11e4c0bce72 Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:18:01 -0700 Subject: [PATCH 2/3] test(core): cover the public logger export Pin the late-binding guarantee through '@google/adk', including the contrast with a cached getLogger() result. --- core/test/utils/logger_test.ts | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) 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(); + }); +}); From 0c78315e892b852ad60f1f70528b972f376bdc79 Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:18:02 -0700 Subject: [PATCH 3/3] docs(core): trim the logger doc comments to the facts The getLogger comment told callers not to cache the result in a module-level const, which two in-tree call sites still do. The logger comment called the facade the recommended way to log, which is a separate decision from exporting it. Both comments now state only the lifetime behaviour. --- core/src/utils/logger.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/src/utils/logger.ts b/core/src/utils/logger.ts index 443c3f0ffc..3d8ec41f27 100644 --- a/core/src/utils/logger.ts +++ b/core/src/utils/logger.ts @@ -104,9 +104,8 @@ export function setLogger(customLogger: Logger | null): void { * Gets the logger instance that is currently installed. * * The result is a snapshot. A later {@link setLogger} call swaps the instance, - * and a reference captured before that call keeps writing to the old one. Do - * not hold the result in a module-level `const`. Use the {@link logger} facade - * instead, which resolves the installed instance on every call. + * 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; @@ -137,12 +136,11 @@ export function setLogLevel(level: LogLevel) { } /** - * The ADK logger, and the recommended way to log through ADK. + * The ADK logger. * - * Every method forwards to the instance installed by {@link setLogger} at the - * moment it is called, so this object is safe to capture once at module scope. - * It keeps tracking the installed logger, including {@link setLogger} with - * `null` to disable logging. + * 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 {