Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export enum LogLevel {
ERROR = 3,
}

/** The winston level name each {@link LogLevel} is logged under. */
const WINSTON_LEVEL: Readonly<Record<LogLevel, string>> = {
[LogLevel.DEBUG]: 'debug',
[LogLevel.INFO]: 'info',
[LogLevel.WARN]: 'warn',
[LogLevel.ERROR]: 'error',
};

/**
* Logger interface for ADK.
*/
Expand Down Expand Up @@ -68,7 +76,7 @@ class SimpleLogger implements Logger {
return;
}

this.logger.log(level.toString(), messages.join(' '));
this.logger.log(WINSTON_LEVEL[level], messages.join(' '));
}

debug(...messages: unknown[]): void {
Expand Down
61 changes: 60 additions & 1 deletion core/test/utils/logger_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
*/

import {getLogger, Logger, LogLevel, setLogger, setLogLevel} from '@google/adk';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import * as winston from 'winston';
import {resetLogger} from '../../src/utils/logger.js';

/** winston records the resolved level name under this triple-beam symbol. */
const LEVEL = Symbol.for('level');

/** Reads the winston level name off a record captured at the transport. */
function winstonLevelOf(record: unknown): string {
if (typeof record !== 'object' || record === null) {
return '';
}
return String(Reflect.get(record, LEVEL));
}

describe('setLogger', () => {
beforeEach(() => {
resetLogger();
Expand Down Expand Up @@ -142,3 +154,50 @@ describe('setLogger', () => {
});
});
});

describe('SimpleLogger.log', () => {
let records: unknown[];

beforeEach(() => {
records = [];
vi.spyOn(winston.transports.Console.prototype, 'log').mockImplementation(
(...args: unknown[]) => {
records.push(args[0]);
const next = args[1];
if (typeof next === 'function') {
next();
}
},
);
resetLogger();
});

afterEach(() => {
vi.restoreAllMocks();
resetLogger();
});

it.each([
['debug', LogLevel.DEBUG],
['info', LogLevel.INFO],
['warn', LogLevel.WARN],
['error', LogLevel.ERROR],
])('writes a %s record under that winston level name', (name, level) => {
const logger = getLogger();
logger.setLogLevel(LogLevel.DEBUG);

logger.log(level, 'hello');

expect(records).toHaveLength(1);
expect(winstonLevelOf(records[0])).toBe(name);
});

it('suppresses a record below the configured level', () => {
const logger = getLogger();
logger.setLogLevel(LogLevel.WARN);

logger.log(LogLevel.INFO, 'hidden');

expect(records).toHaveLength(0);
});
});
Loading