mirror of
https://github.com/taigrr/log-socket
synced 2026-03-20 14:52:27 -07:00
Compare commits
1 Commits
cd/9-color
...
burrow/6-a
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d74b0a950 |
35
README.md
35
README.md
@@ -15,7 +15,6 @@ A real-time log viewer with WebSocket support and namespace filtering, written i
|
||||
- **Namespace filtering**: Subscribe to specific namespaces via WebSocket
|
||||
- **Frontend namespace selector**: Filter logs by namespace in the web UI
|
||||
- **Namespace API**: GET `/api/namespaces` to list all active namespaces
|
||||
- **Colored terminal output**: Log levels are color-coded in terminal (no external packages)
|
||||
|
||||
## Features
|
||||
|
||||
@@ -189,40 +188,6 @@ ws://localhost:8080/ws?namespaces=api,database # Multiple namespaces
|
||||
- **Color Coding**: Different log levels are color-coded
|
||||
- **Reconnect**: Reconnect WebSocket with new namespace filter
|
||||
|
||||
## Terminal Colors
|
||||
|
||||
Log output to stderr is automatically colorized when writing to a terminal. Colors are disabled when output is piped or redirected to a file.
|
||||
|
||||
### Color Scheme
|
||||
|
||||
| Level | Color |
|
||||
|--------|--------------|
|
||||
| TRACE | Gray |
|
||||
| DEBUG | Cyan |
|
||||
| INFO | Green |
|
||||
| NOTICE | Blue |
|
||||
| WARN | Yellow |
|
||||
| ERROR | Red |
|
||||
| PANIC | Bold Red |
|
||||
| FATAL | Bold Red |
|
||||
|
||||
### Controlling Colors
|
||||
|
||||
```go
|
||||
// Disable colors (e.g., for CI/CD or file output)
|
||||
logger.SetColorEnabled(false)
|
||||
|
||||
// Enable colors explicitly
|
||||
logger.SetColorEnabled(true)
|
||||
|
||||
// Check current state
|
||||
if logger.ColorEnabled() {
|
||||
// colors are on
|
||||
}
|
||||
```
|
||||
|
||||
Colors are implemented using standard ANSI escape codes with no external dependencies.
|
||||
|
||||
## Migration from v1
|
||||
|
||||
### Import Path
|
||||
|
||||
171
log/bench_test.go
Normal file
171
log/bench_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const benchNS = "bench-isolated-ns"
|
||||
|
||||
// benchClient creates a client with a continuous drain goroutine.
|
||||
// Returns the client and a stop function to call after b.StopTimer().
|
||||
func benchClient(ns string) (*Client, func()) {
|
||||
c := CreateClient(ns)
|
||||
c.SetLogLevel(LTrace)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-c.writer:
|
||||
}
|
||||
}
|
||||
}()
|
||||
return c, func() {
|
||||
close(done)
|
||||
c.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkCreateClient measures client creation overhead.
|
||||
func BenchmarkCreateClient(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := CreateClient("bench")
|
||||
c.Destroy()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTrace benchmarks Logger.Trace.
|
||||
func BenchmarkTrace(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Trace("benchmark trace message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTracef benchmarks formatted Logger.Tracef.
|
||||
func BenchmarkTracef(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Tracef("benchmark trace message %d", i)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkDebug benchmarks Logger.Debug.
|
||||
func BenchmarkDebug(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Debug("benchmark debug message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkInfo benchmarks Logger.Info.
|
||||
func BenchmarkInfo(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Info("benchmark info message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkInfof benchmarks Logger.Infof with formatting.
|
||||
func BenchmarkInfof(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Infof("user %s performed action %d", "testuser", i)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkWarn benchmarks Logger.Warn.
|
||||
func BenchmarkWarn(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Warn("benchmark warn message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkError benchmarks Logger.Error.
|
||||
func BenchmarkError(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Error("benchmark error message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMultipleClients measures fan-out to multiple consumers.
|
||||
func BenchmarkMultipleClients(b *testing.B) {
|
||||
const numClients = 5
|
||||
l := NewLogger(benchNS)
|
||||
stops := make([]func(), numClients)
|
||||
for i := range stops {
|
||||
_, stops[i] = benchClient(benchNS)
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Info("benchmark multi-client")
|
||||
}
|
||||
b.StopTimer()
|
||||
for _, stop := range stops {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkParallelInfo benchmarks concurrent Info calls from multiple goroutines.
|
||||
func BenchmarkParallelInfo(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient(benchNS)
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
l.Info("parallel benchmark info")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// BenchmarkNamespaceFiltering benchmarks logging when the consumer
|
||||
// filters by a different namespace (messages are not delivered).
|
||||
func BenchmarkNamespaceFiltering(b *testing.B) {
|
||||
l := NewLogger(benchNS)
|
||||
_, stop := benchClient("completely-different-ns")
|
||||
defer stop()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
l.Info("filtered out message")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkFileInfo measures the cost of runtime.Caller for file info.
|
||||
func BenchmarkFileInfo(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
fileInfo(1)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkEntryCreation measures raw fmt.Sprint overhead (baseline).
|
||||
func BenchmarkEntryCreation(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = fmt.Sprint("benchmark message ", i)
|
||||
}
|
||||
}
|
||||
99
log/color.go
99
log/color.go
@@ -1,99 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ANSI color codes for terminal output
|
||||
const (
|
||||
colorReset = "\033[0m"
|
||||
colorRed = "\033[31m"
|
||||
colorGreen = "\033[32m"
|
||||
colorYellow = "\033[33m"
|
||||
colorBlue = "\033[34m"
|
||||
colorPurple = "\033[35m"
|
||||
colorCyan = "\033[36m"
|
||||
colorWhite = "\033[37m"
|
||||
colorGray = "\033[90m"
|
||||
|
||||
// Bold variants
|
||||
colorBoldRed = "\033[1;31m"
|
||||
colorBoldYellow = "\033[1;33m"
|
||||
colorBoldWhite = "\033[1;37m"
|
||||
)
|
||||
|
||||
var (
|
||||
colorEnabled = true
|
||||
colorEnabledOnce sync.Once
|
||||
colorMux sync.RWMutex
|
||||
)
|
||||
|
||||
// SetColorEnabled enables or disables colored output for stderr logging.
|
||||
// By default, color is enabled when stderr is a terminal.
|
||||
func SetColorEnabled(enabled bool) {
|
||||
colorMux.Lock()
|
||||
colorEnabled = enabled
|
||||
colorMux.Unlock()
|
||||
}
|
||||
|
||||
// ColorEnabled returns whether colored output is currently enabled.
|
||||
func ColorEnabled() bool {
|
||||
colorMux.RLock()
|
||||
defer colorMux.RUnlock()
|
||||
return colorEnabled
|
||||
}
|
||||
|
||||
// isTerminal checks if the given file descriptor is a terminal.
|
||||
// This is a simple heuristic that works on Unix-like systems.
|
||||
func isTerminal(f *os.File) bool {
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return (stat.Mode() & os.ModeCharDevice) != 0
|
||||
}
|
||||
|
||||
// initColorEnabled sets the default color state based on whether stderr is a terminal.
|
||||
func initColorEnabled() {
|
||||
colorEnabledOnce.Do(func() {
|
||||
colorEnabled = isTerminal(os.Stderr)
|
||||
})
|
||||
}
|
||||
|
||||
// levelColor returns the ANSI color code for a given log level.
|
||||
func levelColor(level Level) string {
|
||||
switch level {
|
||||
case LTrace:
|
||||
return colorGray
|
||||
case LDebug:
|
||||
return colorCyan
|
||||
case LInfo:
|
||||
return colorGreen
|
||||
case LNotice:
|
||||
return colorBlue
|
||||
case LWarn:
|
||||
return colorYellow
|
||||
case LError:
|
||||
return colorRed
|
||||
case LPanic:
|
||||
return colorBoldRed
|
||||
case LFatal:
|
||||
return colorBoldRed
|
||||
default:
|
||||
return colorReset
|
||||
}
|
||||
}
|
||||
|
||||
// colorize wraps text with ANSI color codes if color is enabled.
|
||||
func colorize(text string, color string) string {
|
||||
if !ColorEnabled() {
|
||||
return text
|
||||
}
|
||||
return color + text + colorReset
|
||||
}
|
||||
|
||||
// colorizeLevelText returns the level string with appropriate color.
|
||||
func colorizeLevelText(level string, lvl Level) string {
|
||||
return colorize(level, levelColor(lvl))
|
||||
}
|
||||
@@ -22,7 +22,6 @@ var (
|
||||
|
||||
func init() {
|
||||
namespaces = make(map[string]bool)
|
||||
initColorEnabled()
|
||||
stderrClient = CreateClient(DefaultNamespace)
|
||||
stderrClient.SetLogLevel(LTrace)
|
||||
stderrFinished = make(chan bool, 1)
|
||||
@@ -32,10 +31,7 @@ func init() {
|
||||
func (c *Client) logStdErr() {
|
||||
for e := range c.writer {
|
||||
if e.level >= c.LogLevel && c.matchesNamespace(e.Namespace) {
|
||||
levelStr := colorizeLevelText(e.Level, e.level)
|
||||
nsStr := colorize("["+e.Namespace+"]", colorPurple)
|
||||
fileStr := colorize(e.File, colorGray)
|
||||
fmt.Fprintf(os.Stderr, "%s\t%s\t%s\t%s\t%s\n", e.Timestamp.String(), levelStr, nsStr, e.Output, fileStr)
|
||||
fmt.Fprintf(os.Stderr, "%s\t%s\t[%s]\t%s\t%s\n", e.Timestamp.String(), e.Level, e.Namespace, e.Output, e.File)
|
||||
}
|
||||
}
|
||||
stderrFinished <- true
|
||||
|
||||
Reference in New Issue
Block a user