1
0
mirror of https://github.com/taigrr/log-socket synced 2026-03-21 02:32:19 -07:00

feat(slog): add slog.Handler adapter (#20)

* docs: add example programs for common usage patterns

Adds four focused examples in examples/ directory:
- basic: drop-in logger with web UI
- namespaces: namespace-based logging by component
- client: programmatic log client with filtering
- log-levels: all log levels and filtering

Fixes #7

* feat(slog): add slog.Handler adapter for log-socket

Implements log/slog.Handler that routes structured log records into the
log-socket broadcasting system. Supports namespaces, WithAttrs,
WithGroup, and configurable minimum level.

Also adds Broadcast() as a public entry point for adapter packages
that construct log.Entry values directly.

* chore: update to Go 1.26, resolve slog LogValuer values
This commit is contained in:
2026-03-01 22:48:12 -05:00
committed by GitHub
parent e725622696
commit 5cb1329155
4 changed files with 320 additions and 3 deletions

View File

@@ -103,7 +103,7 @@ func createLog(e Entry) {
namespacesMux.Lock()
namespaces[e.Namespace] = true
namespacesMux.Unlock()
sliceTex.Lock()
for _, c := range clients {
func(c *Client, e Entry) {
@@ -133,7 +133,7 @@ func createLog(e Entry) {
func GetNamespaces() []string {
namespacesMux.RLock()
defer namespacesMux.RUnlock()
result := make([]string, 0, len(namespaces))
for ns := range namespaces {
result = append(result, ns)
@@ -556,3 +556,37 @@ func fileInfo(skip int) string {
}
return fmt.Sprintf("%s:%d", file, line)
}
// Broadcast sends an [Entry] to all registered clients. This is the public
// entry point used by adapter packages (such as the slog handler) that
// construct entries themselves. The unexported level field is inferred from
// [Entry.Level] when not already set.
func Broadcast(e Entry) {
if e.level == 0 && e.Level != "" && e.Level != "TRACE" {
e.level = parseLevelString(e.Level)
}
createLog(e)
}
func parseLevelString(s string) Level {
switch s {
case "TRACE":
return LTrace
case "DEBUG":
return LDebug
case "INFO":
return LInfo
case "NOTICE":
return LNotice
case "WARN":
return LWarn
case "ERROR":
return LError
case "PANIC":
return LPanic
case "FATAL":
return LFatal
default:
return LInfo
}
}