--[[ A Loki provider for ox_lib's Ox.Logger. ox_lib's older lib.logger can send to Loki; Ox.Logger has no Loki provider, so `set ox:logger "loki"` makes it fail to load. This file is that provider. Put it in ox_lib's resource/logger/ folder (next to fivemanage.lua), keep `set ox:logger "loki"`, and both loggers send to the same `loki:endpoint` — for FiveStaff, a log webhook's address. It reads the same settings as lib.logger's Loki provider: set loki:endpoint "https://" (required; /loki/api/v1/push is added) set loki:user "" (optional; Basic auth with loki:password, or loki:key) set loki:tenant "" (optional; sent as X-Scope-OrgID) Updating ox_lib replaces its folder, so copy this file in again after an update until ox_lib ships a Loki provider of its own. Licensed under LGPL-3.0 or higher, as ox_lib is . ]] local alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' local function base64(data) local out = {} for i = 1, #data, 3 do local a, b, c = data:byte(i, i + 2) local n = (a << 16) | ((b or 0) << 8) | (c or 0) out[#out + 1] = alphabet:sub((n >> 18) + 1, (n >> 18) + 1) out[#out + 1] = alphabet:sub(((n >> 12) & 63) + 1, ((n >> 12) & 63) + 1) out[#out + 1] = b and alphabet:sub(((n >> 6) & 63) + 1, ((n >> 6) & 63) + 1) or '=' out[#out + 1] = c and alphabet:sub((n & 63) + 1, (n & 63) + 1) or '=' end return table.concat(out) end ---@param config OxLoggerConfig ---@return OxLoggerProvider return function(config) local endpoint = GetConvar('loki:endpoint', '') if endpoint == '' then error("convar 'loki:endpoint' has not been set") end if not endpoint:find('^https?://') then endpoint = 'https://' .. endpoint end local headers = { ['Content-Type'] = 'application/json', ['User-Agent'] = 'ox_lib', } local user = GetConvar('loki:user', '') if user ~= '' then headers['Authorization'] = 'Basic ' .. base64(user .. ':' .. GetConvar('loki:password', GetConvar('loki:key', ''))) end local tenant = GetConvar('loki:tenant', '') if tenant ~= '' then headers['X-Scope-OrgID'] = tenant end ---@class OxLoggerProvider local provider = { name = 'Loki', endpoint = endpoint:gsub('/+$', '') .. '/loki/api/v1/push', headers = headers, status = { ok = 204, }, } -- One log as Loki takes it: the resource and level as labels, everything -- else (the message, the players, the metadata) in the line. function provider.append(log) local metadata = log.metadata or {} local resource = metadata.resource or 'unknown' local line = { message = log.message } for key, value in pairs(metadata) do if key ~= 'resource' then line[key] = value end end return { resource = resource, level = log.level or 'info', at = os.time(), line = json.encode(line), } end -- One stream per resource and level, as Loki groups them. function provider.encode(logs) local streams, byKey = {}, {} for i = 1, #logs do local log = logs[i] local key = log.resource .. '\0' .. log.level local stream = byKey[key] if not stream then stream = { stream = { server = config.hostname, resource = log.resource, level = log.level }, values = {}, } byKey[key] = stream streams[#streams + 1] = stream end stream.values[#stream.values + 1] = { ('%d000000000'):format(log.at), log.line } end return json.encode({ streams = streams }) end function provider.onResponse(status, response) if status == 204 or status == 200 then return true end return false, ('\t%s %s'):format(status, type(response) == 'string' and response or '') end return provider end