Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
02fed6e55f | |||
1712042f64 | |||
f8a72d3579 | |||
65cbfe78c3 |
12
go.mod
12
go.mod
@@ -1,3 +1,13 @@
|
||||
module git.site.quack-lab.dev/dave/cylogger
|
||||
|
||||
go 1.24.2
|
||||
go 1.23
|
||||
|
||||
require github.com/hexops/valast v1.5.0
|
||||
|
||||
require (
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
golang.org/x/mod v0.7.0 // indirect
|
||||
golang.org/x/sys v0.3.0 // indirect
|
||||
golang.org/x/tools v0.4.0 // indirect
|
||||
mvdan.cc/gofumpt v0.4.0 // indirect
|
||||
)
|
||||
|
26
go.sum
Normal file
26
go.sum
Normal file
@@ -0,0 +1,26 @@
|
||||
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
|
||||
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/hexops/autogold v0.8.1 h1:wvyd/bAJ+Dy+DcE09BoLk6r4Fa5R5W+O+GUzmR985WM=
|
||||
github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/hexops/valast v1.5.0 h1:FBTuvVi0wjTngtXJRZXMbkN/Dn6DgsUsBwch2DUJU8Y=
|
||||
github.com/hexops/valast v1.5.0/go.mod h1:Jcy1pNH7LNraVaAZDLyv21hHg2WBv9Nf9FL6fGxU7o4=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
|
||||
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
|
||||
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
|
||||
mvdan.cc/gofumpt v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
|
||||
mvdan.cc/gofumpt v0.4.0/go.mod h1:PljLOHDeZqgS8opHRKLzp2It2VBuSdteAgqUfzMTxlQ=
|
129
main.go
129
main.go
@@ -1,5 +1,4 @@
|
||||
// package cylogger
|
||||
package main
|
||||
package cylogger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -14,6 +13,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hexops/valast"
|
||||
)
|
||||
|
||||
// TODO: Enable turning colors on and off maybe even per stream?
|
||||
@@ -34,6 +35,8 @@ const (
|
||||
LevelDebug
|
||||
// LevelTrace is for very detailed tracing information
|
||||
LevelTrace
|
||||
// LevelDump is for dumping objects to console for regressive tests
|
||||
LevelDump
|
||||
// LevelLua is specifically for output from Lua scripts
|
||||
LevelLua
|
||||
LevelPrefix
|
||||
@@ -76,6 +79,12 @@ var levelStyles = map[LogLevel]LevelStyle{
|
||||
Tag: "TRACE",
|
||||
TagColor: BPurple, // Bold Purple
|
||||
},
|
||||
LevelDump: {
|
||||
Tag: "DUMP",
|
||||
TagColor: BIMagenta, // Bold Intense Magenta
|
||||
TagBackgroundColor: On_White, // White background
|
||||
MessageColor: IMagenta, // White text
|
||||
},
|
||||
LevelLua: {
|
||||
Tag: "LUA",
|
||||
TagColor: BBlue, // Bold Blue
|
||||
@@ -89,7 +98,7 @@ var levelStyles = map[LogLevel]LevelStyle{
|
||||
// Logger is our custom logger with level support
|
||||
type Logger struct {
|
||||
mu sync.Mutex
|
||||
out io.Writer
|
||||
out []io.Writer
|
||||
currentLevel LogLevel
|
||||
prefix string
|
||||
userPrefix string
|
||||
@@ -122,6 +131,8 @@ func ParseLevel(levelStr string) LogLevel {
|
||||
return LevelDebug
|
||||
case "TRACE":
|
||||
return LevelTrace
|
||||
case "DUMP":
|
||||
return LevelDump
|
||||
case "LUA":
|
||||
return LevelLua
|
||||
default:
|
||||
@@ -140,7 +151,7 @@ func (l LogLevel) String() string {
|
||||
// New creates a new Logger instance
|
||||
func New(out io.Writer, prefix string, flag int) *Logger {
|
||||
return &Logger{
|
||||
out: out,
|
||||
out: []io.Writer{out},
|
||||
currentLevel: defaultLogLevel,
|
||||
prefix: prefix,
|
||||
userPrefix: "",
|
||||
@@ -206,7 +217,7 @@ func (l *Logger) ShowGoroutine() bool {
|
||||
// WithField adds a field to the logger's context
|
||||
func (l *Logger) WithField(key string, value interface{}) *Logger {
|
||||
newLogger := &Logger{
|
||||
out: l.out,
|
||||
out: append([]io.Writer(nil), l.out...),
|
||||
currentLevel: l.currentLevel,
|
||||
prefix: l.prefix,
|
||||
userPrefix: l.userPrefix,
|
||||
@@ -230,7 +241,7 @@ func (l *Logger) WithField(key string, value interface{}) *Logger {
|
||||
// WithFields adds multiple fields to the logger's context
|
||||
func (l *Logger) WithFields(fields map[string]interface{}) *Logger {
|
||||
newLogger := &Logger{
|
||||
out: l.out,
|
||||
out: append([]io.Writer(nil), l.out...),
|
||||
currentLevel: l.currentLevel,
|
||||
prefix: l.prefix,
|
||||
userPrefix: l.userPrefix,
|
||||
@@ -261,7 +272,7 @@ func (l *Logger) WithPrefix(prefix string) *Logger {
|
||||
l = Default
|
||||
}
|
||||
newLogger := &Logger{
|
||||
out: l.out,
|
||||
out: append([]io.Writer(nil), l.out...),
|
||||
currentLevel: l.currentLevel,
|
||||
prefix: l.prefix,
|
||||
userPrefix: strings.TrimSpace(l.userPrefix + " " + prefix),
|
||||
@@ -291,7 +302,7 @@ func (l *Logger) ToFile(filename string) *Logger {
|
||||
l = Default
|
||||
}
|
||||
newLogger := &Logger{
|
||||
out: io.MultiWriter(l.out, file),
|
||||
out: append([]io.Writer(nil), l.out...),
|
||||
currentLevel: l.currentLevel,
|
||||
prefix: l.prefix,
|
||||
userPrefix: l.userPrefix,
|
||||
@@ -301,6 +312,12 @@ func (l *Logger) ToFile(filename string) *Logger {
|
||||
defaultFields: make(map[string]interface{}),
|
||||
showGoroutine: l.showGoroutine,
|
||||
}
|
||||
// Copy existing fields
|
||||
for k, v := range l.defaultFields {
|
||||
newLogger.defaultFields[k] = v
|
||||
}
|
||||
// Append the new file writer
|
||||
newLogger.out = append(newLogger.out, file)
|
||||
return newLogger
|
||||
}
|
||||
|
||||
@@ -478,7 +495,9 @@ func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
|
||||
|
||||
// Get formatted message with potential background color
|
||||
msg := l.formatMessage(level, format, args...)
|
||||
fmt.Fprintln(l.out, msg)
|
||||
for _, w := range l.out {
|
||||
fmt.Fprintln(w, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// Error logs an error message
|
||||
@@ -506,6 +525,27 @@ func (l *Logger) Trace(format string, args ...interface{}) {
|
||||
l.log(LevelTrace, format, args...)
|
||||
}
|
||||
|
||||
// Dump logs objects using valast for regressive tests
|
||||
func (l *Logger) Dump(message string, objects ...interface{}) {
|
||||
if len(objects) == 0 {
|
||||
l.log(LevelDump, message)
|
||||
return
|
||||
}
|
||||
|
||||
var dumpContent strings.Builder
|
||||
dumpContent.WriteString(message)
|
||||
dumpContent.WriteString(":\n")
|
||||
|
||||
for i, obj := range objects {
|
||||
if i > 0 {
|
||||
dumpContent.WriteString("\n")
|
||||
}
|
||||
dumpContent.WriteString(valast.String(obj))
|
||||
}
|
||||
|
||||
l.log(LevelDump, dumpContent.String())
|
||||
}
|
||||
|
||||
// Lua logs a Lua message
|
||||
func (l *Logger) Lua(format string, args ...interface{}) {
|
||||
l.log(LevelLua, format, args...)
|
||||
@@ -553,6 +593,14 @@ func Trace(format string, args ...interface{}) {
|
||||
Default.Trace(format, args...)
|
||||
}
|
||||
|
||||
// Dump logs objects using valast for regressive tests using the default logger
|
||||
func Dump(message string, objects ...interface{}) {
|
||||
if Default == nil {
|
||||
Init(defaultLogLevel)
|
||||
}
|
||||
Default.Dump(message, objects...)
|
||||
}
|
||||
|
||||
// Lua logs a Lua message using the default logger
|
||||
func Lua(format string, args ...interface{}) {
|
||||
if Default == nil {
|
||||
@@ -620,6 +668,38 @@ func ShowGoroutine() bool {
|
||||
return Default.ShowGoroutine()
|
||||
}
|
||||
|
||||
func (l *Logger) NoStdout() *Logger {
|
||||
if Default == nil {
|
||||
Init(defaultLogLevel)
|
||||
}
|
||||
if l == nil {
|
||||
l = Default
|
||||
}
|
||||
newLogger := &Logger{
|
||||
out: nil,
|
||||
currentLevel: l.currentLevel,
|
||||
prefix: l.prefix,
|
||||
userPrefix: l.userPrefix,
|
||||
flag: l.flag,
|
||||
useColors: l.useColors,
|
||||
callerOffset: l.callerOffset,
|
||||
defaultFields: make(map[string]interface{}),
|
||||
showGoroutine: l.showGoroutine,
|
||||
}
|
||||
// Copy existing fields
|
||||
for k, v := range l.defaultFields {
|
||||
newLogger.defaultFields[k] = v
|
||||
}
|
||||
// Keep all writers except stdout
|
||||
for _, w := range l.out {
|
||||
if w == os.Stdout {
|
||||
continue
|
||||
}
|
||||
newLogger.out = append(newLogger.out, w)
|
||||
}
|
||||
return newLogger
|
||||
}
|
||||
|
||||
func main() {
|
||||
Init(LevelDebug)
|
||||
|
||||
@@ -658,4 +738,35 @@ func main() {
|
||||
// Test with custom prefix
|
||||
WithField("prefix", "custom").Info("Message with custom prefix")
|
||||
Lua("This is a Lua message")
|
||||
|
||||
// Test Dump functionality
|
||||
SetLevel(LevelDump) // Set level to show dump messages
|
||||
|
||||
type ProjectData struct {
|
||||
Title string
|
||||
Name string
|
||||
Data string
|
||||
Commits string
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
Id int64
|
||||
Data *ProjectData
|
||||
}
|
||||
|
||||
p := Project{
|
||||
Id: 1,
|
||||
Data: &ProjectData{
|
||||
Title: "Test",
|
||||
Name: "Mihai",
|
||||
Data: "Some data",
|
||||
Commits: "Test Message",
|
||||
},
|
||||
}
|
||||
|
||||
// Test single object dump
|
||||
Dump("FooBar called", p)
|
||||
|
||||
// Test multiple object dump
|
||||
Dump("Multiple objects", p, fields, "string value", 42)
|
||||
}
|
||||
|
Reference in New Issue
Block a user