-- Compiled with roblox-ts v2.3.0-dev-30dae68 local TS = _G[script] --[[ * * @license * Copyright 2024 Daymon Littrell-Reyes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ]] local _common = TS.import(script, script.Parent, "common") local enrich = _common.enrich local LogLevel = _common.LogLevel local sink = _common.sink local mergeConfigs = TS.import(script, script.Parent, "configuration").mergeConfigs local LogContextManager = TS.import(script, script.Parent, "context", "log-context-manager").LogContextManager local serialize = TS.import(script, script.Parent, "serialization").serialize local robloxConsoleSink = TS.import(script, script.Parent, "sinks").robloxConsoleSink local FILE_NAME = (debug.info(1, "s")) --[[ * * Table version of the constructor parameters for {@link RLog}. * * @public ]] local function isConstructorParameters(value) return value.config ~= nil or value.context ~= nil or value.inheritDefault ~= nil end local function extractSourceMetadata() local metadata = {} local stack_level = 3 local file repeat do local _original = stack_level stack_level += 1 local file, line, func = debug.info(_original, "sln") if file == FILE_NAME then continue end if metadata.file_path == nil then metadata.file_path = file metadata.function_name = func metadata.line_number = line end if metadata.nearest_function_name == nil then metadata.nearest_function_name = func end end until not (file ~= nil and metadata.nearest_function_name == nil) return metadata end --[[ * * Class for server-side Roblox Logging. * * @remarks * * You can also use `rlog` or `rLog`- for style purposes. * * @public ]] local RLog do RLog = setmetatable({}, { __tostring = function() return "RLog" end, }) RLog.__index = RLog function RLog.new(...) local self = setmetatable({}, RLog) return self:constructor(...) or self end function RLog:constructor(config, context, inheritDefault) if config == nil then config = {} end if inheritDefault == nil then inheritDefault = true end if isConstructorParameters(config) then context = config.context local _condition = config.inheritDefault if _condition == nil then _condition = true end inheritDefault = _condition config = config.config or {} end local _result if inheritDefault then local _result_1 = RLog.default if _result_1 ~= nil then _result_1 = _result_1._config end _result = _result_1 else _result = nil end local _result_1 = context if _result_1 ~= nil then _result_1 = _result_1.config end self._config = mergeConfigs(_result, _result_1, config) if context then self.context = context:withConfig(self._config) end end function RLog:SetDefaultConfig(config) self.default._config = mergeConfigs(config) end function RLog:UpdateDefaultConfig(config) self.default._config = mergeConfigs(self.default._config, config) end function RLog:ResetDefaultConfig() self.default._config = mergeConfigs({ sinks = { robloxConsoleSink() }, }) end function RLog:ForceContextFlush() LogContextManager.forceFlush() end function RLog:clone(params) if params == nil then params = {} end local config = mergeConfigs(self._config, params.config) return RLog.new({ config = config, context = self.context, }) end function RLog:log(level, message, data) if data == nil then data = {} end local isOverride = false if self._config.minLogLevel > level then if not self.context or not self._config.contextBypass then return nil end isOverride = true end local isFlag = self.context and level >= LogLevel.WARNING local baseEntry = { level = level, message = message, data = data, encoded_data = serialize(self._config.serialization, data), config = self._config, context = self.context, timestamp = DateTime.now().UnixTimestampMillis, source_metadata = extractSourceMetadata(), } local enrichedEntry = enrich(baseEntry, self._config.enrichers or {}) if isOverride then -- enrichers could remove the context, which is allowed if baseEntry.context then LogContextManager.save(baseEntry, baseEntry.context) end else if isFlag then LogContextManager.flag(baseEntry) end if baseEntry.config.suspendContext and baseEntry.context then LogContextManager.push(baseEntry, baseEntry.context) else sink(enrichedEntry, baseEntry.config.sinks or {}) end end end function RLog:verbose(message, data) if data == nil then data = {} end self:log(LogLevel.VERBOSE, message, data) end function RLog:v(message, data) if data == nil then data = {} end self:verbose(message, data) end function RLog:debug(message, data) if data == nil then data = {} end self:log(LogLevel.DEBUG, message, data) end function RLog:d(message, data) if data == nil then data = {} end self:debug(message, data) end function RLog:info(message, data) if data == nil then data = {} end self:log(LogLevel.INFO, message, data) end function RLog:i(message, data) if data == nil then data = {} end self:info(message, data) end function RLog:warning(message, data) if data == nil then data = {} end self:log(LogLevel.WARNING, message, data) end function RLog:warn(message, data) if data == nil then data = {} end self:warning(message, data) end function RLog:w(message, data) if data == nil then data = {} end self:warning(message, data) end function RLog:error(message, data) if data == nil then data = {} end self:log(LogLevel.ERROR, message, data) end function RLog:e(message, data) if data == nil then data = {} end self:error(message, data) end function RLog:withConfig(config) return self:clone({ config = config, }) end function RLog:withMinLogLevel(minLevel) return self:clone({ config = { minLogLevel = minLevel, }, }) end function RLog:withTag(tag) return self:clone({ config = { tag = tag, }, }) end function RLog:withLogContext(context) return self:clone({ context = context, }) end RLog.default = RLog.new({ sinks = { robloxConsoleSink() }, }, nil) end --[[ * * Mapping to {@link RLog.default} for easier default usage. * * @public ]] local rLogger = RLog.default return { RLog = RLog, rlog = RLog, rLog = RLog, rLogger = rLogger, }