2.6 KiB
2.6 KiB
consolex
Small standalone console toolkit:
- colored
slogto terminal + file - log rotation/compression
- interactive readline loop with autocomplete
- built-in commands:
help,clear/cls,uptime,pid,echo - fluent chalk-like styling API
- prebuilt themes and palette helpers
- pipeline logging architecture (
parse -> processors -> render)
Minimal usage
package main
import (
"log/slog"
"github.com/VexoraDevelopment/consolex"
)
func main() {
logFile, err := consolex.SetupDefaultSlog(consolex.LoggerConfig{
LogFilePath: "server.log",
ArchiveDir: "logs",
Level: slog.LevelDebug,
Theme: consolex.NordTheme(),
FieldProvider: consolex.StaticFieldProvider{
"proto_id": consolex.New().White().BgBlue().Bold(),
"raddr": consolex.New().Black().BgYellow(),
},
FieldTransform: consolex.FieldTransformFunc(func(key, value string) (string, bool) {
if key == "raddr" {
return "\"***hidden***\"", true
}
return "", false
}),
})
if err != nil {
panic(err)
}
defer logFile.Close()
loop := consolex.NewLoop(consolex.Options{
Resolve: func(name, args string) bool {
// handle your custom commands here
return false
},
})
<-loop.Start()
}
Chalk-like style API
ch := consolex.New()
println(ch.Bold().BrightGreen().Sprint("server online"))
println(ch.Hex("#66ccff").Underline().Sprint("hello"))
println(ch.BgHex("#202020").White().Sprint("with background"))
Preset palettes
p := consolex.DefaultPalette()
println(p.Success("ok"))
println(p.Warn("careful"))
println(p.Error("boom"))
println(p.KV("proto", 924))
Structure
consolex/style: chalk API + themesconsolex/logging: colored slog + rotation + pipelineconsolex/cmdline: interactive command loop/autocompleteconsolex/term: terminal ANSI helpersconsolexroot: facade API for easy importconsolex/examples/basic: minimal integration exampleconsolex/examples/pipeline: pipeline/profile/processors example
Logging Pipeline
consolex/logging now uses LogRecord pipeline:
- Parse raw slog text line to structured
LogRecord. - Run processors (
FieldTransform,FieldProvider, extra custom processors). - Render record with renderer (
defaultRendererby default).
You can inject custom processors and renderer via LoggerConfig:
cfg := consolex.LoggerConfig{
Theme: consolex.NordTheme(),
Profile: consolex.DefaultProfile(),
Processors: []consolex.Processor{
consolex.ProcessorFunc(func(rec *consolex.LogRecord) {
// custom mutation logic
}),
},
Renderer: consolex.RendererFunc(func(rec *consolex.LogRecord) string {
// custom final formatting
return rec.Raw
}),
}