-- Compiled with roblox-ts v2.3.0-dev-30dae68 --[[ * * @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. ]] --[[ * * Enum representing the various log levels, or "importance" of a {@link LogEntry}. * * @public ]] local LogLevel do local _inverse = {} LogLevel = setmetatable({}, { __index = _inverse, }) LogLevel.VERBOSE = 0 _inverse[0] = "VERBOSE" LogLevel.DEBUG = 1 _inverse[1] = "DEBUG" LogLevel.INFO = 2 _inverse[2] = "INFO" LogLevel.WARNING = 3 _inverse[3] = "WARNING" LogLevel.ERROR = 4 _inverse[4] = "ERROR" end --[[ * * Type representing the additional data associated with a log entry. * * @public ]] --[[ * * A single logging event. * * @remarks * * Each message has its own instance of this, with relevant data * attached. * * @public ]] --[[ * * Type representing a callback function for consuming log entries, or a "sink". * * Sinks are generally used to send logs to an external database or service, but they can * also be used to filter logs by "consuming" them. * * If your callback returns `true`, then the log will be stopped, and no further sinks will * be called. The {@link LogEntry} will also not be logged to the console. * * @remarks * * You should not yield in sinks. If you're sending data to an external service, do * so via a queue that gets dispatched in a different thread. * * @param entry - The log entry to handle. * * @returns `true` if the log was consume, `false` or `void` otherwise. * * @public * * @example * ```ts * const logger = new rLog({ * sinks: [ * (entry) => { * someExternalDBFunction(entry); * }, * (entry) => { * return true; * }, * (entry) => { * // never throws because the previous sink returned true * error("Messages should not log to the console"); * }, * ], * }); * * logger.i("Hello world!"); * ``` ]] --[[ * * Runs a log through all the provided sinks, stopping if any return true. * * @param entry - Log to send through the sinks. * @param sinks - Collection of {@link LogSinkCallback} to call with the entry. * * @internal * * @throws If sinks is empty, as there's nowhere to send the log. ]] local function sink(entry, sinks) if #sinks == 0 then warn("rLog entry is missing sinks. I don't have anywhere to send this message.\n", entry.message) return nil end -- ▼ ReadonlyArray.some ▼ local _result = false local _callback = function(sinker) return sinker(entry) == true end for _k, _v in sinks do if _callback(_v, _k - 1, sinks) then _result = true break end end -- ▲ ReadonlyArray.some ▲ end --[[ * * Type representing a callback function for enriching log entries, or an "enricher". * * Enrichers optionally mutate {@link LogEntry}s. You can add data to a {@link LogEntry}, * edit its {@link LogEntry.source_metadata | metadata}, or just return it if you don't need to * do anything. * * @param entry - The log entry to enrich. * * @returns The enriched log entry. * * @public ]] --[[ * * Runs a log through all the provided enrichers, folding the value along the way. * * @param entry - Log to send through the enrichers. * @param enrichers - Collection of {@link LogEnricherCallback} to call with the entry. * * @internal * * @returns The final log entry, after being folded across all the enrichers. ]] local function enrich(entry, enrichers) local _enrichers = enrichers local _entry = entry -- ▼ ReadonlyArray.reduce ▼ local _result = _entry local _callback = function(log, enricher) return enricher(log) end for _i = 1, #_enrichers do _result = _callback(_result, _enrichers[_i], _i - 1, _enrichers) end -- ▲ ReadonlyArray.reduce ▲ return _result end --[[ * * Metadata used in identifying _where_ in the source code a log occurred. * * @public ]] return { sink = sink, enrich = enrich, LogLevel = LogLevel, }