You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.0 KiB
51 lines
1.0 KiB
package log
|
|
|
|
import (
|
|
"log/slog"
|
|
"sync"
|
|
|
|
"zestack.dev/color"
|
|
)
|
|
|
|
var (
|
|
cError = color.New(color.FgHiRed, color.Bold)
|
|
cInfo = color.New(color.FgHiGreen, color.Bold)
|
|
cWarn = color.New(color.FgHiYellow, color.Bold)
|
|
cDebug = color.New(color.FgHiCyan, color.Bold)
|
|
// cFatal = color.New(color.FgHiBlue, color.Bold)
|
|
// cPanic = color.New(color.FgHiMagenta, color.Bold)
|
|
// cTrace = color.New(color.FgHiCyan, color.Bold)
|
|
)
|
|
|
|
func levelToColor(l slog.Level) (*color.Value, string) {
|
|
switch {
|
|
case l < slog.LevelInfo:
|
|
return cDebug.Wrap("DEBUG"), ""
|
|
case l < slog.LevelWarn:
|
|
return cInfo.Wrap("INFO"), " "
|
|
case l < slog.LevelError:
|
|
return cWarn.Wrap("WARN"), " "
|
|
default:
|
|
return cError.Wrap("ERROR"), ""
|
|
}
|
|
}
|
|
|
|
var bufPool = sync.Pool{
|
|
New: func() any {
|
|
b := make([]byte, 0, 1024)
|
|
return &b
|
|
},
|
|
}
|
|
|
|
func allocBuf() *[]byte {
|
|
return bufPool.Get().(*[]byte)
|
|
}
|
|
|
|
func freeBuf(b *[]byte) {
|
|
// To reduce peak allocation, return only smaller buffers to the pool.
|
|
const maxBufferSize = 16 << 10
|
|
if cap(*b) <= maxBufferSize {
|
|
*b = (*b)[:0]
|
|
bufPool.Put(b)
|
|
}
|
|
}
|
|
|