Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.db
*.db-shm
*.db-wal
*.db-journal
*.dev.yaml

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum
.idea/
.vscode/
.tools/

coverage.txt
coverage.out

bin/
vendor/
build/
22 changes: 6 additions & 16 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,18 @@ var levels = map[uint32]string{
LevelDebug: "DBG",
}

type Fields map[string]interface{}

type Sender interface {
PutEntity(v *entity)
SendMessage(level uint32, call func(v *Message))
Close()
}

// Writer interface
type Writer interface {
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Warnf(format string, args ...interface{})
Infof(format string, args ...interface{})
Debugf(format string, args ...interface{})
}

type WriterContext interface {
WithError(key string, err error) Writer
WithField(key string, value interface{}) Writer
WithFields(Fields) Writer
Writer
Fatal(format string, args ...interface{})
Error(format string, args ...interface{})
Warn(format string, args ...interface{})
Info(format string, args ...interface{})
Debug(format string, args ...interface{})
}

// Logger base interface
Expand All @@ -55,5 +45,5 @@ type Logger interface {
GetLevel() uint32
Close()

WriterContext
Writer
}
45 changes: 15 additions & 30 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,27 @@ func Close() {
std.Close()
}

// Infof info message
func Infof(format string, args ...interface{}) {
std.Infof(format, args...)
// Info message
func Info(format string, args ...interface{}) {
std.Info(format, args...)
}

// Warnf warning message
func Warnf(format string, args ...interface{}) {
std.Warnf(format, args...)
// Warn message
func Warn(format string, args ...interface{}) {
std.Warn(format, args...)
}

// Errorf error message
func Errorf(format string, args ...interface{}) {
std.Errorf(format, args...)
// Error message
func Error(format string, args ...interface{}) {
std.Error(format, args...)
}

// Debugf debug message
func Debugf(format string, args ...interface{}) {
std.Debugf(format, args...)
// Debug message
func Debug(format string, args ...interface{}) {
std.Debug(format, args...)
}

// Fatalf fatal message and exit
func Fatalf(format string, args ...interface{}) {
std.Fatalf(format, args...)
}

// WithFields setter context to log message
func WithFields(v Fields) Writer {
return std.WithFields(v)
}

// WithError setter context to log message
func WithError(key string, err error) Writer {
return std.WithError(key, err)
}

// WithField setter context to log message
func WithField(key string, value interface{}) Writer {
return std.WithField(key, value)
// Fatal message and exit
func Fatal(format string, args ...interface{}) {
std.Fatal(format, args...)
}
102 changes: 0 additions & 102 deletions entity.go

This file was deleted.

69 changes: 45 additions & 24 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,40 @@ import (
"bytes"
"encoding/json"
"fmt"
"sync"
"strings"
"time"

"go.osspkg.com/ioutils/pool"
)

type Formatter interface {
Encode(m *Message) ([]byte, error)
}

var newLine = []byte("\n")

// //////////////////////////////////////////////////////////////////////////////

type FormatJSON struct{}

func NewFormatJSON() *FormatJSON {
return &FormatJSON{}
}

func (*FormatJSON) Encode(m *Message) ([]byte, error) {
m.CtxToMap()
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
return append(b, '\n'), nil
}

var poolBuff = sync.Pool{
New: func() interface{} {
return newBuff()
},
}
// //////////////////////////////////////////////////////////////////////////////

func newBuff() *bytes.Buffer {
return bytes.NewBuffer(nil)
}
var poolBuff = pool.New[*bytes.Buffer](func() *bytes.Buffer {
return bytes.NewBuffer(make([]byte, 0, 1024))
})

type FormatString struct {
delim string
Expand All @@ -54,25 +57,43 @@ func (v *FormatString) SetDelimiter(d string) {
}

func (v *FormatString) Encode(m *Message) ([]byte, error) {
b, ok := poolBuff.Get().(*bytes.Buffer)
if !ok {
b = newBuff()
}

buff := poolBuff.Get()
defer func() {
b.Reset()
poolBuff.Put(b)
poolBuff.Put(buff)
}()

fmt.Fprintf(b, "time=%s%slvl=%s%smsg=%#v",
time.Unix(m.UnixTime, 0).Format(time.RFC3339),
v.delim, m.Level, v.delim, m.Message)
if len(m.Ctx) > 0 {
for key, value := range m.Ctx {
fmt.Fprintf(b, "%s%s=%#v", v.delim, key, value)
fmt.Fprintf(buff, "time=%s%slvl=%s%smsg=%#v",
m.Time.Format(time.RFC3339), v.delim, m.Level, v.delim, m.Message)

if count := len(m.Ctx); count > 0 {
if count%2 != 0 {
m.Ctx = append(m.Ctx, nil)
count++
}
for i := 0; i < count; i = i + 2 {
fmt.Fprintf(buff, "%s%s=\"%s\"", v.delim, typing(m.Ctx[i]), typing(m.Ctx[i+1]))
}
}
b.WriteString("\n")
buff.Write(newLine)

return append(make([]byte, 0, buff.Len()), buff.Bytes()...), nil
}

return append(make([]byte, 0, b.Len()), b.Bytes()...), nil
func typing(v interface{}) (s string) {
if v == nil {
s = "null"
return
}
switch vv := v.(type) {
case error:
s = vv.Error()
case fmt.GoStringer:
s = vv.GoString()
case fmt.Stringer:
s = vv.String()
default:
s = fmt.Sprintf("%#v", v)
}
s = strings.Trim(s, "\"")
return
}
11 changes: 6 additions & 5 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package logx
import (
"bytes"
"testing"
"time"
)

func TestUnit_FormatString_Encode(t *testing.T) {
Expand All @@ -20,11 +21,11 @@ func TestUnit_FormatString_Encode(t *testing.T) {
{
name: "Case1",
args: &Message{
UnixTime: 123456789,
Level: "INF",
Message: "Hello",
Ctx: map[string]interface{}{
"err": "err\nmsg",
Time: time.Now(),
Level: "INF",
Message: "Hello",
Ctx: []interface{}{
"err", "err\nmsg",
},
},
want: []byte("lvl=INF\tmsg=\"Hello\"\terr=\"err\\nmsg\"\n"),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/mailru/easyjson v0.7.7
go.osspkg.com/casecheck v0.3.0
go.osspkg.com/ioutils v0.4.4
go.osspkg.com/syncing v0.3.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
go.osspkg.com/ioutils v0.4.4 h1:1DCGtlPn0/OaoRgUxNzRcH1L3K90WyFRY6CPcKbWuMU=
go.osspkg.com/ioutils v0.4.4/go.mod h1:58HhG2NHf9JUtixAH3R2XISlUmJruwVIUZ3039QVjOY=
go.osspkg.com/syncing v0.3.0 h1:yBkCsDPEt12a+qagInFFt7+ZongfT+GjSQl7nBmcybI=
go.osspkg.com/syncing v0.3.0/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
Loading