您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
結果
如要追蹤 API 執行階段環境中的問題,最有效的方法之一就是記錄訊息。 您可以在 API 上附加及設定 MessageLogging 政策,將自訂訊息記錄到本機磁碟 (僅限 Edge for Private Cloud) 或系統記錄檔。
範例
系統記錄檔
<MessageLogging name="LogToSyslog">
<Syslog>
<Message>[3f509b58 tag="{organization.name}.{apiproxy.name}.{environment.name}"] Weather request for WOEID {request.queryparam.w}.</Message>
<Host>logs-01.loggly.com</Host>
<Port>514</Port>
<Protocol>TCP</Protocol>
<FormatMessage>true</FormatMessage>
<DateFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZ</DateFormat>
</Syslog>
<logLevel>ALERT</logLevel>
</MessageLogging>MessageLogging 政策類型的常見用途是記錄至系統記錄帳戶。如果設定為使用系統記錄檔,API Proxy 會將 Apigee Edge 的記錄訊息轉送至遠端系統記錄檔伺服器。您必須已備妥系統記錄伺服器。如果沒有,您可以使用 Splunk、Sumo Logic 和 Loggly 等公開記錄管理服務。詳情請參閱「設定第三方記錄管理服務」。
舉例來說,假設您需要記錄 API 從消費者應用程式收到的每個要求訊息相關資訊。值 3f509b58 代表 Loggly 服務專屬的鍵值。如果您有 Loggly 帳戶,請代入 Loggly 金鑰。產生的記錄訊息會填入四個值:與交易相關聯的機構、API Proxy 和環境名稱,以及要求訊息中查詢參數的值。
如果您有 Edge for Private Cloud 部署作業,也可以將記錄訊息寫入檔案。
透過 TLS/SSL 傳送系統記錄
<MessageLogging name="LogToSyslog">
<Syslog>
<Message>[3f509b58 tag="{organization.name}.{apiproxy.name}.{environment.name}"] Weather request for WOEID {request.queryparam.w}.</Message>
<Host>logs-01.loggly.com</Host>
<Port>6514</Port>
<Protocol>TCP</Protocol>
<FormatMessage>true</FormatMessage>
<SSLInfo>
<Enabled>true</Enabled>
</SSLInfo>
<DateFormat>yyMMdd-HH:mm:ss.SSS</DateFormat>
</Syslog>
<logLevel>WARN</logLevel>
</MessageLogging>您可以新增 <SSLInfo> 區塊,透過 TLS/SSL 將訊息傳送給第三方訊息記錄服務供應商。
檔案輪替:大小
<MessageLogging name="LogPolicy">
<File>
<Message>This is a test message. Message id : {request.header.messageid}</Message>
<FileName>test.log</FileName>
<FileRotationOptions rotateFileOnStartup="true">
<FileRotationType>SIZE</FileRotationType>
<MaxFileSizeInMB>10</MaxFileSizeInMB>
<MaxFilesToRetain>10</MaxFilesToRetain>
</FileRotationOptions>
</File>
<logLevel>ERROR</logLevel>
</MessageLogging>根據檔案大小輪替檔案。
檔案輪替:時間
<MessageLogging name="LogPolicy">
<File>
<Message>This is a test message. Message id : {request.header.messageid}</Message>
<FileName>test.log</FileName>
<FileRotationOptions rotateFileOnStartup="true">
<FileRotationType>TIME</FileRotationType>
<RotationFrequency unit="minute">10</RotationFrequency>
<MaxFilesToRetain>10</MaxFilesToRetain>
</FileRotationOptions>
</File>
<logLevel>ERROR</logLevel>
</MessageLogging>根據時間輪替檔案。
檔案輪替:時間和大小
<MessageLogging name="LogPolicy">
<File>
<Message>This is a test message. Message id : {request.header.messageid}</Message>
<FileName>test.log</FileName>
<FileRotationOptions rotateFileOnStartup="true">
<FileRotationType>TIME_SIZE</FileRotationType>
<MaxFileSizeInMB>10</MaxFileSizeInMB>
<MaxFilesToRetain>10</MaxFilesToRetain>
<RotationFrequency unit="minute">10</RotationFrequency>
</FileRotationOptions>
</File>
<logLevel>ERROR</logLevel>
</MessageLogging>根據時間和大小輪替檔案。
支援串流
<MessageLogging name="LogPolicy"> <File> .... .... </File> <BufferMessage>true</BufferMessage> </MessageLogging>
啟用串流的訊息記錄
元素參考資料
使用下列元素設定 MessageLogging 政策類型。
| 欄位名稱 | 欄位說明 | |
|---|---|---|
|
本機檔案目的地。(檔案記錄僅適用於 Private Cloud 部署作業的 Edge)。如要瞭解檔案的儲存位置,請參閱「Edge for Private Cloud 中的記錄檔位置」。 |
Message |
建構要傳送至記錄檔的訊息,並將文字與變數合併,擷取所需資訊。請參閱「範例」。 |
FileName |
記錄檔的基本名稱。
請勿指定檔案路徑。舉例來說,這個 FileName 元素指定了檔案路徑,因此無效:<FileName>/opt/apigee/var/log/messages/mylog.log</FileName> 這段程式碼只會指定檔案名稱,因此有效: <FileName>mylog.log</FileName> 如要瞭解檔案的儲存位置,請參閱「Edge for Private Cloud 中的記錄檔位置」。 |
|
FileRotationOptions |
||
rotateFileOnStartup |
屬性。有效值: 如果設為 true,系統會在每次重新啟動訊息引擎時輪替記錄檔。 |
|
FileRotationType |
指定記錄檔的輪替政策 (size 或 time)。 |
|
MaxFileSizeInMB |
(選取 size 做為輪替類型時)
指定記錄檔大小,達到這個大小時,伺服器就會將記錄訊息移至另一個檔案。記錄檔達到指定大小後,伺服器會重新命名目前的記錄檔。 |
|
RotationFrequency |
(選取 time 做為輪替類型時)
指定觸發伺服器將記錄訊息移至另一個檔案的時間 (以分鐘為單位)。經過指定間隔後,系統會重新命名目前的記錄檔。 |
|
MaxFilesToRetain |
指定輪替設定中要保留的檔案數量上限。預設值為 8。 如果指定零 (0),系統會無限期保留記錄檔,但仍須遵守檔案輪替設定,且不會刪除或重新命名任何檔案。因此,為避免日後發生磁碟空間已滿的錯誤,請將此值設為大於零,或實作定期自動清除或封存較舊保留記錄檔的系統。 |
|
BufferMessage |
如果為 Proxy 啟用 HTTP 串流,系統就不會緩衝處理要求/回應訊息。如要記錄需要剖析流程訊息的內容,請將 BufferMessage 設為 true。如需範例,請參閱「啟用串流」範例分頁。預設值:false |
|
|
syslog 目的地。如要將系統記錄檔傳送至 Splunk、Sumo Logic 或 Loggly,請參閱「設定第三方記錄管理服務」。 |
Message |
建構要傳送至系統記錄的訊息,將文字與變數合併,擷取所需資訊。請參閱範例。 注意:在 PostClientFlow 中,錯誤流程後不會提供回應變數。使用訊息變數記錄錯誤和成功情況的回應資訊。另請參閱使用注意事項。 |
Host |
應傳送系統記錄的伺服器主機名稱或 IP 位址。如未加入這個元素,預設值為 localhost。 | |
Port |
執行系統記錄的通訊埠。如未加入這個元素,預設值為 514。 | |
Protocol |
TCP 或 UDP (預設值)。雖然 UDP 的效能較高,但 TCP 通訊協定可確保訊息記錄檔傳送至系統記錄伺服器。透過 TLS/SSL 傳送系統記錄訊息時,僅支援 TCP。 | |
FormatMessage |
選用,但如要搭配 Loggly 使用,則必須提供 這個元素可讓您控制附加至訊息的 Apigee 產生內容格式。如果設為 true,syslog 訊息會以固定數量的字元做為前置字元,方便您從訊息中篩除該資訊。以下是固定格式的範例:
Apigee 產生的資訊包括:
如果設為 false (預設值),系統不會在訊息開頭加上這些固定字元。 |
|
PayloadOnly |
這個元素會將 Apigee 產生的訊息格式設為僅包含系統記錄訊息主體,不含 FormatMessage 指定的前置字元。 如未加入這個元素或將其留空,預設值為 請參閱 FormatMessage。 |
|
DateFormat |
選用項目。 用於設定每個記錄訊息時間戳記格式的範本字串。
根據預設,Apigee 會使用 |
|
SSLInfo |
可透過 SSL/TLS 記錄訊息。搭配子元素 如未加入這個元素或將其留空,預設值為 false (無 TLS/SSL)。 <SSLInfo>
<Enabled>true</Enabled>
</SSLInfo>您可以設定 <SSLInfo> 標記,與在 TargetEndpoint 上設定的方式相同,包括啟用雙向 TLS/SSL,詳情請參閱「API Proxy 設定參考資料」。僅支援 TCP 通訊協定。 |
|
logLevel |
選用項目。 有效值: 設定要在訊息記錄中加入的資訊詳細程度。 如果您使用 |
|
結構定義
使用須知
將 MessageLogging 政策附加至 API Proxy 流程時,請考慮將其放在 ProxyEndpoint 回應中,也就是名為 PostClientFlow 的特殊流程。回應傳送至要求用戶端後,PostClientFlow 會執行,確保所有指標都可供記錄。如要瞭解如何使用 PostClientFlow,請參閱 API Proxy 設定參考資料。
PostClientFlow 有兩項特殊之處:
- 只會在回應流程中執行。
- 這是 Proxy 進入錯誤狀態後執行的唯一流程。
因為無論 Proxy 是否成功或失敗,都會執行這項政策,所以您可以將 MessageLogging 政策放在 PostClientFlow 中,保證政策一律會執行。
下圖的追蹤記錄顯示 MessageLogging 政策在 DefaultFaultRule 執行後,於 PostClientFlow 中執行:

