Skip to content

Commit

Permalink
refactor: 直接采用 github.com/issue9/logs 代替 logs
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 24, 2023
1 parent da081d6 commit 3ddda6a
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 479 deletions.
3 changes: 1 addition & 2 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/issue9/web"
"github.com/issue9/web/locales"
"github.com/issue9/web/logs"
"github.com/issue9/web/server"
)

Expand All @@ -32,7 +31,7 @@ type configOf[T any] struct {
//
// 如果为空,所有日志输出都将被抛弃。
Logs *logsConfig `yaml:"logs,omitempty" xml:"logs,omitempty" json:"logs,omitempty"`
logs *logs.Options
logs *server.Logs
cleanup []func() error

// 指定默认语言
Expand Down
28 changes: 14 additions & 14 deletions app/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"strconv"
"strings"

xlogs "github.com/issue9/logs/v7"
"github.com/issue9/logs/v7"
"github.com/issue9/term/v3/colors"

"github.com/issue9/web"
"github.com/issue9/web/locales"
"github.com/issue9/web/logs"
"github.com/issue9/web/server"
)

var logHandlersFactory = map[string]LogsHandlerBuilder{}
Expand All @@ -41,10 +41,10 @@ type logsConfig struct {

// 日志输出对象的配置
//
// 为空表示 [logs.NewNopHandler] 返回的对象。
// 为空表示 [server.NewNopHandler] 返回的对象。
Handlers []*logHandlerConfig `xml:"writer" json:"writers" yaml:"writers"`

logs *logs.Options
logs *server.Logs
cleanup []func() error
}

Expand Down Expand Up @@ -98,7 +98,7 @@ type logHandlerConfig struct {

func (conf *logsConfig) build() *web.FieldError {
if conf.logs == nil {
conf.logs = &logs.Options{}
conf.logs = &server.Logs{}
}

if len(conf.Levels) == 0 {
Expand All @@ -110,7 +110,7 @@ func (conf *logsConfig) build() *web.FieldError {
return err
}

conf.logs = &logs.Options{
conf.logs = &server.Logs{
Handler: w,
Created: conf.Created,
Location: conf.Location,
Expand Down Expand Up @@ -192,25 +192,25 @@ func newFileLogsHandler(args []string) (logs.Handler, func() error, error) {
return nil, nil, err
}

w, err := logs.NewRotateFile(args[1], args[0], size)
w, err := server.NewRotateFile(args[1], args[0], size)
if err != nil {
return nil, nil, err
}

if len(args) < 4 || args[3] == "text" {
return logs.NewTextHandler(w), w.Close, nil
return server.NewTextHandler(w), w.Close, nil
}
return logs.NewJSONHandler(w), w.Close, nil
return server.NewJSONHandler(w), w.Close, nil
}

func newSMTPLogsHandler(args []string) (logs.Handler, func() error, error) {
sendTo := strings.Split(args[4], ",")
w := logs.NewSMTP(args[0], args[1], args[2], args[3], sendTo)
w := server.NewSMTP(args[0], args[1], args[2], args[3], sendTo)

if len(args) < 6 || args[6] == "text" {
return logs.NewTextHandler(w), nil, nil
return server.NewTextHandler(w), nil, nil
}
return logs.NewJSONHandler(w), nil, nil
return server.NewJSONHandler(w), nil, nil
}

var colorMap = map[string]colors.Color{
Expand Down Expand Up @@ -255,7 +255,7 @@ func newTermLogsHandler(args []string) (logs.Handler, func() error, error) {
return nil, nil, web.NewFieldError("Args["+strconv.Itoa(1+index)+"]", locales.InvalidValue)
}

lv, err := xlogs.ParseLevel(a[0])
lv, err := logs.ParseLevel(a[0])
if err != nil {
return nil, nil, web.NewFieldError("Args["+strconv.Itoa(1+index)+"]", err)
}
Expand All @@ -268,5 +268,5 @@ func newTermLogsHandler(args []string) (logs.Handler, func() error, error) {
cs[lv] = c
}

return logs.NewTermHandler(w, cs), nil, nil
return server.NewTermHandler(w, cs), nil, nil
}
37 changes: 5 additions & 32 deletions app/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/logs/v7"

"github.com/issue9/web"
"github.com/issue9/web/logs"
"github.com/issue9/web/server"
)

func TestLogsConfig_build(t *testing.T) {
Expand All @@ -17,45 +18,17 @@ func TestLogsConfig_build(t *testing.T) {
conf := &logsConfig{}
err := conf.build()
a.NotError(err).NotNil(conf.logs).Length(conf.cleanup, 0).
Equal(conf.logs.Levels, logs.AllLevels()).
Equal(conf.logs.Levels, server.AllLevels()).
Empty(conf.logs.Created)

conf = &logsConfig{Levels: []logs.Level{logs.Warn, logs.Error}, Created: logs.NanoLayout}
conf = &logsConfig{Levels: []logs.Level{logs.LevelWarn, logs.LevelError}, Created: logs.NanoLayout}
err = conf.build()
a.NotError(err).NotNil(conf.logs).Length(conf.cleanup, 0).
Equal(conf.logs.Levels, []logs.Level{logs.Warn, logs.Error}).
Equal(conf.logs.Levels, []logs.Level{logs.LevelWarn, logs.LevelError}).
Equal(conf.logs.Created, logs.NanoLayout).
False(conf.logs.Location)
}

func TestLogsConfig_output(t *testing.T) {
a := assert.New(t, false)

conf := &logsConfig{
Created: "2006",
Handlers: []*logHandlerConfig{
{
Type: "file",
Args: []string{"./testdata", "1504-%i.log", "1024"},
},
{
Type: "term",
Args: []string{"stdout"},
},
{
Type: "term",
Args: []string{"stdout", "erro:red", "warn:yellow"},
},
},
}
err := conf.build()
a.NotError(err).NotNil(conf.logs).Length(conf.cleanup, 1) // 文件有 cleanup 返回
l, err1 := logs.New(nil, conf.logs)
a.NotError(err1).NotNil(l)
l.ERROR().Print("test")
a.NotError(conf.cleanup[0]())
}

func TestNewTermHandler(t *testing.T) {
a := assert.New(t, false)

Expand Down
4 changes: 2 additions & 2 deletions cmd/web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/issue9/cmdopt v0.13.0
github.com/issue9/errwrap v0.3.1
github.com/issue9/localeutil v0.25.0
github.com/issue9/logs/v7 v7.3.0
github.com/issue9/query/v3 v3.1.2
github.com/issue9/source v0.7.0
github.com/issue9/term/v3 v3.2.4
Expand All @@ -29,9 +30,8 @@ require (
github.com/issue9/cache v0.8.0 // indirect
github.com/issue9/config v0.6.1 // indirect
github.com/issue9/conv v1.3.4 // indirect
github.com/issue9/logs/v7 v7.1.0 // indirect
github.com/issue9/mux/v7 v7.3.3 // indirect
github.com/issue9/scheduled v0.15.1 // indirect
github.com/issue9/scheduled v0.16.0 // indirect
github.com/issue9/sliceutil v0.15.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down
8 changes: 4 additions & 4 deletions cmd/web/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ github.com/issue9/errwrap v0.3.1 h1:8g4lYJaGnoiXyZ1oZyH/7zPDGgw5RNiE9Q6ri9kE6Z8=
github.com/issue9/errwrap v0.3.1/go.mod h1:HLR0e5iimd2aJXM9YrThOsRj3/6lMtk77lVp7zyvJ4E=
github.com/issue9/localeutil v0.25.0 h1:RJPsbfnRt1FauXjAgkRdKtdXiM+FqdfUT0J/axGJIVU=
github.com/issue9/localeutil v0.25.0/go.mod h1:JvTb8B/2oVEZU1VHrBJXPHlE/1gZJFRMcF/ziKD8JJY=
github.com/issue9/logs/v7 v7.1.0 h1:4SafDtfa4hMOTeEpQRX25YOvWw/don65QqxOA1iSa0M=
github.com/issue9/logs/v7 v7.1.0/go.mod h1:WfimUOXUkMUq8inRXUfUAN7KkDUdMkdRC0xrP3nCpxA=
github.com/issue9/logs/v7 v7.3.0 h1:MHtmiJOWR5zN1ibTgxfetGCnIGAEhJgTgl3bqxAzF5k=
github.com/issue9/logs/v7 v7.3.0/go.mod h1:+aLERkBvHbu8Olg8Du1EFG4CaAK+0rqulYHmpVxgF00=
github.com/issue9/mux/v7 v7.3.3 h1:cUKmaNspmjG1AmAwtPDWM/CjkicbhX0jbsUXrZdFeu0=
github.com/issue9/mux/v7 v7.3.3/go.mod h1:Jip6le3ZVLLV6ecA4JdElD3Z7A97IvkW8fbh1vlhRqQ=
github.com/issue9/query/v3 v3.1.2 h1:+ObxriMUTmv9qq8E4HWVByv9W2hnEXFw/FI69BlomO0=
github.com/issue9/query/v3 v3.1.2/go.mod h1:6SZyc4Ide0YuwRJk4w5aTzVJzQVU2smoWg+KlQiN+n4=
github.com/issue9/rands/v2 v2.0.0 h1:SdG8iflIzuWpa6CKLmMwn4pTZj+mSxS7s+1TCH6WXUE=
github.com/issue9/rands/v2 v2.0.0/go.mod h1:wTOXDlW9QUtba7o3I/RayfjH7mLYrER2TFHaIeKe96o=
github.com/issue9/scheduled v0.15.1 h1:tq2Q5N9J2lmaS+hw/24c+/DCQMB8t8MsY5v2SbiVc6A=
github.com/issue9/scheduled v0.15.1/go.mod h1:7Up90hFy4eZKYkAZOxDO7dhhZk7Aq+LRbOcCzTa57cc=
github.com/issue9/scheduled v0.16.0 h1:MhjxcHkcS+ui/X09iCyFZj6pdiW0fr3QN58AUpsP+ro=
github.com/issue9/scheduled v0.16.0/go.mod h1:7Up90hFy4eZKYkAZOxDO7dhhZk7Aq+LRbOcCzTa57cc=
github.com/issue9/sliceutil v0.15.0 h1:E6Xnl3FY5h0ZGNzyx1VEFAfGdParaq/BkX1QQR0uFwI=
github.com/issue9/sliceutil v0.15.0/go.mod h1:n9meV7AamDhmehOBuV4GrxW3yw7O1cZmLx3Xizg1bps=
github.com/issue9/source v0.7.0 h1:4rmXN3STbF9w+19eQeVLhZ6+5FAhdLbX0DJlhW4C8ys=
Expand Down
14 changes: 7 additions & 7 deletions cmd/web/restdoc/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import (
"fmt"
"go/scanner"

"github.com/issue9/logs/v7"
"github.com/issue9/web"
"github.com/issue9/web/logs"
"golang.org/x/mod/modfile"
)

type Logger struct {
logs logs.Logs
logs *logs.Logs
count int
hasErr bool
}

func New(l logs.Logs) *Logger { return &Logger{logs: l} }
func New(l *logs.Logs) *Logger { return &Logger{logs: l} }

// Count 接收到的日志数量
func (l *Logger) Count() int { return l.count }

// Info 输出提示信息
func (l *Logger) Info(msg any) { l.log(logs.Info, msg, "", 0) }
func (l *Logger) Info(msg any) { l.log(logs.LevelInfo, msg, "", 0) }

// Warning 输出警告信息
func (l *Logger) Warning(msg any) { l.log(logs.Warn, msg, "", 0) }
func (l *Logger) Warning(msg any) { l.log(logs.LevelWarn, msg, "", 0) }

func (l *Logger) HasError() bool { return l.hasErr }

// Error 输出错误信息
//
// 如果 msg 包含了定位信息,则 filename 和 line 将被忽略
func (l *Logger) Error(msg any, filename string, line int) {
l.log(logs.Error, msg, filename, line)
l.log(logs.LevelError, msg, filename, line)
}

func (l *Logger) log(lv logs.Level, msg any, filename string, line int) {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (l *Logger) log(lv logs.Level, msg any, filename string, line int) {

l.count++ // 只有真正输出时,才需要+1。

if !l.hasErr && (lv == logs.Error || lv == logs.Fatal) {
if !l.hasErr && (lv == logs.LevelError || lv == logs.LevelFatal) {
l.hasErr = true
}

Expand Down
9 changes: 3 additions & 6 deletions cmd/web/restdoc/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import (
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/web/logs"
"github.com/issue9/logs/v7"
"golang.org/x/mod/modfile"
)

func TestLogger(t *testing.T) {
a := assert.New(t, false)

buf := new(bytes.Buffer)
ll, err := logs.New(nil, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTextHandler(buf),
})
a.NotError(err).NotNil(ll)
ll := logs.New(logs.NewTextHandler(buf), logs.WithLevels(logs.AllLevels()...))
a.NotNil(ll)
l := New(ll)
a.NotNil(l).Zero(l.Count())

Expand Down
9 changes: 3 additions & 6 deletions cmd/web/restdoc/logger/loggertest/loggertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/issue9/assert/v3"
"github.com/issue9/web/logs"
"github.com/issue9/logs/v7"

"github.com/issue9/web/cmd/web/restdoc/logger"
)
Expand Down Expand Up @@ -46,11 +46,8 @@ func New(a *assert.Assertion) *Tester {
Records: make(map[logs.Level][]string, 10),
}

ll, err := logs.New(nil, &logs.Options{
Levels: logs.AllLevels(),
Handler: &handler{t: t},
})
a.NotError(err).NotNil(ll)
ll := logs.New(&handler{t: t}, logs.WithLevels(logs.AllLevels()...))
a.NotNil(ll)

t.Logger = logger.New(ll)

Expand Down
9 changes: 4 additions & 5 deletions cmd/web/restdoc/logger/loggertest/loggertest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/logs/v7"
"github.com/issue9/web"

"github.com/issue9/web/logs"
)

func TestTester(t *testing.T) {
Expand All @@ -20,7 +19,7 @@ func TestTester(t *testing.T) {
lt.Warning(web.Phrase("aaa"))
lt.Error(errors.New("text string"), "", 0)

a.Length(lt.Records[logs.Warn], 1).
Length(lt.Records[logs.Error], 1).
Length(lt.Records[logs.Info], 0)
a.Length(lt.Records[logs.LevelWarn], 1).
Length(lt.Records[logs.LevelError], 1).
Length(lt.Records[logs.LevelInfo], 0)
}
8 changes: 4 additions & 4 deletions cmd/web/restdoc/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/getkin/kin-openapi/openapi3"
"github.com/issue9/assert/v3"
"github.com/issue9/web/logs"
"github.com/issue9/logs/v7"

"github.com/issue9/web/cmd/web/restdoc/logger/loggertest"
)
Expand All @@ -21,9 +21,9 @@ func TestParser_Parse(t *testing.T) {
p.AddDir(context.Background(), "./testdata", true)
d := p.Parse(context.Background())
a.NotNil(d).
Length(l.Records[logs.Error], 0).
Length(l.Records[logs.Warn], 0).
Length(l.Records[logs.Info], 0)
Length(l.Records[logs.LevelError], 0).
Length(l.Records[logs.LevelWarn], 0).
Length(l.Records[logs.LevelInfo], 0)

a.NotNil(d.Doc().Info).
Equal(d.Doc().Info.Version, "1.0.0")
Expand Down
4 changes: 2 additions & 2 deletions cmd/web/restdoc/parser/restdoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/issue9/assert/v3"
"github.com/issue9/web/logs"
"github.com/issue9/logs/v7"

"github.com/issue9/web/cmd/web/restdoc/logger/loggertest"
"github.com/issue9/web/cmd/web/restdoc/openapi"
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestRESTDoc_parseRESTDoc(t *testing.T) {
a.Equal(1, l.Count()).
Length(d.Doc().Tags, 1).
Equal(d.Doc().Info.Description, "# markdown desc\nline 2").
Contains(l.Records[logs.Error][0], "example.go:8")
Contains(l.Records[logs.LevelError][0], "example.go:8")
}

func TestBuildContact(t *testing.T) {
Expand Down
19 changes: 7 additions & 12 deletions cmd/web/termlog/termlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ import (
"io"

"github.com/issue9/localeutil"
"github.com/issue9/web/logs"
"github.com/issue9/logs/v7"
)

// New 声明用于终端输出的日志
//
// 如果初始化时出错,则会直接 Panic。
func New(p *localeutil.Printer, out io.Writer) logs.Logs {
log, err := logs.New(p, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTermHandler(out, nil),
Created: logs.NanoLayout,
})
if err != nil {
panic(err)
}

return log
func New(p *localeutil.Printer, out io.Writer) *logs.Logs {
return logs.New(
logs.NewTermHandler(out, nil),
logs.WithLevels(logs.AllLevels()...),
logs.WithCreated(logs.NanoLayout),
)
}
Loading

0 comments on commit 3ddda6a

Please sign in to comment.