本文以電商平台的日誌為例,介紹Log4j日誌的分析操作。
前提條件
背景資訊
例如某電商公司,希望通過分析使用者行為習慣資料(例如使用者登入方式、上線的時間點及時間長度、瀏覽頁面、頁面停留時間、平均下單時間、消費水平等)、平台穩定性、系統報錯、資料安全性等資訊擷取平台的最佳營運方案。針對此需求,Log Service提供一站式資料擷取與分析功能,協助客戶儲存並分析日誌。
Log Service採集到的日誌範例如下。
記錄使用者登入行為的日誌
level: INFO location: com.aliyun.log4jappendertest.Log4jAppenderBizDemo.login(Log4jAppenderBizDemo.java:38) message: User login successfully. requestID=id4 userID=user8 thread: main time: 2022-01-26T15:31+0000記錄使用者購買行為的日誌
level: INFO location: com.aliyun.log4jappendertest.Log4jAppenderBizDemo.order(Log4jAppenderBizDemo.java:46) message: Place an order successfully. requestID=id44 userID=user8 itemID=item3 amount=9 thread: main time: 2022-01-26T15:31+0000
操作步驟
在Project列表地區,單擊目標Project。
在頁簽中,單擊目標Logstore。
輸入查詢和分析語句,然後單擊最近15分鐘,設定查詢和分析的時間範圍。
更多資訊,請參見步驟一:配置索引。
統計最近1小時發生錯誤最多的3個位置。
level: ERROR | select location ,count(*) as count GROUP BY location ORDER BY count DESC LIMIT 3統計最近15分鐘各種記錄層級的日誌條數。
* | select level ,count(*) as count GROUP BY level ORDER BY count DESC統計最近1小時登入次數最多的三個使用者。
login | SELECT regexp_extract(message, 'userID=(?<userID>[a-zA-Z\d]+)', 1) AS userID, count(*) as count GROUP BY userID ORDER BY count DESC LIMIT 3統計最近15分鐘每個使用者的付款總額。
order | SELECT regexp_extract(message, 'userID=(?<userID>[a-zA-Z\d]+)', 1) AS userID, sum(cast(regexp_extract(message, 'amount=(?<amount>[a-zA-Z\d]+)', 1) AS double)) AS amount GROUP BY userID