主题
LogQueryService
面向编辑器日志的查询服务,把磁盘日志文件与内存缓冲统一成可分页、可检索的读取入口。它支持按模式匹配定位日志行、按起止标记截取区间、提取异常上下文与最近一次试玩的日志全文,还能枚举试玩会话并读取日志文件信息。排查运行期问题时,...
概览
面向编辑器日志的查询服务,把磁盘日志文件与内存缓冲统一成可分页、可检索的读取入口。它支持按模式匹配定位日志行、按起止标记截取区间、提取异常上下文与最近一次试玩的日志全文,还能枚举试玩会话并读取日志文件信息。排查运行期问题时,可先定位会话再按需取回内容。
Get by:
lua
-- @runtime editor
local service = editor:GetService("LogQueryService")函数
GetHistory
从日志文件中读取一段增量日志,返回 LogQueryPage 分页结果,其中包含本次读取的日志文本、下一个字节偏移、是否已读完,以及该内容是否属于当前试玩会话。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
offset | Int | 起始字节偏移,默认 0。 |
maxBytes | Int | 单次最多读取字节数,默认 32768。 |
fromRunBegin | Bool | 从试玩起点开始读,默认 false。 |
playSession | String | 限定试玩会话,用于 freshness。 |
返回值
| 类型 | 说明 |
|---|---|
LogQueryPage | 本次读取的日志分页:文本、下一个字节偏移、是否读完,以及是否属于当前试玩会话。 |
读取日志增量分页并校验日志文件信息
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local offsetForGetHistory = 0
local maxBytesForGetHistory = 65536
local fromRunBeginForGetHistory = true
local playSessionForGetHistory = ""
local history = logQueryService:GetHistory(offsetForGetHistory, maxBytesForGetHistory, fromRunBeginForGetHistory, playSessionForGetHistory)
local info = logQueryService:GetInfo()
if history ~= nil and info ~= nil then
print("日志增量分页与日志文件信息均已获取")
print(tostring(history))
print(tostring(info))
else
print("日志增量分页或日志文件信息不可用")
endGetRecentLines
读取日志末尾的若干行,返回 LogQueryTail,其中包含末尾日志行内容以及是否属于当前试玩会话。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
lineCount | Int | 读取行数,默认 100。 |
level | LogQueryLevel | 级别过滤;取值见 LogQueryLevel,All 表示不过滤。 |
platform | LogQueryPlatform | 平台过滤;取值见 LogQueryPlatform,All 表示不过滤。 |
playSession | String | 限定试玩会话。 |
maxBytes | Int | 向前读取的最大字节数,默认 1048576。 |
返回值
| 类型 | 说明 |
|---|---|
LogQueryTail | 末尾若干行日志,以及是否属于当前试玩会话。 |
读取末尾日志行并结合最近会话日志确认
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local lineCountForGetRecentLines = 20
local tail = logQueryService:GetRecentLines(lineCountForGetRecentLines)
local latestContext = logQueryService:GetLatestContext("")
if tail ~= nil then
print("末尾日志行已读取,最近会话日志长度:" .. #latestContext)
print(tostring(tail))
print(tostring(latestContext))
else
print("末尾日志行不可用")
endSearch
按正则模式检索日志,返回命中日志行的数组,每条命中记录带有命中位置与前后文。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
pattern | String | 必填;Python re 正则。 |
context | Int | 匹配前后保留行数,默认 2。 |
caseSensitive | Bool | 默认 false。 |
maxMatches | Int | 最多返回条数,默认 50。 |
level | LogQueryLevel | 级别过滤;取值见 LogQueryLevel,All 表示不过滤。 |
platform | LogQueryPlatform | 平台过滤;取值见 LogQueryPlatform,All 表示不过滤。 |
startPattern | String | 起止边界起点;必须与 endPattern 成对。 |
endPattern | String | 起止边界终点;必须与 startPattern 成对。 |
playSession | String | 限定试玩会话。 |
maxBytes | Int | 向前读取的最大字节数,默认 1048576。 |
返回值
| 类型 | 说明 |
|---|---|
Array | 命中的日志行,每条带命中位置与前后文。 |
检索日志并截取指定区间
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local pattern = "error"
local contextLineCount = 2
local caseSensitive = false
local maxMatches = 10
local matches = logQueryService:Search(pattern, contextLineCount, caseSensitive, maxMatches)
local startPattern = "START"
local endPattern = "END"
local slice = logQueryService:GetSlice(startPattern, endPattern, caseSensitive)
if matches ~= nil and slice ~= nil then
print("检索命中日志行数:" .. #matches)
for index = 1, #matches do
print(tostring(matches[index]))
end
print(tostring(slice))
else
print("检索结果或日志区间不可用")
endGetExceptionContext
按内置规则从日志中聚合异常与警告,返回 LogQueryException,集中呈现匹配到的异常上下文信息。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
playSession | String | 试玩会话;省略时自动解析当前/最近会话。 |
maxBytes | Int | 向前读取的最大字节数,默认 1048576。 |
maxMatches | Int | 每个列表最多保留 50 条根因。 |
multiline | Bool | 捕获多行堆栈,默认 true。 |
includeStaleMatches | Bool | 允许分析过期会话,默认 false。 |
caseSensitive | Bool | 默认 false。 |
返回值
| 类型 | 说明 |
|---|---|
LogQueryException | 按内置规则聚合后的异常与警告。 |
获取异常上下文并核对日志分页
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local playSessionForExceptionContext = ""
local maxBytesForExceptionContext = 65536
local maxMatchesForExceptionContext = 5
local multilineForExceptionContext = true
local includeStaleMatchesForExceptionContext = false
local caseSensitiveForExceptionContext = false
local exceptionContext = logQueryService:GetExceptionContext(playSessionForExceptionContext, maxBytesForExceptionContext, maxMatchesForExceptionContext, multilineForExceptionContext, includeStaleMatchesForExceptionContext, caseSensitiveForExceptionContext)
local offsetForHistory = 0
local maxBytesForHistory = 65536
local fromRunBeginForHistory = false
local history = logQueryService:GetHistory(offsetForHistory, maxBytesForHistory, fromRunBeginForHistory, playSessionForExceptionContext)
if exceptionContext ~= nil and history ~= nil then
print("异常上下文与日志分页均已获取")
print(tostring(exceptionContext))
print(tostring(history))
else
print("异常上下文或日志分页不可用")
endGetLatestContext
读取最近一次试玩会话的日志文本,返回字符串;没有内容时返回空串。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
playSession | String | 试玩会话;省略时自动解析当前/最近会话。 |
level | LogQueryLevel | 级别过滤;取值见 LogQueryLevel,All 表示不过滤。 |
platform | LogQueryPlatform | 平台过滤;取值见 LogQueryPlatform,All 表示不过滤。 |
maxBytes | Int | 向前读取的最大字节数,默认 1048576。 |
返回值
| 类型 | 说明 |
|---|---|
String | 最近一次试玩会话的日志文本;没有内容时返回空串。 |
获取最近会话日志并读取末尾日志行
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local playSessionForGetLatestContext = ""
local latestContext = logQueryService:GetLatestContext(playSessionForGetLatestContext)
local lineCountForGetRecentLines = 20
local tail = logQueryService:GetRecentLines(lineCountForGetRecentLines)
if tail ~= nil then
print("最近会话日志长度:" .. #latestContext)
print(tostring(latestContext))
print(tostring(tail))
else
print("末尾日志行不可用")
endGetSlice
使用起始与结束两个正则模式截取日志区间,返回 LogQuerySlice,其中包含两个边界之间的日志行以及该区间是否完整。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
startPattern | String | 必填;起点正则,取最后一个匹配行。 |
endPattern | String | 必填;终点正则,取起点之后第一个匹配行。 |
caseSensitive | Bool | 默认 false。 |
level | LogQueryLevel | 级别过滤;取值见 LogQueryLevel,All 表示不过滤。 |
platform | LogQueryPlatform | 平台过滤;取值见 LogQueryPlatform,All 表示不过滤。 |
playSession | String | 限定试玩会话。 |
maxBytes | Int | 向前读取的最大字节数,默认 1048576。 |
返回值
| 类型 | 说明 |
|---|---|
LogQuerySlice | 两个正则边界之间的日志行,以及区间是否完整。 |
截取日志区间并检索命中行
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local startPattern = "START"
local endPattern = "END"
local caseSensitiveForGetSlice = false
local slice = logQueryService:GetSlice(startPattern, endPattern, caseSensitiveForGetSlice)
local pattern = "error"
local contextLineCount = 2
local maxMatches = 10
local matches = logQueryService:Search(pattern, contextLineCount, caseSensitiveForGetSlice, maxMatches)
if slice ~= nil and matches ~= nil then
print("日志区间已截取,命中日志行数:" .. #matches)
print(tostring(slice))
for index = 1, #matches do
print(tostring(matches[index]))
end
else
print("日志区间或检索结果不可用")
endGetBuffered
查询内存缓冲中的在线日志,返回 LogQueryBuffer,其中包含缓冲命中的日志内容以及下次增量读取所需使用的游标。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
limit | Int | 返回最后 N 条,默认 100,必须 >0。 |
level | LogQueryBufferedLevel | 级别过滤;取值见 LogQueryBufferedLevel,All 表示不过滤。 |
since | Float | Unix 秒;只返回严格大于该时间的记录。 |
grep | String | 不区分大小写的文本过滤。 |
playSession | String | 精确匹配试玩会话。 |
返回值
| 类型 | 说明 |
|---|---|
LogQueryBuffer | 内存缓冲中的日志,以及下次增量读取的游标。 |
查询在线缓冲日志并列出试玩会话
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local limitForGetBuffered = 100
local buffer = logQueryService:GetBuffered(limitForGetBuffered)
local sessions = logQueryService:ListPlaySessions()
if buffer ~= nil and sessions ~= nil then
print("在线缓冲日志已获取,试玩会话数量:" .. #sessions)
print(tostring(buffer))
for index = 1, #sessions do
print(tostring(sessions[index]))
end
else
print("在线缓冲日志或试玩会话列表不可用")
endListPlaySessions
列出缓冲内的试玩会话,返回 LogQuerySession 数组。
返回值
| 类型 | 说明 |
|---|---|
Array | 缓冲内的试玩会话列表。 |
列出试玩会话并读取日志文件信息
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local sessions = logQueryService:ListPlaySessions()
local info = logQueryService:GetInfo()
if sessions ~= nil and info ~= nil then
print("试玩会话数量:" .. #sessions)
for index = 1, #sessions do
print("会话序号:" .. index .. ",会话信息:" .. tostring(sessions[index]))
end
print(tostring(info))
else
print("试玩会话列表或日志文件信息不可用")
endGetInfo
获取当前日志文件的信息,返回 LogQueryInfo,其中包含日志文件路径以及该路径的来源。
返回值
| 类型 | 说明 |
|---|---|
LogQueryInfo | 当前日志文件的路径与路径来源。 |
获取日志文件信息并确认试玩会话列表
lua
-- @runtime editor
local logQueryService = editor:GetService("LogQueryService")
local info = logQueryService:GetInfo()
local sessions = logQueryService:ListPlaySessions()
if info ~= nil and sessions ~= nil then
print("日志文件信息已获取,试玩会话数量:" .. #sessions)
print(tostring(info))
for index = 1, #sessions do
print("会话序号:" .. index .. ",会话信息:" .. tostring(sessions[index]))
end
else
print("日志文件信息或试玩会话列表不可用")
end