在设计和开发自定义集成策略时,需要提醒您有关策略的任何问题。 通过定义自定义日志记录策略来记录上下文变量的值,可以快速提醒您策略可能出现故障的地方,以便您可以纠正任何错误。
本教程描述了一个用户定义的策略,以帮助在API Connect组装工具中进行日志记录和错误诊断。 在此策略中,自定义字符串或上下文变量可以在运行API的任何时候直接写入IBM®DataPower®日志。 通过定义这样的策略,可以以任何优先级级别编写消息。 而且,当您将API Connect与DataPower的功能结合起来以选择性地侦听不同的优先级时,在设计API时,您将获得直接而强大的日志记录和诊断。
API Connect中的策略
使用API Connect组装工具,您可以向API的运行时行为添加转换和逻辑。 您可以使用该程序集创建具有逻辑结构的复杂流,例如if
和switch
语句。 您还可以创建数据处理机制,例如字段编辑,JSON到XML和输入到输出映射。 这些操作称为策略 。 您可以通过将图块拖动到装配体来连接和组织它们。
writeToDatapowerLog策略
要解决在使用程序集时记录错误诊断中已确定的挑战,可以使用用户定义的策略。 在本文中,我们使用并描述writeToDatapowerLog策略。 (您可以从本文结尾的“可下载资源”部分下载此策略。)
您将此策略作为压缩(.zip)文件导入API Connect。 压缩文件除了包含描述策略参数和元数据的YAML文件之外,还包含DataPower在运行时运行策略规则所需的所有文件。 无需提取或处理档案。
要使用该策略, 请将压缩文件导入API Connect 。 导入后,该策略将显示在程序集中,如下图底部所示,可以使用了。
现在,您可以根据需要将策略拖动到程序集中多次。 磁贴的每个实例都拥有自己的独特配置。 该策略使用以下两个参数:
- logLevel 。 此参数指示记录的消息的优先级。 DataPower具有一系列优先级,范围从信息到关键 。 这些级别可从策略配置内的下拉列表中获得。
- 留言 。 此参数是指要写入DataPower日志的消息。 Message参数完全支持DataPower运行时上下文变量 。
下图显示了一个示例程序集,该程序集使用writeToDatapowerLog策略的两个单独的实例。
下图显示了两个实例在API Connect程序集中的配置。 第一个实例(请参见下图)显示了严重情况的消息。
第二个实例(请参见下图)使用上下文变量支持在日志错误消息中记录API名称。
API调用中这些实例的结果显示在DataPower日志中,如下图所示。 此图中显示的两个消息都是根据我们先前描述的策略配置创建的。 顶部的第一条消息显示了API的名称(writetodatapowerlog-api),该名称是从上下文变量引用$(api.name)
扩展而来的。 底部的第二条消息以第一个实例中指定的关键级别记录。
这两个消息都是gatewayscript-user类别的一部分,因为主要规则逻辑是通过使用DataPower中的gatewayscript描述的。 下一节显示了该策略的规则编码的扩展版本。
网关脚本代码
该策略的主要执行围绕以下网关脚本构建。
function writeToDatapowerLog()
{
var apic = require('local://isp/policy/apim.custom.js');
var props = apic.getPolicyProperty(),
apiName = apic.getContext('api.name'),
m = props.message;
if (props === undefined) {
console.error('The policy properties were not retrieved for policy: writeToDatapowerLog.');
return;
}
if (apiName === undefined || apiName === '') {
console.error('The name of the API for this call could not be determined.');
}
switch(props.logLevel) {
case 'info':
console.info('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'notice':
console.notice('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'warn':
console.warn('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'error':
console.error('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'critical':
console.critical('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'alert':
console.alert('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'emerg':
console.emerg('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'log':
console.log('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
case
'trace':
console.trace('The API: \"' + apiName + '\" has logged the following message: ' + m); break;
default:
console.error('The logLevel chosen: ' + props.logLevel + ' was not recognised');
}
return;
}
writeToDatapowerLog();
有关更多信息,请参阅IBM Knowledge Center中的“ 实施策略和网关脚本”主题。
结论
本教程介绍了一种通用的解决方案,可帮助您解决使用DataPower运行时和API Connect程序集时记录灵活性的挑战。 通过使用如上所述的用户定义策略,您可以将特定优先级的自定义消息直接记录到DataPower日志中,并完全支持运行时上下文变量。 该解决方案功能强大,可创建有用的消息,以在开发过程中进行错误检测和诊断,并作为运行时监视设备。
翻译自: https://www.ibm.com/developerworks/library/mw-1610-phillips-trs/index.html