Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): add Check for the logger #34

Merged
merged 1 commit into from
Apr 10, 2024
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
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
Loading