-
Notifications
You must be signed in to change notification settings - Fork 833
feat(cli): Add log:list CLI command #6136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,188 @@ | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Copyright 2026 Google LLC | ||||||||||||||||||
| * | ||||||||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||||||||
| * You may obtain a copy of the License at | ||||||||||||||||||
| * | ||||||||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
| * | ||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||||||||
| * limitations under the License. | ||||||||||||||||||
| */ | ||||||||||||||||||
|
|
||||||||||||||||||
| import type { LogQueryFilter } from '@genkit-ai/tools-common'; | ||||||||||||||||||
| import type { BaseRuntimeManager } from '@genkit-ai/tools-common/manager'; | ||||||||||||||||||
| import { | ||||||||||||||||||
| findProjectRoot, | ||||||||||||||||||
| forceStderr, | ||||||||||||||||||
| logger, | ||||||||||||||||||
| } from '@genkit-ai/tools-common/utils'; | ||||||||||||||||||
| import { Command, Option } from 'commander'; | ||||||||||||||||||
| import { runWithManager } from '../utils/manager-utils'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export interface LogListOptions { | ||||||||||||||||||
| limit: string; | ||||||||||||||||||
| traceId?: string; | ||||||||||||||||||
| spanId?: string; | ||||||||||||||||||
| severity?: string; | ||||||||||||||||||
| continuationToken?: string; | ||||||||||||||||||
| format: 'text' | 'jsonl'; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Command to list logs. By default, logs are returned in reverse | ||||||||||||||||||
| * chronological order. | ||||||||||||||||||
| */ | ||||||||||||||||||
| export const logList = new Command('log:list') | ||||||||||||||||||
| .description('list logs') | ||||||||||||||||||
| .option('-l, --limit <number>', 'limit the number of returned logs', '15') | ||||||||||||||||||
| .option('--trace-id <id>', 'filter by trace ID') | ||||||||||||||||||
| .option('--span-id <id>', 'filter by span ID') | ||||||||||||||||||
| .option( | ||||||||||||||||||
| '--severity <severity>', | ||||||||||||||||||
| 'filter by severity (e.g., INFO, ERROR, WARNING)' | ||||||||||||||||||
|
Comment on lines
+46
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this filtering to that exact severity or to and above? I think the latter is a common pattern for logs |
||||||||||||||||||
| ) | ||||||||||||||||||
| .addOption( | ||||||||||||||||||
| new Option('-f, --format <format>', 'output format') | ||||||||||||||||||
| .choices(['text', 'jsonl']) | ||||||||||||||||||
| .default('text') | ||||||||||||||||||
| ) | ||||||||||||||||||
| .option('--continuation-token <token>', 'continuation token for pagination') | ||||||||||||||||||
| .action(async (options: LogListOptions) => { | ||||||||||||||||||
| if (options.format === 'jsonl') forceStderr(); | ||||||||||||||||||
| const projectRoot = await findProjectRoot(); | ||||||||||||||||||
|
|
||||||||||||||||||
| const runAction = async (manager: BaseRuntimeManager) => { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const filter: LogQueryFilter = {}; | ||||||||||||||||||
| if (options.traceId) { | ||||||||||||||||||
| filter.traceId = options.traceId; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (options.spanId) { | ||||||||||||||||||
| filter.spanId = options.spanId; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (options.severity) { | ||||||||||||||||||
| filter.severityText = options.severity; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const limit = Number.parseInt(options.limit, 10); | ||||||||||||||||||
| if (Number.isNaN(limit) || limit <= 0) { | ||||||||||||||||||
| logger.error( | ||||||||||||||||||
| `Invalid limit: "${options.limit}". It must be a positive integer.` | ||||||||||||||||||
| ); | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const listRequest = { | ||||||||||||||||||
| limit, | ||||||||||||||||||
| continuationToken: options.continuationToken, | ||||||||||||||||||
| filter: Object.keys(filter).length > 0 ? filter : undefined, | ||||||||||||||||||
| }; | ||||||||||||||||||
|
shrutip90 marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
| const response = await manager.listLogs(listRequest); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!response || !response.logs || response.logs.length === 0) { | ||||||||||||||||||
| logger.info('No logs found.'); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this log to stdout? If so that may be distruptive in jsonl mode? |
||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const logs = response.logs; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (options.format === 'jsonl') { | ||||||||||||||||||
| logs.forEach((log) => { | ||||||||||||||||||
| console.log(JSON.stringify(log)); | ||||||||||||||||||
| }); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| console.log( | ||||||||||||||||||
| `Found ${logs.length} log${logs.length === 1 ? '' : 's'}:\n` | ||||||||||||||||||
| ); | ||||||||||||||||||
| logs.forEach((log) => { | ||||||||||||||||||
| let time = 'unknown'; | ||||||||||||||||||
| if (log.timestamp) { | ||||||||||||||||||
| time = new Date(log.timestamp).toLocaleString(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+104
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||||||||||||||||||
|
|
||||||||||||||||||
| const id = log.logId || 'unknown'; | ||||||||||||||||||
| const severity = log.severityText || 'unknown'; | ||||||||||||||||||
| const message = formatBody(log.body); | ||||||||||||||||||
| const attributes = formatAttributes(log.attributes); | ||||||||||||||||||
|
|
||||||||||||||||||
| console.log(`ID: ${id}`); | ||||||||||||||||||
| if (!options.traceId && log.traceId) | ||||||||||||||||||
| console.log(`Trace ID: ${log.traceId}`); | ||||||||||||||||||
| if (!options.spanId && log.spanId) | ||||||||||||||||||
| console.log(`Span ID: ${log.spanId}`); | ||||||||||||||||||
| console.log(`Severity: ${severity}`); | ||||||||||||||||||
| console.log(`Time: ${time}`); | ||||||||||||||||||
| if (message) console.log(`Message: ${message}`); | ||||||||||||||||||
| if (attributes) console.log(`Attrs: ${attributes}`); | ||||||||||||||||||
|
|
||||||||||||||||||
| console.log('---'); | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (response.continuationToken) { | ||||||||||||||||||
| if (options.format === 'jsonl') { | ||||||||||||||||||
| logger.info( | ||||||||||||||||||
| `To get the next page, use: --continuation-token ${response.continuationToken}` | ||||||||||||||||||
| ); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| console.log( | ||||||||||||||||||
| `\nTo get the next page, use: --continuation-token ${response.continuationToken}` | ||||||||||||||||||
| ); | ||||||||||||||||||
|
Comment on lines
+130
to
+136
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth mentioning here and in trace-list that user should use log-get or trace-get to fetch the full log? |
||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } catch (e) { | ||||||||||||||||||
| logger.error(`Error listing logs: ${e}`); | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| await runWithManager(projectRoot, runAction); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| function formatBody(value: unknown): string { | ||||||||||||||||||
| if (value === undefined || value === null) return ''; | ||||||||||||||||||
| let strValue: string; | ||||||||||||||||||
| if (typeof value === 'object') { | ||||||||||||||||||
| try { | ||||||||||||||||||
| strValue = JSON.stringify(value); | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| strValue = '[Object]'; | ||||||||||||||||||
| } | ||||||||||||||||||
| } else { | ||||||||||||||||||
| strValue = String(value); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // If it's a long string and doesn't match patterns, limit it | ||||||||||||||||||
| return strValue.length > 100 ? strValue.substring(0, 100) + '...' : strValue; | ||||||||||||||||||
| } | ||||||||||||||||||
|
shrutip90 marked this conversation as resolved.
|
||||||||||||||||||
|
|
||||||||||||||||||
| function formatAttributes( | ||||||||||||||||||
| attributes: Record<string, unknown> | undefined | ||||||||||||||||||
| ): string { | ||||||||||||||||||
| if (!attributes || Object.keys(attributes).length === 0) return ''; | ||||||||||||||||||
| const pairs = Object.entries(attributes).map(([key, value]) => { | ||||||||||||||||||
| let strValue: string; | ||||||||||||||||||
| if (typeof value === 'object' && value !== null) { | ||||||||||||||||||
| try { | ||||||||||||||||||
| strValue = JSON.stringify(value); | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| strValue = '[Object]'; | ||||||||||||||||||
| } | ||||||||||||||||||
| } else { | ||||||||||||||||||
| strValue = String(value); | ||||||||||||||||||
| } | ||||||||||||||||||
| const truncated = | ||||||||||||||||||
| strValue.length > 50 ? strValue.substring(0, 50) + '...' : strValue; | ||||||||||||||||||
| return `${key}=${truncated}`; | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| const fullString = pairs.join(', '); | ||||||||||||||||||
| return fullString.length > 100 | ||||||||||||||||||
| ? fullString.substring(0, 100) + '...' | ||||||||||||||||||
| : fullString; | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the behaviour if neither of these are specified? Will the most recent logs be printed? Worth documenting, and also worth mentioning that while traceId and spanId are optional, they are recommended to be provided (at least traceId). Not sure if there's any use case where that is not true....