Adaptive Insights集成脚本无法完整输出XML响应日志的问题求助
问题描述
我在Adaptive Insights集成脚本里写了testConnection函数,用来点击"Test Connection"按钮时调用API并记录返回的XML响应,方便后续开发验证。但目前日志里只能显示部分内容(末尾是"..."),单独输出单个字符(比如finalResponse[1])是正常的,尝试按1000字符切片输出后,日志却没有任何内容。代码如下:
function testConnection(context) { // dataSource variable to get Data Source Setting Parameters var dataSource = context.getDataSource(); // Get the static credentials parameters. const userName = dataSource.getSetting("Adaptive Username").getValue(); // Not Nedeed after WD User Sync. Do Not delete const password = dataSource.getSetting("Adaptive Password").getValue(); // Not Nedeed after WD User Sync. Do Not delete const instanceCode = dataSource.getSetting("instanceCode").getValue(); //This example connects to Postman Echo using POST var url = 'https://api.adaptiveinsights.com/api/v35'; var method = 'POST'; // XML request body, this is where the method type (ie. exportUsers) goes. var body = '<?xml version="1.0" encoding="UTF-8"?><call method="exportUsers" callerName="AdaptiveInsightsAPI">' body += '<credentials login="' + userName + '" password="' + password + '" /></call>'; var headers = { 'Content-Type': 'application/xml' }; var response = null; let finalResponse = null; try { response = ai.https.request(url, method, body, headers); finalResponse = response.getBody(); ai.log.logVerbose("API Call Successful", finalResponse); return true; } catch (exception) { ai.log.logInfo('api call failed', '' + exception); return false; } }
解决方案
这个问题主要是因为Adaptive Insights的日志函数对单条日志的字符长度有内置限制,超过阈值就会自动截断显示为"..."。另外你之前切片无输出可能是切片方式的问题,下面提供几种可行的解决办法:
1. 分块输出长字符串(推荐)
写一个辅助函数把长XML字符串切成小片段,逐段输出日志,这样就能避开单条日志的长度限制:
function testConnection(context) { // ... 保留原有代码 ... try { response = ai.https.request(url, method, body, headers); finalResponse = response.getBody(); // 新增分块日志函数 const logLongContent = (title, content) => { const chunkSize = 500; // 可根据日志限制调整大小 let currentIndex = 0; const totalChunks = Math.ceil(content.length / chunkSize); while (currentIndex < content.length) { const chunk = content.substring(currentIndex, currentIndex + chunkSize); ai.log.logVerbose(`${title} - Chunk ${Math.floor(currentIndex/chunkSize)+1}/${totalChunks}`, chunk); currentIndex += chunkSize; } }; // 调用分块日志函数代替原日志输出 logLongContent("API Call Successful", finalResponse); return true; } catch (exception) { ai.log.logInfo('api call failed', '' + exception); return false; } }
2. 用JSON.stringify包裹输出(快速验证)
如果只是临时验证内容,可以把XML字符串用JSON.stringify包裹,这样能保留所有特殊字符,且日志系统通常会完整输出JSON格式的内容:
// 在try块中替换原日志行 ai.log.logVerbose("API Call Successful", JSON.stringify(finalResponse));
注意:这种方式会给字符串添加双引号,适合快速排查,正式记录建议用分块输出。
3. 检查日志级别限制
不同日志级别的长度限制可能不同,比如logVerbose的限制比logInfo更严格。可以临时换成logInfo测试是否能输出更长内容,但注意生产环境不要用logInfo输出大量调试信息:
ai.log.logInfo("API Call Successful", finalResponse);
关于之前切片无输出的排查
你尝试切片后无日志,大概率是切片方式的问题。比如如果用了finalResponse[0, 1000]这种错误语法(这不是正确的字符串切片方式),应该用finalResponse.substring(0, 1000)或者finalResponse.slice(0, 1000)。另外可以先输出切片后的长度,确认是否有内容:
const testChunk = finalResponse.substring(0, 1000); ai.log.logVerbose("Test chunk length", testChunk.length); ai.log.logVerbose("Test chunk content", testChunk);
内容的提问来源于stack exchange,提问作者David M

