Skip to content

LogQueryService ​

面向编辑器日志的查询服务,把磁盘日志文件与内存缓冲统一成可分页、可检索的读取入口。它支持按模式匹配定位日志行、按起止标记截取区间、提取异常上下文与最近一次试玩的日志全文,还能枚举试玩会话并读取日志文件信息。排查运行期问题时,...

概览 ​

面向编辑器日志的查询服务,把磁盘日志文件与内存缓冲统一成可分页、可检索的读取入口。它支持按模式匹配定位日志行、按起止标记截取区间、提取异常上下文与最近一次试玩的日志全文,还能枚举试玩会话并读取日志文件信息。排查运行期问题时,可先定位会话再按需取回内容。

Get by:

lua
-- @runtime editor
local service = editor:GetService("LogQueryService")

函数 ​

GetHistory ​

从日志文件中读取一段增量日志,返回 LogQueryPage 分页结果,其中包含本次读取的日志文本、下一个字节偏移、是否已读完,以及该内容是否属于当前试玩会话。

参数

参数类型说明
offsetInt起始字节偏移,默认 0。
maxBytesInt单次最多读取字节数,默认 32768。
fromRunBeginBool从试玩起点开始读,默认 false。
playSessionString限定试玩会话,用于 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("日志增量分页或日志文件信息不可用")
end

GetRecentLines ​

读取日志末尾的若干行,返回 LogQueryTail,其中包含末尾日志行内容以及是否属于当前试玩会话。

参数

参数类型说明
lineCountInt读取行数,默认 100。
levelLogQueryLevel级别过滤;取值见 LogQueryLevel,All 表示不过滤。
platformLogQueryPlatform平台过滤;取值见 LogQueryPlatform,All 表示不过滤。
playSessionString限定试玩会话。
maxBytesInt向前读取的最大字节数,默认 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("末尾日志行不可用")
end

按正则模式检索日志,返回命中日志行的数组,每条命中记录带有命中位置与前后文。

参数

参数类型说明
patternString必填;Python re 正则。
contextInt匹配前后保留行数,默认 2。
caseSensitiveBool默认 false。
maxMatchesInt最多返回条数,默认 50。
levelLogQueryLevel级别过滤;取值见 LogQueryLevel,All 表示不过滤。
platformLogQueryPlatform平台过滤;取值见 LogQueryPlatform,All 表示不过滤。
startPatternString起止边界起点;必须与 endPattern 成对。
endPatternString起止边界终点;必须与 startPattern 成对。
playSessionString限定试玩会话。
maxBytesInt向前读取的最大字节数,默认 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("检索结果或日志区间不可用")
end

GetExceptionContext ​

按内置规则从日志中聚合异常与警告,返回 LogQueryException,集中呈现匹配到的异常上下文信息。

参数

参数类型说明
playSessionString试玩会话;省略时自动解析当前/最近会话。
maxBytesInt向前读取的最大字节数,默认 1048576。
maxMatchesInt每个列表最多保留 50 条根因。
multilineBool捕获多行堆栈,默认 true。
includeStaleMatchesBool允许分析过期会话,默认 false。
caseSensitiveBool默认 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("异常上下文或日志分页不可用")
end

GetLatestContext ​

读取最近一次试玩会话的日志文本,返回字符串;没有内容时返回空串。

参数

参数类型说明
playSessionString试玩会话;省略时自动解析当前/最近会话。
levelLogQueryLevel级别过滤;取值见 LogQueryLevel,All 表示不过滤。
platformLogQueryPlatform平台过滤;取值见 LogQueryPlatform,All 表示不过滤。
maxBytesInt向前读取的最大字节数,默认 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("末尾日志行不可用")
end

GetSlice ​

使用起始与结束两个正则模式截取日志区间,返回 LogQuerySlice,其中包含两个边界之间的日志行以及该区间是否完整。

参数

参数类型说明
startPatternString必填;起点正则,取最后一个匹配行。
endPatternString必填;终点正则,取起点之后第一个匹配行。
caseSensitiveBool默认 false。
levelLogQueryLevel级别过滤;取值见 LogQueryLevel,All 表示不过滤。
platformLogQueryPlatform平台过滤;取值见 LogQueryPlatform,All 表示不过滤。
playSessionString限定试玩会话。
maxBytesInt向前读取的最大字节数,默认 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("日志区间或检索结果不可用")
end

GetBuffered ​

查询内存缓冲中的在线日志,返回 LogQueryBuffer,其中包含缓冲命中的日志内容以及下次增量读取所需使用的游标。

参数

参数类型说明
limitInt返回最后 N 条,默认 100,必须 >0。
levelLogQueryBufferedLevel级别过滤;取值见 LogQueryBufferedLevel,All 表示不过滤。
sinceFloatUnix 秒;只返回严格大于该时间的记录。
grepString不区分大小写的文本过滤。
playSessionString精确匹配试玩会话。

返回值

类型说明
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("在线缓冲日志或试玩会话列表不可用")
end

ListPlaySessions ​

列出缓冲内的试玩会话,返回 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("试玩会话列表或日志文件信息不可用")
end

GetInfo ​

获取当前日志文件的信息,返回 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