feat: 为模板管理API添加详细日志记录

- 为executeTemplate方法添加完整的执行日志
  - 记录开始时间、参数信息
  - 添加参数验证和错误日志
  - 记录执行耗时和结果统计
  - 详细的异常信息和堆栈跟踪
- 为getTemplate方法添加查询日志
  - 记录请求的模板代码
  - 显示查找结果和模板信息
- 为getTemplates方法添加列表日志
  - 记录模板数量和列表信息
- 移除无效的webhook日志输出
- 提升调试和监控能力
This commit is contained in:
杨明明 2025-09-03 18:42:26 +08:00
parent b2cf85f51d
commit 52c22ab9bd
2 changed files with 62 additions and 2 deletions

View File

@ -13,7 +13,14 @@ export class AppController {
@Get() @Get()
async getTemplates(): Promise<ApiResponse<Template[]>> { async getTemplates(): Promise<ApiResponse<Template[]>> {
console.log(`📋 [获取模板列表] 请求获取所有模板`);
console.log(`⏰ 请求时间: ${new Date().toISOString()}`);
const templates = this.tempalte.getAllTemplates(); const templates = this.tempalte.getAllTemplates();
console.log(`✅ [获取模板列表] 成功获取 ${templates.length} 个模板`);
console.log(`📊 模板列表: ${templates.map(t => t.code || t.name).join(', ')}`);
return { return {
status: true, status: true,
data: templates, data: templates,
@ -23,14 +30,24 @@ export class AppController {
@Get(':templateCode') @Get(':templateCode')
async getTemplate(@Param('templateCode') templateCode: string): Promise<ApiResponse<Template | null>> { async getTemplate(@Param('templateCode') templateCode: string): Promise<ApiResponse<Template | null>> {
console.log(`🔍 [获取模板] 请求获取模板: ${templateCode}`);
console.log(`⏰ 请求时间: ${new Date().toISOString()}`);
const template = this.tempalte.getTemplate(templateCode); const template = this.tempalte.getTemplate(templateCode);
if (template) { if (template) {
console.log(`✅ [获取模板] 模板找到: ${template.name || templateCode}`);
console.log(`📋 模板信息: ${JSON.stringify(template, null, 2)}`);
return { return {
status: true, status: true,
data: template, data: template,
msg: 'success' msg: 'success'
}; };
} }
console.log(`❌ [获取模板] 模板不存在: ${templateCode}`);
return { return {
status: false, status: false,
data: null, data: null,
@ -40,27 +57,71 @@ export class AppController {
@Post() @Post()
async executeTemplate(@Body('imageUrl') imageUrl: string, @Body('templateCode') templateCode: string): Promise<ApiResponse<string | null>> { async executeTemplate(@Body('imageUrl') imageUrl: string, @Body('templateCode') templateCode: string): Promise<ApiResponse<string | null>> {
const startTime = Date.now();
console.log(`🚀 [模板执行] 开始执行模板`);
console.log(`📋 模板代码: ${templateCode}`);
console.log(`🖼️ 图片URL: ${imageUrl}`);
console.log(`⏰ 开始时间: ${new Date().toISOString()}`);
try { try {
// 验证输入参数
if (!templateCode) {
console.log(`❌ [模板执行] 参数验证失败: 模板代码为空`);
return {
status: false,
data: null,
msg: '模板代码不能为空'
};
}
if (!imageUrl) {
console.log(`❌ [模板执行] 参数验证失败: 图片URL为空`);
return {
status: false,
data: null,
msg: '图片URL不能为空'
};
}
console.log(`✅ [模板执行] 参数验证通过,开始执行模板处理`);
const result = await this.tempalte.executeTemplate(templateCode, imageUrl); const result = await this.tempalte.executeTemplate(templateCode, imageUrl);
const executionTime = Date.now() - startTime;
if (result) { if (result) {
console.log(`🎉 [模板执行] 执行成功`);
console.log(`📊 执行结果长度: ${result.length} 字符`);
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
console.log(`✨ 完成时间: ${new Date().toISOString()}`);
return { return {
status: true, status: true,
data: result, data: result,
msg: 'success' msg: 'success'
}; };
} }
console.log(`⚠️ [模板执行] 执行完成但结果为空`);
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
return { return {
status: false, status: false,
data: null, data: null,
msg: '执行失败' msg: '执行失败'
}; };
} catch (e) { } catch (e) {
const executionTime = Date.now() - startTime;
console.log(`💥 [模板执行] 执行异常`);
console.log(`❌ 错误信息: ${e.message}`);
console.log(`📍 错误堆栈: ${e.stack}`);
console.log(`⏱️ 执行耗时: ${executionTime}ms`);
console.log(`🔚 异常时间: ${new Date().toISOString()}`);
return { return {
status: false, status: false,
data: null, data: null,
msg: e.message msg: e.message
}; };
} }
} }
} }

View File

@ -21,6 +21,5 @@ async function bootstrap() {
console.log(`🚀 多平台小程序后台服务启动成功!`); console.log(`🚀 多平台小程序后台服务启动成功!`);
console.log(`📡 服务地址: http://localhost:${port}`); console.log(`📡 服务地址: http://localhost:${port}`);
console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`); console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`);
console.log(`🔗 Webhook API: http://localhost:${port}/api/v1/webhook`);
} }
bootstrap(); bootstrap();