Skip to content

Commit 76c02ca

Browse files
committed
up: support report caller info
1 parent 43e3cb2 commit 76c02ca

File tree

11 files changed

+70
-9
lines changed

11 files changed

+70
-9
lines changed

_example/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ github.com/gookit/slog v0.0.0-20200718054058-a7053e5b311b h1:VjstQGjCdqf79e2p6Bd
1414
github.com/gookit/slog v0.0.0-20200718054058-a7053e5b311b/go.mod h1:ObcDZB9gjOVltZnvB4frHW2Mf5pFWnoJ9601VIDMEIE=
1515
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
1616
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
17+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
1718
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1819
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1920
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

define.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,20 @@ const flushInterval = 30 * time.Second
5555

5656
const (
5757
FieldKeyTime = "time"
58+
// FieldKeyDate = "date"
5859
FieldKeyData = "data"
5960
FieldKeyFunc = "func"
6061
FieldKeyFile = "file"
61-
// FieldKeyDate = "date"
6262

63-
FieldKeyDatetime = "datetime"
63+
FieldKeyDatetime = "datetime"
64+
FieldKeyPosition = "position"
65+
FieldKeyTimestamp = "timestamp"
6466

6567
FieldKeyLevel = "level"
6668
FieldKeyError = "error"
6769
FieldKeyExtra = "extra"
6870

71+
FieldKeyCaller = "caller"
6972
FieldKeyChannel = "channel"
7073
FieldKeyMessage = "message"
7174
)

formatter_json.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func (f *JSONFormatter) Format(r *Record) ([]byte,error) {
5858
}
5959

6060
logData[outName] = r.Time.Format(f.TimeFormat)
61+
case field == FieldKeyCaller && r.Caller != nil:
62+
logData[outName] = formatCallerToString(r.Caller)
6163
case field == FieldKeyLevel:
6264
logData[outName] = r.LevelName
6365
case field == FieldKeyChannel:

formatter_text.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/gookit/goutil/strutil"
1111
)
1212

13-
const DefaultTemplate = "[{{datetime}}] [{{channel}}] [{{level}}] {{message}} {{data}} {{extra}}\n"
13+
const DefaultTemplate = "[{{datetime}}] [{{channel}}] [{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n"
1414

1515
// ColorTheme for format log to console
1616
var ColorTheme = map[Level]color.Color{
@@ -90,6 +90,8 @@ func (f *TextFormatter) formatNoColor(r *Record) ([]byte, error) {
9090
}
9191

9292
tplData[tplVar] = r.Time.Format(f.TimeFormat)
93+
case field == FieldKeyCaller && r.Caller != nil: // caller eg: "logger_test.go:48"
94+
tplData[tplVar] = formatCallerToString(r.Caller)
9395
case field == FieldKeyLevel:
9496
tplData[tplVar] = r.LevelName
9597
case field == FieldKeyChannel:
@@ -130,6 +132,8 @@ func (f *TextFormatter) formatWithColor(r *Record) ([]byte, error) {
130132
}
131133

132134
tplData[tplVar] = r.Time.Format(f.TimeFormat)
135+
case field == FieldKeyCaller && r.Caller != nil: // caller eg: "logger_test.go:48"
136+
tplData[tplVar] = formatCallerToString(r.Caller)
133137
case field == FieldKeyLevel:
134138
tplData[tplVar] = f.renderColorByLevel(r.LevelName, r.Level)
135139
case field == FieldKeyChannel:

handler/console.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type ConsoleHandler struct {
1919
// NewConsoleHandler create new ConsoleHandler
2020
func NewConsoleHandler(levels []slog.Level) *ConsoleHandler {
2121
h := &ConsoleHandler{}
22-
h.Out = os.Stdout
22+
h.Output = os.Stdout
2323
h.Levels = levels
2424

2525
// create new formatter

handler/stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
type StreamHandler struct {
1111
BaseHandler
1212

13-
// Out io.WriteCloser
14-
Out io.Writer
13+
// Output io.WriteCloser
14+
Output io.Writer
1515

1616
UseLock bool
1717
}
1818

1919
// NewStreamHandler create new StreamHandler
2020
func NewStreamHandler(out io.Writer, levels []slog.Level) *StreamHandler {
2121
return &StreamHandler{
22-
Out: out,
22+
Output: out,
2323
BaseHandler: BaseHandler{
2424
Levels: levels,
2525
},
@@ -39,7 +39,7 @@ func (h *StreamHandler) Handle(record *slog.Record) error {
3939
return err
4040
}
4141

42-
_, err = h.Out.Write(bts)
42+
_, err = h.Output.Write(bts)
4343
return err
4444
}
4545

logger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func New() *Logger {
3535
return NewWithName("")
3636
}
3737

38+
// NewWithConfig create an new logger with config func
39+
func NewWithConfig(fn func(logger *Logger)) *Logger {
40+
return New().Configure(fn)
41+
}
42+
3843
// NewWithHandlers create an new logger with handlers
3944
func NewWithHandlers(hs ...Handler) *Logger {
4045
logger := NewWithName("")

logger_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package slog_test
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/gookit/slog"
8+
"github.com/gookit/slog/handler"
79
"github.com/stretchr/testify/assert"
810
)
911

@@ -21,3 +23,29 @@ func TestLoggerBasic(t *testing.T) {
2123
func TestLogger_AddHandlers(t *testing.T) {
2224

2325
}
26+
27+
type bufferHandler struct {
28+
handler.BaseHandler
29+
}
30+
31+
func (h *bufferHandler) Handle(*slog.Record) error {
32+
panic("implement me")
33+
}
34+
35+
func TestLogger_ReportCaller(t *testing.T) {
36+
l := slog.NewWithConfig(func(logger *slog.Logger) {
37+
logger.ReportCaller = true
38+
})
39+
40+
var buf bytes.Buffer
41+
h := handler.NewStreamHandler(&buf, slog.AllLevels)
42+
h.SetFormatter(slog.NewJSONFormatter(func(f *slog.JSONFormatter) {
43+
f.Fields = append(f.Fields, slog.FieldKeyCaller)
44+
}))
45+
46+
l.AddHandler(h)
47+
l.Info("message")
48+
49+
str := buf.String()
50+
assert.Contains(t, str, `"caller":"logger_test.go`)
51+
}

slog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (sl *SugaredLogger) Reset() {
120120
// It is directly available without any additional configuration
121121
var std = newStdLogger()
122122

123-
func newStdLogger() *SugaredLogger {
123+
func newStdLogger() *SugaredLogger {
124124
return NewSugaredLogger(os.Stdout, ErrorLevel).Configure(func(sl *SugaredLogger) {
125125
sl.SetName("stdLogger")
126126
// auto enable console color

testdata/runtime.Frame.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*runtime.Frame {
2+
PC: 0x73139f,
3+
Func: &runtime.Func{opaque:struct {}{}},
4+
Function: "github.com/gookit/slog_test.TestLogger_ReportCaller",
5+
File: "F:/work/go/gookit/slog/logger_test.go",
6+
Line: 47,
7+
Entry: 0x7311b0,
8+
funcInfo: "<runtime.funcInfo Value>",
9+
}

0 commit comments

Comments
 (0)