A simple logging libary with a logbook for Mark-Marks/sapphire
- Install it with wally
[dependencies]
sapphire_logging = "mark-marks/sapphire-logging@LATEST"
wally install
- Extend sapphire with it
local sapphire_logging = require("@pkg/sapphire_logging")
sapphire
:use(sapphire_logging)
type signal<T...> = {
Root: signal_node<T...>?,
Connect: (self: signal<T...>, Callback: (T...) -> ()) -> () -> (),
Wait: (self: signal<T...>) -> T...,
Once: (self: signal<T...>, Callback: (T...) -> ()) -> () -> (),
Fire: (self: signal<T...>, T...) -> (),
DisconnectAll: (self: signal<T...>) -> (),
}
type log_type = "info" | "debug" | "warn" | "error" | "fatal"
type log = {
--- Timestamps representing when the log took place
timestamp: {
--- CPU time at the time of log
clock: number,
--- Seconds passed since the start of the UNIX epoch at the time of log
unix: number,
},
--- Type of the log
type: log_type,
--- Message passed to the log
msg: string,
--- Full traceback of all function calls leading to this log
trace: string,
}
type sink = (log) -> ()
type logger = {
_debug: boolean,
msg_out: signal<log>,
logbook: { log },
new: (debug: boolean?) -> logger,
write: (self: logger, log_type: log_type, msg: string, trace: string) -> log,
debug: (self: logger, msg: string) -> log?,
warn: (self: logger, msg: string) -> log,
error: (self: logger, msg: string) -> log,
fatal: (self: logger, msg: string) -> (),
connect_sink: (self: logger, sink: sink) -> () -> (),
}
Readonly, extension identifier required by sapphire.
type identifier = string
Readonly
type methods = {}
Readonly, cache for sapphire_logging.get()
type cache = { [string]: logger }
A default, uncached logger
type default = logger
Default sinks, connectable with logger:connect_sink()
type sinks = {
--- Roblox logging sink.
roblox: sink,
}
N/A
Readonly, required by sapphire for extension startup.
() -> ()
Gets an existing cached logger or creates a new one.
(
identifier: string,
debug: boolean?, -- Only applied if the logger doesn't exist
) -> logger
Readonly - log debug messages?
type _debug = boolean
Readonly - on log signal, connect with :connect_sink()
type msg_out = signal<log>
Readonly logbook of all logs that happened
type logbook = { log }
Creates a new logger.
(
debug: boolean? -- Should debug messages be logged? Defaults to false
) -> logger
Writes a message to the logger. Prefer to use the logging methods instead of this.
(
self: logger,
log_type: log_type,
msg: string,
trace: string -- Full traceback of all function calls leading to this
) -> log
Writes an info
to the logger.
(
self: logger,
msg: string
) -> log
Writes a debug
to the logger, IF logger is in debug mode.
(
self: logger,
msg: string
) -> log?
Writes a warn
to the logger.
(
self: logger,
msg: string
) -> log
Writes an error
to the logger, continues execution.
(
self: logger,
msg: string
) -> log
Kills the current thread after writing a fatal
to the logger.
(
self: logger,
msg: string
)
Connects the given sink to the loggers msg_out
signal.
Returns a disconnect function.
(
self: logger,
sink: sink
) -> () -> ()