Skip to content

Conversation

@mlingner
Copy link

@mlingner mlingner commented Jan 29, 2026

Summary

Attempts to address #84

This PR adds a setLogger() function that allows users to provide a custom logger implementation. This is a simpler implementation but follows the general pattern used by OpenTelemetry and other JS libraries.

This enables:

  • Disabling ANSI color codes for cloud logging (GCP, etc.)
  • Using custom logging libraries (Winston, Pino)
  • Completely disabling logging

Changes

  • Added setLogger(customLogger: Logger | null) to set a custom logger or disable logging
  • Added getLogger() to retrieve the current logger instance
  • Added NoOpLogger class for when logging is disabled
  • Maintained backward compatibility - existing logger import still works
  • Added tests

Usage Examples

Disable logging entirely:

import {setLogger} from '@google/adk';
setLogger(null);

Plain logger without ANSI colors (for GCP/cloud):

import {setLogger, Logger, LogLevel} from '@google/adk';

const plainLogger: Logger = {
  log: (level, ...args) => console.log(`[ADK ${LogLevel[level]}]:`, ...args),
  debug: (...args) => console.debug('[ADK DEBUG]:', ...args),
  info: (...args) => console.info('[ADK INFO]:', ...args),
  warn: (...args) => console.warn('[ADK WARN]:', ...args),
  error: (...args) => console.error('[ADK ERROR]:', ...args),
};
setLogger(plainLogger);

Use Winston for structured JSON logging:

import {setLogger, Logger} from '@google/adk';
import winston from 'winston';

const winstonInstance = winston.createLogger({
  format: winston.format.json(),
  transports: [new winston.transports.Console()],
});

const winstonLogger: Logger = {
  log: (level, ...args) => winstonInstance.log(['debug','info','warn','error'][level], args.join(' ')),
  debug: (...args) => winstonInstance.debug(args.join(' ')),
  info: (...args) => winstonInstance.info(args.join(' ')),
  warn: (...args) => winstonInstance.warn(args.join(' ')),
  error: (...args) => winstonInstance.error(args.join(' ')),
};
setLogger(winstonLogger);

Test Plan

  • Run unit tests: npx vitest run core/test/utils/logger_test.ts
  • Tested locally with all three scenarios above

Copy link
Collaborator

@kalenkevich kalenkevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mlingner
Copy link
Author

Addressed merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants