Skip to content

Commit

Permalink
增加包级别的日志函数
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 21, 2023
1 parent b9c5213 commit 92dd0ee
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 16 deletions.
7 changes: 4 additions & 3 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v1.5.6-alpha (Coming soon)
### v1.5.6-alpha

> 此版本发布于 2023-12-25
> 此版本发布于 2023-12-21
* 完善单元测试,提高覆盖率到 80%
* 增加包级别的日志方法
* PS: 周董的新歌《圣诞星》上线了,怎么说呢哈哈,周董的光芒还是留在回忆里面吧~

### v1.5.5-alpha

Expand Down
6 changes: 4 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import (
func main() {
// Use default logger to log.
// By default, logs will be output to stdout.
// logit.Default() returns the default logger, but we also provide some common logging functions.
logit.Info("hello from logit", "key", 123)
logit.Default().Info("hello from logit", "key", 123)

// Use a new logger to log.
Expand All @@ -77,9 +79,9 @@ func main() {
logger.Info("check where I'm logged", "file", "logit.log")

// What if I want to use default logger and output logs to a file? Try SetDefault.
// It sets a logger to default and you can use it by package function or Default().
// It sets a logger to default and you can use it by package functions or Default().
logit.SetDefault(logger)
logit.Default().Warn("this is from default logger", "pi", 3.14, "default", true)
logit.Warn("this is from default logger", "pi", 3.14, "default", true)

// If you want to change level of logger to info, try WithInfoLevel.
// Other levels is similar to info level.
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
func main() {
// Use default logger to log.
// By default, logs will be output to stdout.
// logit.Default() returns the default logger, but we also provide some common logging functions.
logit.Info("hello from logit", "key", 123)
logit.Default().Info("hello from logit", "key", 123)

// Use a new logger to log.
Expand All @@ -74,9 +76,9 @@ func main() {
logger.Info("check where I'm logged", "file", "logit.log")

// What if I want to use default logger and output logs to a file? Try SetDefault.
// It sets a logger to default and you can use it by package function or Default().
// It sets a logger to default and you can use it by package functions or Default().
logit.SetDefault(logger)
logit.Default().Warn("this is from default logger", "pi", 3.14, "default", true)
logit.Warn("this is from default logger", "pi", 3.14, "default", true)

// If you want to change level of logger to info, try WithInfoLevel.
// Other levels is similar to info level.
Expand Down
6 changes: 4 additions & 2 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
func main() {
// Use default logger to log.
// By default, logs will be output to stdout.
// logit.Default() returns the default logger, but we also provide some common logging functions.
logit.Info("hello from logit", "key", 123)
logit.Default().Info("hello from logit", "key", 123)

// Use a new logger to log.
Expand All @@ -50,9 +52,9 @@ func main() {
logger.Info("check where I'm logged", "file", "logit.log")

// What if I want to use default logger and output logs to a file? Try SetDefault.
// It sets a logger to default and you can use it by package function or Default().
// It sets a logger to default and you can use it by package functions or Default().
logit.SetDefault(logger)
logit.Default().Warn("this is from default logger", "pi", 3.14, "default", true)
logit.Warn("this is from default logger", "pi", 3.14, "default", true)

// If you want to change level of logger to info, try WithInfoLevel.
// Other levels is similar to info level.
Expand Down
4 changes: 2 additions & 2 deletions _examples/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func main() {
return time.Unix(666, 0).In(time.Local)
}

logit.Default().Print("println log is info level")
logit.Print("println log is info level")

// If you want change the level of old-school logging methods:
defaults.LevelPrint = slog.LevelDebug

logit.Default().Print("println log is debug level now")
logit.Print("println log is debug level now")

// More fields see defaults package.
defaults.HandleError = func(label string, err error) {
Expand Down
45 changes: 45 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
package logit

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

"github.com/FishGoddess/logit/defaults"
)

var defaultLogger atomic.Value
Expand All @@ -33,3 +37,44 @@ func SetDefault(logger *Logger) {
func Default() *Logger {
return defaultLogger.Load().(*Logger)
}

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

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

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

// Error logs a log with msg and args in error level.
func Error(msg string, args ...any) {
Default().log(slog.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(defaults.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(defaults.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(defaults.LevelPrint, msg)
}
43 changes: 43 additions & 0 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
package logit

import (
"bytes"
"io"
"log/slog"
"testing"

"github.com/FishGoddess/logit/handler"
)

// go test -v -cover -count=1 -test.cpu=1 -run=^TestSetDefault$
Expand Down Expand Up @@ -45,3 +50,41 @@ func TestDefault(t *testing.T) {
t.Fatalf("gotLogger %+v != logger %+v", gotLogger, logger)
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestDefaultLogger$
func TestDefaultLogger(t *testing.T) {
handlerName := t.Name()

newHandler := func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
return slog.NewTextHandler(w, opts)
}

handler.Register(handlerName, newHandler)

buffer := bytes.NewBuffer(make([]byte, 0, 1024))
logger := NewLogger(
WithDebugLevel(), WithHandler(handlerName), WithWriter(buffer), WithSource(), WithPID(),
)

SetDefault(logger)
Debug("debug msg", "key1", 1)
Info("info msg", "key2", 2)
Warn("warn msg", "key3", 3)
Error("error msg", "key4", 4)

opts := &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}
wantBuffer := bytes.NewBuffer(make([]byte, 0, 1024))
slogLogger := slog.New(newHandler(wantBuffer, opts)).With(keyPID, pid)

slogLogger.Debug("debug msg", "key1", 1)
slogLogger.Info("info msg", "key2", 2)
slogLogger.Warn("warn msg", "key3", 3)
slogLogger.Error("error msg", "key4", 4)

got := removeTimeAndSource(buffer.String())
want := removeTimeAndSource(wantBuffer.String())

if got != want {
t.Fatalf("got %s != want %s", got, want)
}
}
12 changes: 7 additions & 5 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Package logit provides an easy way to use foundation for your logging operations
// Use default logger to log.
// By default, logs will be output to stdout.
// logit.Default() returns the default logger, but we also provide some common logging functions.
logit.Info("hello from logit", "key", 123)
logit.Default().Info("hello from logit", "key", 123)
// Use a new logger to log.
Expand All @@ -45,9 +47,9 @@ Package logit provides an easy way to use foundation for your logging operations
logger.Info("check where I'm logged", "file", "logit.log")
// What if I want to use default logger and output logs to a file? Try SetDefault.
// It sets a logger to default and you can use it by package function or Default().
// It sets a logger to default and you can use it by package functions or Default().
logit.SetDefault(logger)
logit.Default().Warn("this is from default logger", "pi", 3.14, "default", true)
logit.Warn("this is from default logger", "pi", 3.14, "default", true)
// If you want to change level of logger to info, try WithInfoLevel.
// Other levels is similar to info level.
Expand Down Expand Up @@ -261,12 +263,12 @@ Package logit provides an easy way to use foundation for your logging operations
return time.Unix(666, 0).In(time.Local)
}
logit.Default().Print("println log is info level")
logit.Print("println log is info level")
// If you want change the level of old-school logging methods:
defaults.LevelPrint = slog.LevelDebug
logit.Default().Print("println log is debug level now")
logit.Print("println log is debug level now")
// More fields see defaults package.
defaults.HandleError = func(label string, err error) {
Expand Down Expand Up @@ -315,5 +317,5 @@ package logit // import "github.com/FishGoddess/logit"

const (
// Version is the version string representation of logit.
Version = "v1.5.5-alpha"
Version = "v1.5.6-alpha"
)

0 comments on commit 92dd0ee

Please sign in to comment.