mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor(logger/server): cleanup
DRY
This commit is contained in:
@@ -15,13 +15,43 @@ local function split(str,pat)
|
|||||||
return tbl
|
return tbl
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local playerData = {}
|
local playerData = {}
|
||||||
|
|
||||||
AddEventHandler('playerDropped', function()
|
AddEventHandler('playerDropped', function()
|
||||||
playerData[source] = nil
|
playerData[source] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local function formatTags(source, tags)
|
||||||
|
if type(source) == 'number' and source > 0 then
|
||||||
|
local data = playerData[source]
|
||||||
|
|
||||||
|
if not data then
|
||||||
|
local _data = {
|
||||||
|
('username:%s'):format(GetPlayerName(source))
|
||||||
|
}
|
||||||
|
|
||||||
|
local num = 1
|
||||||
|
|
||||||
|
---@cast source string
|
||||||
|
for i = 0, GetNumPlayerIdentifiers(source) - 1 do
|
||||||
|
local identifier = GetPlayerIdentifier(source, i)
|
||||||
|
|
||||||
|
if not identifier:find('ip') then
|
||||||
|
num += 1
|
||||||
|
_data[num] = identifier
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
data = table.concat(_data, ',')
|
||||||
|
playerData[source] = data
|
||||||
|
end
|
||||||
|
|
||||||
|
tags = tags and ('%s,%s'):format(tags, data) or data
|
||||||
|
end
|
||||||
|
|
||||||
|
return tags
|
||||||
|
end
|
||||||
|
|
||||||
if service == 'datadog' then
|
if service == 'datadog' then
|
||||||
local key = GetConvar('datadog:key', ''):gsub("[\'\"]", '')
|
local key = GetConvar('datadog:key', ''):gsub("[\'\"]", '')
|
||||||
|
|
||||||
@@ -33,37 +63,6 @@ if service == 'datadog' then
|
|||||||
['DD-API-KEY'] = key,
|
['DD-API-KEY'] = key,
|
||||||
}
|
}
|
||||||
|
|
||||||
local function formatTags(source, tags)
|
|
||||||
if type(source) == 'number' and source > 0 then
|
|
||||||
local data = playerData[source]
|
|
||||||
|
|
||||||
if not data then
|
|
||||||
local _data = {
|
|
||||||
('username:%s'):format(GetPlayerName(source))
|
|
||||||
}
|
|
||||||
|
|
||||||
local num = 1
|
|
||||||
|
|
||||||
---@cast source string
|
|
||||||
for i = 0, GetNumPlayerIdentifiers(source) - 1 do
|
|
||||||
local identifier = GetPlayerIdentifier(source, i)
|
|
||||||
|
|
||||||
if not identifier:find('ip') then
|
|
||||||
num += 1
|
|
||||||
_data[num] = identifier
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
data = table.concat(_data, ',')
|
|
||||||
playerData[source] = data
|
|
||||||
end
|
|
||||||
|
|
||||||
tags = tags and ('%s,%s'):format(tags, data) or data
|
|
||||||
end
|
|
||||||
|
|
||||||
return tags
|
|
||||||
end
|
|
||||||
|
|
||||||
function lib.logger(source, event, message, ...)
|
function lib.logger(source, event, message, ...)
|
||||||
if not buffer then
|
if not buffer then
|
||||||
buffer = {}
|
buffer = {}
|
||||||
@@ -96,9 +95,22 @@ end
|
|||||||
if service == 'loki' then
|
if service == 'loki' then
|
||||||
local lokiUser = GetConvar('loki:user', '')
|
local lokiUser = GetConvar('loki:user', '')
|
||||||
local lokiKey = GetConvar('loki:key', '')
|
local lokiKey = GetConvar('loki:key', '')
|
||||||
local lokiEndpoint = GetConvar('loki:endpoint', '')
|
local endpoint = ('https://%s:%s@%s/loki/api/v1/push'):format(lokiUser, lokiKey, GetConvar('loki:endpoint', ''))
|
||||||
local endpoint = ('https://%s:%s@%s/loki/api/v1/push'):format(lokiUser, lokiKey, lokiEndpoint)
|
|
||||||
local resourceName = GetCurrentResourceName()
|
-- Converts a string of comma seperated kvp string to a table of kvps
|
||||||
|
-- example `discord:blahblah,fivem:blahblah,license:blahblah` -> `{discord="blahblah",fivem="blahblah",license="blahblah"}`
|
||||||
|
local function convertDDTagsToKVP(tags)
|
||||||
|
local tempTable = { string.strsplit(',', tags) } -- outputs a number index table wth k:v strings as values
|
||||||
|
local bTable = table.create(0, #tempTable) -- buffer table
|
||||||
|
|
||||||
|
-- Loop through table and grab only values
|
||||||
|
for _, v in pairs(tempTable) do
|
||||||
|
local key, value = string.strsplit(':', v) -- splits string on ':' character
|
||||||
|
bTable[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
return bTable -- Return the new table of kvps
|
||||||
|
end
|
||||||
|
|
||||||
function lib.logger(source, event, message, ...)
|
function lib.logger(source, event, message, ...)
|
||||||
if not buffer then
|
if not buffer then
|
||||||
@@ -130,54 +142,6 @@ if service == 'loki' then
|
|||||||
-- Initializes values table with the message
|
-- Initializes values table with the message
|
||||||
local values = {message = message}
|
local values = {message = message}
|
||||||
|
|
||||||
-- Formats arguments into kvp comma seperated string and adds user data
|
|
||||||
local function formatTags(source, tags)
|
|
||||||
if type(source) == 'number' and source > 0 then
|
|
||||||
local data = playerData[source]
|
|
||||||
|
|
||||||
if not data then
|
|
||||||
local _data = {
|
|
||||||
('username:%s'):format(GetPlayerName(source))
|
|
||||||
}
|
|
||||||
|
|
||||||
local num = 1
|
|
||||||
|
|
||||||
---@cast source string
|
|
||||||
for i = 0, GetNumPlayerIdentifiers(source) - 1 do
|
|
||||||
local identifier = GetPlayerIdentifier(source, i)
|
|
||||||
|
|
||||||
if not identifier:find('ip') then
|
|
||||||
num += 1
|
|
||||||
_data[num] = identifier
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
data = table.concat(_data, ',')
|
|
||||||
playerData[source] = data
|
|
||||||
end
|
|
||||||
|
|
||||||
tags = tags and ('%s,%s'):format(tags, data) or data
|
|
||||||
end
|
|
||||||
|
|
||||||
return tags
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Converts a string of comma seperated kvp string to a table of kvps
|
|
||||||
-- example `discord:blahblah,fivem:blahblah,license:blahblah` -> `{discord="blahblah",fivem="blahblah",license="blahblah"}`
|
|
||||||
local function convertDDTagsToKVP(tags)
|
|
||||||
local tempTable = split(tags, "[^,]*") -- outputs a number index table wth k:v strings as values
|
|
||||||
local bTable = {} -- buffer table
|
|
||||||
|
|
||||||
-- Loop through table and grab only values
|
|
||||||
for _,v in pairs(tempTable) do
|
|
||||||
local tempTable2 = split(v, "[^:]*") -- splits string on ':' character
|
|
||||||
local key = tempTable2[1] -- generate key from first half of string
|
|
||||||
local value = tempTable2[2] -- store the value from the second half of the string
|
|
||||||
bTable[key] = value
|
|
||||||
end
|
|
||||||
return bTable -- Return the new table of kvps
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Format the args into strings
|
-- Format the args into strings
|
||||||
local tags = formatTags(source, ... and string.strjoin(',', string.tostringall(...)) or nil)
|
local tags = formatTags(source, ... and string.strjoin(',', string.tostringall(...)) or nil)
|
||||||
local tagsTable = convertDDTagsToKVP(tags)
|
local tagsTable = convertDDTagsToKVP(tags)
|
||||||
@@ -190,7 +154,7 @@ if service == 'loki' then
|
|||||||
-- initialise stream payload
|
-- initialise stream payload
|
||||||
local payload = {
|
local payload = {
|
||||||
stream = {
|
stream = {
|
||||||
hostname = resourceName,
|
hostname = cache.resource,
|
||||||
service = event
|
service = event
|
||||||
},
|
},
|
||||||
values = {
|
values = {
|
||||||
|
|||||||
Reference in New Issue
Block a user