Skip to content

Commit

Permalink
Add back some deprecated logging functions
Browse files Browse the repository at this point in the history
Change-Id: Ie213e668a927342c31deb398f19d8982b1a2169a
  • Loading branch information
jxskiss committed Dec 29, 2024
1 parent 4fd47ff commit e0e3abf
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
113 changes: 113 additions & 0 deletions zlog/deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//nolint:staticcheck
package zlog

import (
"fmt"
"log/slog"
"os"
)

// Debugf ...
// Deprecated: this function will be removed in the future, please use Debug instead.
func Debugf(format string, args ...any) {
l := Default()
if l.Enabled(nil, slog.LevelDebug) {
msg := formatMessage(format, args)
_log(nil, 0, l, slog.LevelDebug, msg, nil)
}
}

// Infof ...
// Deprecated: this function will be removed in the future, please use Info instead.
func Infof(format string, args ...any) {
l := Default()
if l.Enabled(nil, slog.LevelInfo) {
msg := formatMessage(format, args)
_log(nil, 0, l, slog.LevelInfo, msg, nil)
}
}

// Warnf ...
// Deprecated: this function will be removed in the future, please use Warn instead.
func Warnf(format string, args ...any) {
l := Default()
if l.Enabled(nil, slog.LevelWarn) {
msg := formatMessage(format, args)
_log(nil, 0, l, slog.LevelWarn, msg, nil)
}
}

// Errorf ...
// Deprecated: this function will be removed in the future, please use Error instead.
func Errorf(format string, args ...any) {
msg := formatMessage(format, args)
_log(nil, 0, Default(), slog.LevelError, msg, nil)
}

// Fatalf is equivalent to Errorf() followed by a call to os.Exit(1).
// Deprecated: this function will be removed in the future, please use Fatal instead.
func Fatalf(format string, args ...any) {
msg := formatMessage(format, args)
_log(nil, 0, Default(), slog.LevelError, msg, nil)
os.Exit(1)
}

// Print uses [fmt.Sprint] to log a message at InfoLevel, or level
// detected from args, if it's enabled.
// It has same signature with [log.Print].
//
// Deprecated: this function will be removed in the future, please use
// Logger or the leveled logging functions.
func Print(args ...any) {
l := Default()
level := slog.LevelInfo
if len(args) > 0 {
s, _ := args[0].(string)
level = detectLevel(s)
}
if l.Enabled(nil, level) {
msg := fmt.Sprint(args...)
_log(nil, 0, l, level, msg, nil)
}
}

// Printf uses [fmt.Sprintf] to log a message at InfoLevel, or level
// detected from args, if it's enabled.
// It has same signature with [log.Printf].
//
// Deprecated: this function will be removed in the future, please use
// Logger or the leveled logging functions.
func Printf(format string, args ...any) {
l := Default()
level := detectLevel(format)
if l.Enabled(nil, level) {
msg := formatMessage(format, args)
_log(nil, 0, l, level, msg, nil)
}
}

// Println uses [fmt.Sprintln] to log a message at InfoLevel, or level
// detected from args, if it's enabled.
// It has same signature with [log.Println].
//
// Deprecated: this function will be removed in the future, please use
// Logger or the leveled logging functions.
func Println(args ...any) {
l := Default()
level := slog.LevelInfo
if len(args) > 0 {
s, _ := args[0].(string)
level = detectLevel(s)
}
if l.Enabled(nil, level) {
msg := fmt.Sprintln(args...)
_log(nil, 0, l, level, msg, nil)
}
}

func formatMessage(format string, args []any) string {
if len(args) == 0 {
return format
}
return fmt.Sprintf(format, args...)
}
4 changes: 2 additions & 2 deletions zlog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func Fatal(ctx context.Context, msg string, args ...any) {
}

// _log is the low-level logging method for methods that take ...any.
// It must always be called directly by an exported logging method
// or function, because it uses a fixed call depth to obtain the pc.
// Param skip can be used to skip call stacks when obtaining the pc,
// to get correct source information.
func _log(ctx context.Context, skip int, l *Logger, level slog.Level, msg string, args []any) {
if ctx == nil {
ctx = context.Background()
Expand Down

0 comments on commit e0e3abf

Please sign in to comment.