Composer 中的 ThingWorx 模型定义 > 系统 > 日志 > 面向 ThingWorx Platform 日志记录的 OpenTelemetry > 用于从外部源查询日志的自定义日志检索策略
用于从外部源查询日志的自定义日志检索策略
可通过市场上不同的第三方企业日志记录提供者应用程序实现日志的可观测性。这些应用程序有多种推送和检索日志的方法。
使用 OpenTelemetry 可将日志推送至日志记录提供者应用程序。
此日志检索策略的主要用途是从第三方源检索日志并在 Composer > “监控” > “日志”页面显示这些日志。
在整个实施过程中,ThingWorx 提供了以下内容:
默认策略事物 DefaultLogRetrievalStrategyThing,用于从 ThingWorx 文件日志和 Sumo Logic 检索日志,具体视配置而定。
用于开发自定义日志检索策略的 Extension SDK。
DefaultLogRetrievalStrategyThing 为系统对象。只有其配置部分才能更新。用户不应将此 DefaultLogRetrievalStrategyThing 模板用于实施自定义日志检索策略。
日志检索由随 DefaultLogRetrievalStrategyThing 提供的 RetrieveLogs 服务处理。日志检索可分为以下两种情况:
如果禁用文件日志记录 (DisableFileLogging 设置为 true),且启用了 OpenTelemetry (EnableOpenTelemetryForLogging 设置为 true),则从 Sumo Logic 检索日志。
对于所有其他情况,将从文件中检索日志。
若要自定义实施日志检索,可通过以下 2 种方式实现 RetrieveLogs 服务。
Composer 中通过 JavaScript 服务实现
a. Composer 中创建基本模板为 LogRetrievalStrategy 的事物。
b. 转至“服务”并覆盖 RetriveLogs 服务。
c. 提供 JavaScript 代码以从第三方日志记录软件检索日志。
* 
内容加载程序功能或其他类似功能通常用于与第三方通信。
通过使用 Extension SDK 在 ThingWorx 中创建扩展
a. 使用最新的 SDK (>=9.6.0) 在 Eclipse 中创建扩展
b. 创建基本模板为 LogRetrievalStrategy 的事物模板 (java 类)。
您的类应对日志检索策略类起扩展作用。
c. 覆盖检索日志 java 方法,并执行自定义代码以从第三方日志记录软件检索日志。
d. (可选) 更新扩展的 metadata.xml 并创建事物。导入扩展后,也可稍后于 Composer 中创建事物。
e. 构建扩展并将其导入 ThingWorx。
在上述两种情况下,一旦创建了事物,就必须将所创建的事物设置为日志记录子系统中的 LogRetrievalStrategy 事物。
本教程不涉及有关创建 ThingWorx 扩展的基础知识。我们假定:
您已知道如何构建 ThingWorx 扩展。
您已拥有包括 LogRetrievalStrategy 事物模板的 Extension SDK 9.6.0 或更高版本。
您已知道如何构建扩展以及如何将其导入 ThingWorx Platform。
实施自定义日志检索策略
1. 要用 Java 创建自定义日志检索策略,首先要创建一个策略事物模板。此事物模板可充当基于 Java 算法的代码库。事物模板的类定义如下:
@ThingworxBaseTemplateDefinition(name = "LogRetrievalStrategy")
public class CustomLogRetrievalStrategy extends LogRetrievalStrategy{
2. 实现并覆盖 RetrieveLogs 方法
@ThingworxServiceDefinition(
name = LoggingConstants.RETRIEVE_LOGS_STRATEGY_SERVICE_NAME,
isAllowOverride = true,
description = "Retrieve Logs from the given source",
category = "Queries")
@ThingworxServiceResult(name = "result", description = "Table entries", baseType = "INFOTABLE", aspects = { "dataShape:LogEntry" })
@Override
public InfoTable RetrieveLogs(
@ThingworxServiceParameter(
name = LoggingConstants.MAX_ITEMS,
description = "Maximum number of items to return",
baseType = "NUMBER",
aspects = { "defaultValue:" + RESTAPIConstants.DEFAULT_ITEM_COUNT }) Double maxItems,
@ThingworxServiceParameter(
name = LoggingConstants.START_DATE,
description = "Start time",
baseType = "DATETIME") DateTime startDate,
@ThingworxServiceParameter(
name = LoggingConstants.END_DATE,
description = "End time",
baseType = "DATETIME") DateTime endDate,
@ThingworxServiceParameter(
name = LoggingConstants.FROM_LOG_LEVEL,
description = "From log level",
baseType = "STRING") String fromLogLevel,
@ThingworxServiceParameter(
name = LoggingConstants.TO_LOG_LEVEL,
description = "To log level",
baseType = "STRING") String toLogLevel,
@ThingworxServiceParameter(
name = LoggingConstants.USER,
description = "Log messages for a specific user",
baseType = "USERNAME") String user,
@ThingworxServiceParameter(
name = LoggingConstants.THREAD,
description = "Log messages for a specific thread",
baseType = "STRING") String thread,
@ThingworxServiceParameter(
name = LoggingConstants.PLATFORM_ID,
description = "Log message from a specific instance",
baseType = "STRING") String platformId,
@ThingworxServiceParameter(
name = LoggingConstants.ORIGIN,
description = "Specific class or log source",
baseType = "STRING") String origin,
@ThingworxServiceParameter(
name = LoggingConstants.INSTANCE,
description = "Specific class or log source",
baseType = "STRING") String instance,
@ThingworxServiceParameter(
name = LoggingConstants.SEARCH_EXPRESSION,
description = "Keywords to search content",
baseType = "STRING") String searchExpression,
@ThingworxServiceParameter(
name = LoggingConstants.SORT_FIELD_NAME,
description = "Sort field name",
baseType = "STRING") String sortFieldName,
@ThingworxServiceParameter(
name = LoggingConstants.ASCENDING_SEARCH,
description = "Ascending search indicator (true = ascending, false = descending)",
baseType = "BOOLEAN") Boolean ascendingSearch,
@ThingworxServiceParameter(
name = LoggingConstants.OLDEST_FIRST,
description = "Search/sort from oldest to newest",
baseType = "BOOLEAN") Boolean oldestFirst,
@ThingworxServiceParameter(
name = LoggingConstants.IS_REGEX,
description = "The Search expression should be treated as REGEX",
baseType = "BOOLEAN",
aspects = { "defaultValue:" + false }) Boolean isRegex,
@ThingworxServiceParameter(
name = "logName",
description = "Keywords to search content",
baseType = "STRING",
aspects = { Aspects.ASPECT_ISREQUIRED + ":true", Aspects.ASPECT_SELECTOPTIONS +
":ApplicationLog:ApplicationLog|CommunicationLog:CommunicationLog|" +
"ConfigurationLog:ConfigurationLog|ScriptLog:ScriptLog|SecurityLog:SecurityLog" }) String logName)
throws Exception {

// Create InfoTable to hold results
InfoTable logEntriesTable = new InfoTable();

// Add Field Definitions to InfoTable.
logEntriesTable.addField(new FieldDefinition(LoggingConstants.CONTENT, BaseTypes.STRING));
logEntriesTable.addField(new FieldDefinition(LoggingConstants.USER, BaseTypes.STRING));
logEntriesTable.addField(new FieldDefinition(LoggingConstants.THREAD, BaseTypes.STRING));
logEntriesTable.addField(new FieldDefinition(LoggingConstants.PLATFORM_ID, BaseTypes.STRING));
logEntriesTable.addField(new FieldDefinition(LoggingConstants.ORIGIN, BaseTypes.STRING));
logEntriesTable.addField(new FieldDefinition(LoggingConstants.INSTANCE, BaseTypes.STRING));

// TODO: Custom Log Retrieval Strategy implementation here. Add each log entry as row to logEntriesTable

return logEntriesTable;
3. 在扩展的 metadata.xml 文件中创建策略事物。
<Things>
<Thing
enabled="true"
name="CustomLogRetrievalStrategyThing"
published="true"
thingTemplate="CustomLogRetrievalStrategy">
</Thing>
</Things>
4. 封装并构建扩展。
5. 通过 ThingWorx Composer 导入扩展。
6. 基于事物模板 CustomLogRetrievalStrategy 创建事物。
7. 转至 LoggingSubsystem > “配置”页面。
8. 选择在步骤 6 中创建的事物作为“日志检索策略”
9. 转至 Composer > “监控” > ApplicationLog 并验证是否从外部源检索日志。
这对您有帮助吗?