Composer 中的 ThingWorx 模型定義 > 系統 > 記錄 > 適用於 ThingWorx Platform 記錄的 OpenTelemetry > 針對從外部來源查詢記錄檔自訂記錄檔擷取策略
針對從外部來源查詢記錄檔自訂記錄檔擷取策略
記錄檔可觀察性可透過市面上提供的不同第三方企業記錄提供者應用程式實現。這些應用程式有多種方法可用來推送及擷取記錄檔。
透過 OpenTelemetry,可將記錄檔推送至記錄提供者應用程式。
此「記錄檔擷取策略」的主要目的是從第三方來源擷取記錄檔,並在 Composer > 「監視」 > 「記錄」頁顯示這些記錄檔。
作為整體實行的一部份,ThingWorx 提供下列項目:
根據組態從「ThingWorx 檔案」記錄檔與 Sumo Logic 擷取記錄檔的預設策略物件 DefaultLogRetrievalStrategyThing
用來開發自訂記錄檔擷取策略的延伸功能 SDK。
DefaultLogRetrievalStrategyThing 是系統物件。只有它的組態可以更新。使用者不應使用此 DefaultLogRetrievalStrategyThing 範本來實行自訂記錄檔擷取策略。
記錄檔擷取由 DefaultLogRetrievalStrategyThing 提供的 RetrieveLogs 服務處理。記錄檔擷取的基礎為,
如果禁用檔案記錄 (DisableFileLogging 設定為 true),且已啟用 OpenTelemetry (EnableOpenTelemetryForLogging 設定為 true),則只會從 sumo logic 擷取記錄檔。
對於其他所有情況,都會從檔案擷取記錄檔。
對於自訂實行記錄檔擷取的情況,可透過下列 2 種方式實行 RetrieveLogs 服務。
透過 Composer 使用 JavaScript 服務
a. Composer 使用基礎範本 LogRetrievalStrategy 建立物件。
b. 轉至「服務」並取代 RetriveLogs 服務。
c. 提供您的 JavaScript 程式碼以從第三方記錄軟體擷取記錄檔。
* 
通常,內容載入程式功能或類似功能可用來與第三方通訊。
透過使用延伸功能 SDK 在 ThingWorx 中建立延伸功能
a. 使用最新 SDK (>=9.6.0) 在 Eclipse 中建立延伸功能
b. 使用基礎範本 LogRetrievalStrategy 建立物範本 (java 類別)。
您的類別應延伸「記錄檔擷取策略」類別。
c. 取代「擷取記錄檔」java 方法,並實行您的自訂程式碼以從第三方記錄軟體擷取記錄檔。
d. (選用) 更新延伸功能的 metadata.xml 並建立物件。您也可以稍後於匯入延伸功能之後在 Composer 中建立物件。
e. 建構延伸功能,並將其匯入至 ThingWorx。
在上述兩種情況下,當您擁有物件之後,您需要在「記錄子系統」中將此物件設定為 LogRetrievalStrategy 物件。
本教學專區不包含建立 ThingWorx 延伸功能的基本內容。它假設您:
瞭解如何建構 ThingWorx 延伸功能。
擁有 Extension SDK 9.6.0 版或更新版本,其中包括 LogRetrievalStrategy 物範本。
知道如何建構延伸功能以及如何將其匯入至 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 > 「監視」 > 「應用程式記錄檔」,並核對記錄檔是否為從外部來源所擷取。
這是否有幫助?