Skip to content

Commit

Permalink
feat(logger): add Check for the logger (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
shipengqi committed Apr 10, 2024
1 parent d8f9846 commit f2e3d2b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,10 @@ func Flush() error { return _globalL.Flush() }

// Close implements io.Closer, and closes the current logfile of default logger.
func Close() error { return _globalL.Close() }

// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
func Check(lvl Level, msg string) *CheckedEntry {
return _globalL.Check(lvl, msg)
}
19 changes: 19 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,25 @@ func TestStdInfoLogger(t *testing.T) {
})
}

func TestCheck(t *testing.T) {
r, w, _ := os.Pipe()
tmp := os.Stdout
defer func() {
os.Stdout = tmp
}()
os.Stdout = w
opts := NewOptions()
Configure(opts)

ce := Check(InfoLevel, "Hello, world!")
if ce != nil {
ce.Write(String("checked field", "checked field value"))
}
_ = w.Close()
stdout, _ := io.ReadAll(r)
assert.Contains(t, string(stdout), "Hello, world! {\"checked field\": \"checked field value\"}")
}

// fileWithLineNum return the file name and line number of the current file
func fileWithLineNum() string {
for i := 4; i < 15; i++ {
Expand Down
11 changes: 11 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func (l *Logger) AtLevelf(level Level, msg string, args ...interface{}) {
}

// Sugared returns sugared logger.
// SugaredLogger wraps the Logger to provide a more ergonomic, but slightly slower,
// API. Sugaring a Logger is quite inexpensive, so it's reasonable for a
// single application to use both Loggers and SugaredLoggers, converting
// between them on the boundaries of performance-sensitive code.
func (l *Logger) Sugared() *zap.SugaredLogger {
return l.sugared
}
Expand Down Expand Up @@ -296,6 +300,13 @@ func (l *Logger) Close() error {
return nil
}

// Check returns a CheckedEntry if logging a message at the specified level
// is enabled. It's a completely optional optimization; in high-performance
// applications, Check can help avoid allocating a slice to hold fields.
func (l *Logger) Check(lvl Level, msg string) *CheckedEntry {
return l.log.Check(lvl, msg)
}

func (l *Logger) EncodedFilename() string {
return l.encodedFilename
}
Expand Down
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Field = zapcore.Field
// Level is an alias for the zapcore.Level.
type Level = zapcore.Level

// CheckedEntry is an alias for the zapcore.CheckedEntry.
type CheckedEntry = zapcore.CheckedEntry

// TimeEncoder is an alias for the zapcore.TimeEncoder.
type TimeEncoder = zapcore.TimeEncoder

Expand Down

0 comments on commit f2e3d2b

Please sign in to comment.