Skip to content

Commit

Permalink
去除 level 设计
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 5, 2023
1 parent ae31e22 commit dd138a4
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 317 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newDefaultConfig() *config {
}

conf := &config{
level: levelDebug,
level: slog.LevelDebug,
newWriter: newWriter,
wrapWriter: nil,
newHandler: handler.NewTextHandler,
Expand Down
4 changes: 2 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestConfigHandlerOptions(t *testing.T) {
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr { return attr }

conf := &config{
level: levelWarn,
level: slog.LevelWarn,
withSource: true,
replaceAttr: replaceAttr,
}
Expand All @@ -58,7 +58,7 @@ func TestConfigHandler(t *testing.T) {
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr { return attr }

conf := &config{
level: levelWarn,
level: slog.LevelWarn,
withSource: true,
replaceAttr: replaceAttr,

Expand Down
22 changes: 6 additions & 16 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,16 @@ import (

type contextKey struct{}

// NewContextWithKey wraps context with logger of key and returns a new context.
func NewContextWithKey(ctx context.Context, key interface{}, logger *Logger) context.Context {
return context.WithValue(ctx, key, logger)
}

// FromContextWithKey gets logger from context of key and returns the default logger if missed.
func FromContextWithKey(ctx context.Context, key interface{}) *Logger {
if logger, ok := ctx.Value(key).(*Logger); ok {
return logger
}

return Default()
}

// NewContext wraps context with logger and returns a new context.
func NewContext(ctx context.Context, logger *Logger) context.Context {
return NewContextWithKey(ctx, contextKey{}, logger)
return context.WithValue(ctx, contextKey{}, logger)
}

// FromContext gets logger from context and returns the default logger if missed.
func FromContext(ctx context.Context) *Logger {
return FromContextWithKey(ctx, contextKey{})
if logger, ok := ctx.Value(contextKey{}).(*Logger); ok {
return logger
}

return Default()
}
40 changes: 0 additions & 40 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,6 @@ import (
"testing"
)

// go test -v -cover -run=^TestNewContextWithKey$
func TestNewContextWithKey(t *testing.T) {
key := "key"
logger := NewLogger()
ctx := NewContextWithKey(context.Background(), key, logger)

value := ctx.Value(key)
if value == nil {
t.Fatal("value == nil")
}

contextLogger, ok := value.(*Logger)
if !ok {
t.Fatalf("value type %T is wrong", value)
}

if contextLogger != logger {
t.Fatalf("contextLogger %+v != logger %+v", contextLogger, logger)
}
}

// go test -v -cover -run=^TestFromContextWithKey$
func TestFromContextWithKey(t *testing.T) {
ctx := context.Background()

key := "key"
logger := FromContextWithKey(ctx, key)

if logger == nil {
t.Fatal("logger == nil")
}

logger = NewLogger()
contextLogger := FromContextWithKey(context.WithValue(ctx, key, logger), key)

if contextLogger != logger {
t.Fatalf("contextLogger %+v != logger %+v", contextLogger, logger)
}
}

// go test -v -cover -run=^TestNewContext$
func TestNewContext(t *testing.T) {
logger := NewLogger()
Expand Down
39 changes: 9 additions & 30 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package logit

import (
"context"
"fmt"
"log/slog"
"sync/atomic"
)

Expand All @@ -38,61 +38,40 @@ func Default() *Logger {

// Debug logs a log with msg and args in debug level.
func Debug(msg string, args ...any) {
Default().log(context.Background(), levelDebug, msg, args...)
Default().log(context.Background(), slog.LevelDebug, msg, args...)
}

// Info logs a log with msg and args in info level.
func Info(msg string, args ...any) {
Default().log(context.Background(), levelInfo, msg, args...)
Default().log(context.Background(), slog.LevelInfo, msg, args...)
}

// Warn logs a log with msg and args in warn level.
func Warn(msg string, args ...any) {
Default().log(context.Background(), levelWarn, msg, args...)
Default().log(context.Background(), slog.LevelWarn, msg, args...)
}

// Error logs a log with msg and args in error level.
func Error(msg string, args ...any) {
Default().log(context.Background(), levelError, msg, args...)
Default().log(context.Background(), slog.LevelError, msg, args...)
}

// DebugContext logs a log with ctx, msg and args in debug level.
func DebugContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, levelDebug, msg, args...)
Default().log(ctx, slog.LevelDebug, msg, args...)
}

// InfoContext logs a log with ctx, msg and args in info level.
func InfoContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, levelInfo, msg, args...)
Default().log(ctx, slog.LevelInfo, msg, args...)
}

// WarnContext logs a log with ctx, msg and args in warn level.
func WarnContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, levelWarn, msg, args...)
Default().log(ctx, slog.LevelWarn, msg, args...)
}

// ErrorContext logs a log with ctx, msg and args in error level.
func ErrorContext(ctx context.Context, msg string, args ...any) {
Default().log(ctx, levelError, msg, args...)
}

// Printf logs a log with format and args in print level.
// It a old-school way to log.
func Printf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
Default().log(context.Background(), levelPrint, msg)
}

// Print logs a log with args in print level.
// It a old-school way to log.
func Print(args ...interface{}) {
msg := fmt.Sprint(args...)
Default().log(context.Background(), levelPrint, msg)
}

// Println logs a log with args in print level.
// It a old-school way to log.
func Println(args ...interface{}) {
msg := fmt.Sprintln(args...)
Default().log(context.Background(), levelPrint, msg)
Default().log(ctx, slog.LevelError, msg, args...)
}
4 changes: 4 additions & 0 deletions defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package defaults

import (
"encoding/json"
"log/slog"
"os"
"time"
)
Expand All @@ -24,6 +25,9 @@ var (
// CallerDepth is the depth of caller.
// See runtime.Caller.
CallerDepth = 4

// LevelPrint is the level used for printing logs.
LevelPrint = slog.LevelInfo
)

var (
Expand Down
67 changes: 0 additions & 67 deletions level.go

This file was deleted.

78 changes: 0 additions & 78 deletions level_test.go

This file was deleted.

Loading

0 comments on commit dd138a4

Please sign in to comment.