Skip to content

Commit

Permalink
完善日志包
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Sep 12, 2021
1 parent 61f78c5 commit e481bdd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
59 changes: 28 additions & 31 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package logger

import (
"context"
"time"
)

// Colors
// Console Colors
const (
Reset = "\033[0m"
Red = "\033[31m"
Expand All @@ -21,11 +20,11 @@ const (
YellowBold = "\033[33;1m"
)

// LogLevel
type LogLevel int
// Log Level
type Level int

const (
Silent LogLevel = iota + 1
Silent Level = iota
Error
Warn
Info
Expand All @@ -36,65 +35,63 @@ type Writer interface {
Printf(string, ...interface{})
}

type Config struct {
SlowThreshold time.Duration
Colorful bool
IgnoreRecordNotFoundError bool
LogLevel LogLevel
}

// Logger interface
type Logger interface {
Info(context.Context, string, ...interface{})
Warn(context.Context, string, ...interface{})
Error(context.Context, string, ...interface{})
SetLevel(Level)
}

func NewLogger(writer Writer, config Config) Logger {
var (
infoStr = "[info] "
warnStr = "[warn] "
errStr = "[error] "
)
func NewLogger(writer Writer, level Level, colorful bool) Logger {
infoStr := "[info] "
warnStr := "[warn] "
errStr := "[error] "

if config.Colorful {
if colorful {
infoStr = Green + "[info] " + Reset
warnStr = Magenta + "[warn] " + Reset
errStr = Red + "[error] " + Reset
}

return &logger{
Writer: writer,
Config: config,
infoStr: infoStr,
warnStr: warnStr,
errStr: errStr,
Writer: writer,
Colorful: colorful,
infoStr: infoStr,
warnStr: warnStr,
errStr: errStr,
Level: level,
}
}

type logger struct {
Writer
Config
Colorful bool
Level Level
infoStr, warnStr, errStr string
}

// Info print info
func (l logger) Info(ctx context.Context, msg string, data ...interface{}) {
if l.LogLevel >= Info {
func (l *logger) Info(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Info {
l.Printf(l.infoStr+msg, data...)
}
}

// Warn print warn messages
func (l logger) Warn(ctx context.Context, msg string, data ...interface{}) {
if l.LogLevel >= Warn {
func (l *logger) Warn(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Warn {
l.Printf(l.warnStr+msg, data...)
}
}

// Error print error messages
func (l logger) Error(ctx context.Context, msg string, data ...interface{}) {
if l.LogLevel >= Error {
func (l *logger) Error(ctx context.Context, msg string, data ...interface{}) {
if l.Level >= Error {
l.Printf(l.errStr+msg, data...)
}
}

func (l *logger) SetLevel(lv Level) {
l.Level = lv
}
14 changes: 8 additions & 6 deletions weapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/medivhzhan/weapp/v3/cache"
"github.com/medivhzhan/weapp/v3/livebroadcast"
Expand Down Expand Up @@ -64,11 +63,7 @@ func NewClient(appid, secret string, opts ...func(*Client)) *Client {
}

if cli.logger == nil {
cli.logger = logger.NewLogger(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 3 * time.Second,
LogLevel: logger.Info,
Colorful: true,
})
cli.logger = logger.NewLogger(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Info, true)
}

return cli
Expand Down Expand Up @@ -130,6 +125,13 @@ func bool2int(ok bool) uint8 {
// 获取日志记录器
func (cli *Client) Logger() logger.Logger { return cli.logger }

// 设置日志等级
func (cli *Client) SetLogLevel(lv logger.Level) {
if cli.logger != nil {
cli.logger.SetLevel(lv)
}
}

// 拼凑完整的 URI
func (cli *Client) conbineURI(url string, req interface{}) (string, error) {

Expand Down

0 comments on commit e481bdd

Please sign in to comment.