在本例中,由於金鑰無效,Verify API Key 政策導致錯誤。
以下是包含 PostClientFlow 的 ProxyEndpoint 定義:
<ProxyEndpoint name="default">
...
<PostClientFlow>
<Response>
<Step>
<Name>Message-Logging-1</Name>
</Step>
</Response>
</PostClientFlow>
...
</ProxyEndpoint>Edge 會將訊息記錄為簡單文字,您可以設定記錄檔,加入要求或回應的接收日期和時間、要求中的使用者身分、傳送要求的來源 IP 位址等變數。Edge 會非同步記錄訊息,因此不會因封鎖呼叫而導致 API 延遲。
MessageLogging 政策會將記憶體中記錄的訊息寫入緩衝區。訊息記錄器會從緩衝區讀取訊息,然後寫入您設定的目的地。每個目的地都有自己的緩衝區。
使用者可能會延遲收到傳送至新系統記錄端點的記錄訊息。這是因為政策中預期的「冷啟動」行為。設定新的記錄目的地時,除了現有的記錄目的地,訊息處理器 (MP) 也可能會先在記憶體中將 1000 則記錄訊息排入佇列,然後再傳送這些訊息。這可能會導致低流量環境中的初始延遲。對於一般生產環境工作負載,訊息應該會快速累積,因此這項初始延遲無法觀察到。達到門檻後,記錄訊息就會如預期傳送。對訊息處理器執行正常重新啟動,也可以觸發佇列訊息的傳送作業,因為這些訊息會從佇列中排空。
如果寫入緩衝區的速率超過讀取速率,緩衝區就會溢位,導致記錄作業失敗。如果發生這種情況,您可能會在記錄檔中看到包含下列內容的訊息:
Log message size exceeded. Increase the max message size setting
如果您在 Edge for Private Cloud 4.15.07 和更早版本中遇到這個問題,請找出 message-logging.properties 檔案並使用下列解決方案:
在 message-logging.properties 檔案中增加 max.log.message.size.in.kb 屬性 (預設值為 128 KB)。
如果是 Edge for Private Cloud 4.16.01 以上版本,請在 /opt/apigee/customer/application/message-processor.properties 檔案中設定 conf/message-logging.properties+max.
log.message.size.in.kb 屬性,然後重新啟動 Message Processor。請注意,這項屬性預設會註解掉。
注意:Edge 中的回覆訊息變數無法從錯誤流程取得。如果前一個流程是錯誤流程,這些變數也不會出現在 PostClientFlow 中。如要記錄 PostClientFlow 的回應資訊,請使用 message 物件。無論是否發生錯誤,您都可以使用這個物件從回應中取得標頭和其他資訊。如需詳細資訊和範例,請參閱「訊息變數」。
在 Edge for Private Cloud 中控制記錄訊息時間戳記
根據預設,所有記錄訊息中的時間戳記格式如下:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
您可以使用 DateFormat 元素,針對系統記錄目的地覆寫這項全系統預設值。如要瞭解這個範本的行為,請參閱 Java 的 SimpleDateFormat 類別說明文件。根據該定義,yyyy 會替換為 4 位數的年份,MM 會替換為 2 位數的月份編號,依此類推。上述格式可能會產生下列形式的字串:
2022-09-28T22:38:11.721+0000
您可以使用 Edge Message Processor 上的 conf_system_apigee.syslogger.dateFormat 屬性控管該格式。舉例來說,將訊息格式變更為:
yy/MM/dd'T'HH:mm:ss.SSSZ
..將破折號換成斜線,並縮短為 2 位數年份,以以下格式記錄時間戳記:
22/09/28T22:38:11.721+0000
如要變更格式,請按照下列步驟操作:
- 在編輯器中開啟 message-processor.properties 檔案。如果該檔案不存在,請建立該檔案:
> vi /opt/apigee/customer/application/message-processor.properties - 視需要設定屬性:
conf_system_apigee.syslogger.dateFormat=yy/MM/dd'T'HH:mm:ss.SSSZ - 儲存變更。
- 確認屬性檔案的擁有者為「apigee」使用者:
> chown apigee:apigee /opt/apigee/customer/application/message-processor.properties - 重新啟動 Edge Message Processor:
> /opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
Edge for Private Cloud 中的記錄檔位置
Edge for Private Cloud 4.16.01 以上版本
根據預設,私有雲訊息記錄檔位於訊息處理器節點的下列目錄中:
/opt/apigee/var/log/edge-message-processor/messagelogging/org_name/environment/api_proxy_name/revision/logging_policy_name/
如要變更預設記錄位置,請修改訊息處理器上 message-logging.properties 檔案中的屬性:
- bin_setenv_data_dir -
設定記錄檔儲存的根路徑。例如:
bin_setenv_data_dir=/opt/apigee/var/log - conf_message-logging_log.root.dir - 如果您將此項設為相對路徑 (例如
conf/message-logging.properties+log.root.dir=custom/folder/, the path is appended to the bin_setenv_data_dir location.
),或是絕對路徑 (例如conf/message-logging.properties+log.root.dir=/opt/apigee/var/log/messages),訊息記錄就會儲存在/opt/apigee/var/log/messages/messagelog/。絕對路徑的優先順序高於bin_setenv_data_dir。
請注意,您必須將屬性參照為 conf/message-logging.properties+log.root.dir,因為預設會註解掉該屬性。詳情請參閱「設定目前已註解的權杖」。
如要將記錄檔儲存在平面檔案結構中,讓所有記錄檔都放在同一個目錄,請在 message-logging.properties 檔案中,將 conf/message-logging.properties+enable.flat.directory.structure 設為 true。訊息會儲存在上述屬性指定的目錄中,檔案名稱格式為 {org}_{environment}_{api_proxy_name}_{revision}_{logging_policy_name}_{filename}。
如要設定這些屬性,請按照下列步驟操作:
- 在編輯器中開啟 message-processor.properties 檔案。如果該檔案不存在,請建立該檔案:
> vi /opt/apigee/customer/application/message-processor.properties - 視需要設定屬性:
conf/message-logging.properties+log.root.dir=/opt/apigee/var/log/messages - 儲存變更。
- 確認屬性檔案的擁有者為「apigee」使用者:
> chown apigee:apigee /opt/apigee/customer/application/message-processor.properties - 重新啟動 Edge 元件:
> /opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
Edge for Private Cloud 4.15.07 和更早版本
根據預設,訊息記錄檔位於訊息處理器的下列位置:
/opt/apigee4/var/log/apigee/message-processor/messagelog/{org}/{environment}/{api_proxy_name}/{revision}/{logging_policy_name}/
如要變更預設記錄位置,請在訊息處理器的 message-logging.properties 檔案中修改下列屬性:
- data.dir - 設定記錄檔儲存空間的根路徑。例如:data.dir=/opt/apigee4/var/log
- log.root.dir - 如果您將此屬性設為相對路徑 (例如 log.root.dir=custom/folder/),系統會將該路徑附加至 data.dir 位置。
舉例來說,這兩個屬性合併後,記錄目錄會設為 /opt/apigee4/var/log/custom/folder/messagelog/ (請注意,/messagelog 會自動新增)。
如果將此屬性設為絕對路徑,例如 log.root.dir=/opt/apigee4/var/log/messages,訊息記錄檔就會儲存在 /opt/apigee4/var/log/messages/messagelog/。log.root.dir 中的絕對路徑優先於 data.dir。
如要將記錄檔儲存在平面檔案結構中,讓所有記錄檔都放在同一個目錄,請在訊息處理器的 message-logging.properties 檔案中,將 enable.flat.directory.structure 屬性設為 true。訊息會儲存在上述屬性指定的目錄中,檔案名稱格式為 {org}_{environment}_{api_proxy_name}_{revision}_{logging_policy_name}_{filename}。
訊息範本中變數的預設值
您可以分別為訊息範本中的每個變數指定預設值。舉例來說,如果無法解析變數 request.header.id,系統會將其值替換為 unknown。
<Message>This is a test message. id = {request.header.id:unknown}</Message>您可以為所有未解析的變數指定通用預設值,方法是在 Message 元素上設定 defaultVariableValue 屬性:
<Message defaultVariableValue="unknown">This is a test message. id = {request.header.id}</Message>設定第三方記錄檔管理服務
您可以使用 MessageLogging 政策,將系統記錄訊息傳送至第三方記錄管理服務,例如 Splunk、Sumo Logic 和 Loggly。如要將系統記錄傳送至其中一項服務,請參閱該服務的說明文件,設定服務的主機、通訊埠和通訊協定,然後據此設定這項政策的 Syslog 元素。
如要瞭解第三方記錄檔管理設定,請參閱下列說明文件:
- Splunk (選取產品版本)
另請參閱這篇 Apigee 社群貼文:將記錄訊息寫入 Splunk -
Sumo
Logic
- 另請參閱這篇 Apigee 社群貼文: Setting up logging with Sumo Logic, which host should I use?
- 如需以 Sumo Logic 做為記錄服務的完整範例,請參閱下列 Apigee 社群貼文。這項解決方案會使用單一 JavaScript 政策,對 Sumo Logic HTTP 來源收集器發出 HTTP POST 要求:使用 JavaScript 和 HTTP 記錄至 Sumo Logic
- Loggly
使用 Loggly 時,政策中必須有<FormatMessage>true</FormatMessage>,做為<Syslog>元素的子項。
如要進一步瞭解如何將訊息記錄到 Loggly,請參閱這篇 Apigee 社群貼文: 將訊息記錄到 Loggly
錯誤參考資料
This section describes the fault codes and error messages that are returned and fault variables that are set by Edge when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.
Runtime errors
These errors can occur when the policy executes.
| Fault code | HTTP status | Cause |
|---|---|---|
steps.messagelogging.StepDefinitionExecutionFailed |
500 | See fault string. |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
| Error name | Cause | Fix |
|---|---|---|
InvalidProtocol |
The deployment of the MessageLogging policy can fail with this error if the protocol
specified within the <Protocol> element is not valid. The valid protocols are TCP and UDP.
For sending syslog messages over TLS/SSL, only TCP is supported. |
build |
InvalidPort |
The deployment of the MessageLogging policy can fail with this error if the port number
is not specified within the <Port> element or if it is not valid. The port number must be
an integer greater than zero. |
build |
Fault variables
These variables are set when a runtime error occurs. For more information, see What you need to know about policy errors.
| Variables | Where | Example |
|---|---|---|
fault.name="fault_name" |
fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. | fault.name Matches "StepDefinitionExecutionFailed" |
messagelogging.policy_name.failed |
policy_name is the user-specified name of the policy that threw the fault. | messagelogging.ML-LogMessages.failed = true |
Example error response
{
"fault":{
"detail":{
"errorcode":"steps.messagelogging.StepDefinitionExecutionFailed"
},
"faultstring":"Execution failed"
}
}Example fault rule
<FaultRule name="MessageLogging">
<Step>
<Name>ML-LogMessages</Name>
<Condition>(fault.name Matches "StepDefinitionExecutionFailed") </Condition>
</Step>
<Condition>(messagelogging.ML-LogMessages.failed = true) </Condition>
</FaultRule>流程變數
政策失敗時,系統會填入下列變數。
messagelogging.failedmessagelogging.{stepdefinition-name}.failed
相關主題
- Edge 公開的變數:變數參考資料