/** * commands/log/exception.ts — 异常日志 (P0 AI 调试核心入口) * * v0.11+ Phase 2: webapi 化 (loadLog.getLogList + category=4) * 替代 v0.10 直接 c.get */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadLog } from '../../webapi/log.ts' interface ListOpts { keyword?: string json?: boolean limit?: string } export function buildLogExceptionCommand(): Command { const cmd = new Command('exception') .description('异常日志 (Java 异常堆栈, AI 调试核心入口, v0.11+ webapi)') .option('--keyword ', '按账号/异常名/URL 模糊搜索') .option('--limit ', '最多取 N 条 (默认 20)', '20') .option('--json', '原始 JSON 输出', false) .action(async (opts: ListOpts) => { const c = await JnpfClient.fromToken() await initWebapi(c) // v0.11+ Phase 2: 走 webapi 镜像 const r = (await loadLog.getLogList({ category: '4', keyword: opts.keyword, currentPage: 1, pageSize: opts.limit || '20', })) as { list?: Record[] } if (!r || (r as { code?: number }).code !== 200 && (r as { code?: number }).code !== undefined) { log.error(`log exception 失败: ${(r as { msg?: string }).msg}`) process.exit(1) } const list = r.list || [] if (opts.json) { printJson(r) return } log.info(`异常日志 ${list.length} 条 (category=4, 找后端 Java 异常堆栈)`) for (const d of list) { const time = String(d.createTime || d.F_CreatorTime || '').slice(0, 19) const user = String(d.userName || d.F_UserId || '-') const url = String(d.requestUrl || d.F_RequestUrl || '-') const module = String(d.moduleName || d.F_ModuleName || '-') const method = String(d.requestMethod || d.F_RequestMethod || '-') console.log(`[${time}] ${user} ${method} ${url} (${module})`) const json = String(d.json || d.F_Json || '') if (json) { console.log(' ↳ 异常详情:') for (const line of json.split('\n').slice(0, 30)) console.log(` ${line}`) if (json.split('\n').length > 30) console.log(` ... (共 ${json.split('\n').length} 行, --json 拿全)`) } } }) installAiHelpHook(cmd, { purpose: '异常日志 (Java 异常堆栈, AI 调试后端错误核心入口).', optional: { '--keyword': '按账号/异常名/URL 模糊搜索', '--limit': '最多取 N 条', '--json': '原始 JSON 输出' }, example: [ 'dacli log exception', 'dacli log exception --keyword="NullPointer"', 'dacli log exception --keyword=form_create --limit=5', ], source: { web: 'web/apps/jnpf-web-apps-main/src/views/sysLog/errorLog/index.vue', api: 'GET /api/system/Log?category=4', ref: 'web/.../api/system/log.ts getLogList()', }, notes: ['v0.11+ 改用 webapi 镜像 (loadLog.getLogList + category=4)'], }) return cmd }