Skip to content

Commit 9bbc4c3

Browse files
lvan100lianghuan
authored andcommitted
refactor(log): transferred log to go-spring/log
1 parent 6bf5b2e commit 9bbc4c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+227
-5988
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
require (
66
github.com/expr-lang/expr v1.17.2
7+
github.com/go-spring/log v0.0.2
78
github.com/lvan100/go-assert v0.0.2
89
github.com/magiconair/properties v1.8.10
910
github.com/pelletier/go-toml v1.9.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/expr-lang/expr v1.17.2 h1:o0A99O/Px+/DTjEnQiodAgOIK9PPxL8DtXhBRKC+Iso
22
github.com/expr-lang/expr v1.17.2/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
33
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
44
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
5+
github.com/go-spring/log v0.0.2 h1:Apt5hjV5kBjm+jxYzYkLBMEBUt9x7xw/7zINljMENFo=
6+
github.com/go-spring/log v0.0.2/go.mod h1:aKAsCR5Fv4WyFBtNtHNZV4YmitRutJ1Wh9fRI/IiHZM=
57
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
68
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
79
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=

gs/app.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package gs
18+
19+
import (
20+
"context"
21+
22+
"github.com/go-spring/log"
23+
"github.com/go-spring/spring-core/gs/internal/gs_app"
24+
)
25+
26+
type AppStarter struct{}
27+
28+
// Run runs the app and waits for an interrupt signal to exit.
29+
func (s *AppStarter) Run() {
30+
s.RunWith(nil)
31+
}
32+
33+
// initApp initializes the app.
34+
func (s *AppStarter) initApp() error {
35+
printBanner()
36+
if err := initLog(); err != nil {
37+
return err
38+
}
39+
if err := B.(*gs_app.BootImpl).Run(); err != nil {
40+
return err
41+
}
42+
B = nil
43+
return nil
44+
}
45+
46+
// RunWith runs the app with a given function and waits for an interrupt signal to exit.
47+
func (s *AppStarter) RunWith(fn func(ctx context.Context) error) {
48+
var err error
49+
defer func() {
50+
if err != nil {
51+
log.Errorf(context.Background(), log.TagApp, "app run failed: %v", err)
52+
}
53+
}()
54+
if err = s.initApp(); err != nil {
55+
return
56+
}
57+
err = gs_app.GS.RunWith(fn)
58+
}
59+
60+
// RunAsync runs the app asynchronously and returns a function to stop the app.
61+
func (s *AppStarter) RunAsync() (func(), error) {
62+
if err := s.initApp(); err != nil {
63+
return nil, err
64+
}
65+
if err := gs_app.GS.Start(); err != nil {
66+
return nil, err
67+
}
68+
return func() { gs_app.GS.Stop() }, nil
69+
}

gs/banner.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2025 The Go-Spring Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package gs
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
)
23+
24+
var appBanner = `
25+
____ ___ ____ ____ ____ ___ _ _ ____
26+
/ ___| / _ \ / ___| | _ \ | _ \ |_ _| | \ | | / ___|
27+
| | _ | | | | _____ \___ \ | |_) | | |_) | | | | \| | | | _
28+
| |_| | | |_| | |_____| ___) | | __/ | _ < | | | |\ | | |_| |
29+
\____| \___/ |____/ |_| |_| \_\ |___| |_| \_| \____|
30+
`
31+
32+
// Banner sets a custom app banner.
33+
func Banner(banner string) {
34+
appBanner = banner
35+
}
36+
37+
// printBanner prints the app banner.
38+
func printBanner() {
39+
if len(appBanner) == 0 {
40+
return
41+
}
42+
43+
var sb strings.Builder
44+
if appBanner[0] != '\n' {
45+
sb.WriteString("\n")
46+
}
47+
48+
maxLength := 0
49+
for s := range strings.SplitSeq(appBanner, "\n") {
50+
sb.WriteString("\x1b[36m")
51+
sb.WriteString(s)
52+
sb.WriteString("\x1b[0m\n")
53+
if len(s) > maxLength {
54+
maxLength = len(s)
55+
}
56+
}
57+
58+
if appBanner[len(appBanner)-1] != '\n' {
59+
sb.WriteString("\n")
60+
}
61+
62+
var padding []byte
63+
if n := (maxLength - len(Version)) / 2; n > 0 {
64+
padding = make([]byte, n)
65+
for i := range padding {
66+
padding[i] = ' '
67+
}
68+
}
69+
sb.WriteString(string(padding))
70+
sb.WriteString(Version)
71+
fmt.Println(sb.String())
72+
}

gs/gs.go

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package gs
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"reflect"
23-
"strings"
2422

23+
"github.com/go-spring/log"
2524
"github.com/go-spring/spring-core/conf"
2625
"github.com/go-spring/spring-core/gs/internal/gs"
2726
"github.com/go-spring/spring-core/gs/internal/gs_app"
@@ -30,7 +29,6 @@ import (
3029
"github.com/go-spring/spring-core/gs/internal/gs_cond"
3130
"github.com/go-spring/spring-core/gs/internal/gs_conf"
3231
"github.com/go-spring/spring-core/gs/internal/gs_dync"
33-
"github.com/go-spring/spring-core/log"
3432
)
3533

3634
const (
@@ -175,7 +173,7 @@ func BeanSelectorFor[T any](name ...string) BeanSelector {
175173
// Property sets a system property.
176174
func Property(key string, val string) {
177175
if err := gs_conf.SysConf.Set(key, val); err != nil {
178-
log.Errorf(context.Background(), log.TagGS, "failed to set property key=%s, err=%v", key, err)
176+
log.Errorf(context.Background(), log.TagApp, "failed to set property key=%s, err=%v", key, err)
179177
}
180178
}
181179

@@ -212,48 +210,12 @@ func FuncJob(fn func(ctx context.Context) error) *RegisteredBean {
212210
return Object(funcJob(fn)).AsJob().Caller(1)
213211
}
214212

215-
type AppStarter struct{}
216-
217213
// Web enables or disables the built-in web server.
218214
func Web(enable bool) *AppStarter {
219215
EnableSimpleHttpServer(enable)
220216
return &AppStarter{}
221217
}
222218

223-
// Run runs the app and waits for an interrupt signal to exit.
224-
func (s *AppStarter) Run() {
225-
s.RunWith(nil)
226-
}
227-
228-
// RunWith runs the app with a given function and waits for an interrupt signal to exit.
229-
func (s *AppStarter) RunWith(fn func(ctx context.Context) error) {
230-
var err error
231-
defer func() {
232-
if err != nil {
233-
log.Errorf(context.Background(), log.TagGS, "app run failed: %v", err)
234-
}
235-
}()
236-
printBanner()
237-
if err = B.(*gs_app.BootImpl).Run(); err != nil {
238-
return
239-
}
240-
B = nil
241-
err = gs_app.GS.RunWith(fn)
242-
}
243-
244-
// RunAsync runs the app asynchronously and returns a function to stop the app.
245-
func (s *AppStarter) RunAsync() (func(), error) {
246-
printBanner()
247-
if err := B.(*gs_app.BootImpl).Run(); err != nil {
248-
return nil, err
249-
}
250-
B = nil
251-
if err := gs_app.GS.Start(); err != nil {
252-
return nil, err
253-
}
254-
return func() { gs_app.GS.Stop() }, nil
255-
}
256-
257219
// Run runs the app and waits for an interrupt signal to exit.
258220
func Run() {
259221
new(AppStarter).Run()
@@ -321,50 +283,3 @@ func RefreshProperties() error {
321283
}
322284
return gs_app.GS.C.RefreshProperties(p)
323285
}
324-
325-
/********************************** banner ***********************************/
326-
327-
var appBanner = `
328-
____ ___ ____ ____ ____ ___ _ _ ____
329-
/ ___| / _ \ / ___| | _ \ | _ \ |_ _| | \ | | / ___|
330-
| | _ | | | | _____ \___ \ | |_) | | |_) | | | | \| | | | _
331-
| |_| | | |_| | |_____| ___) | | __/ | _ < | | | |\ | | |_| |
332-
\____| \___/ |____/ |_| |_| \_\ |___| |_| \_| \____|
333-
`
334-
335-
// Banner sets a custom app banner.
336-
func Banner(banner string) {
337-
appBanner = banner
338-
}
339-
340-
// printBanner prints the app banner.
341-
func printBanner() {
342-
if len(appBanner) == 0 {
343-
return
344-
}
345-
346-
if appBanner[0] != '\n' {
347-
fmt.Println()
348-
}
349-
350-
maxLength := 0
351-
for s := range strings.SplitSeq(appBanner, "\n") {
352-
fmt.Printf("\x1b[36m%s\x1b[0m\n", s) // CYAN
353-
if len(s) > maxLength {
354-
maxLength = len(s)
355-
}
356-
}
357-
358-
if appBanner[len(appBanner)-1] != '\n' {
359-
fmt.Println()
360-
}
361-
362-
var padding []byte
363-
if n := (maxLength - len(Version)) / 2; n > 0 {
364-
padding = make([]byte, n)
365-
for i := range padding {
366-
padding[i] = ' '
367-
}
368-
}
369-
fmt.Println(string(padding) + Version + "\n")
370-
}

gs/internal/gs_app/app.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727
"syscall"
2828
"time"
2929

30+
"github.com/go-spring/log"
3031
"github.com/go-spring/spring-core/conf"
3132
"github.com/go-spring/spring-core/gs/internal/gs"
3233
"github.com/go-spring/spring-core/gs/internal/gs_conf"
3334
"github.com/go-spring/spring-core/gs/internal/gs_core"
34-
"github.com/go-spring/spring-core/log"
3535
"github.com/go-spring/spring-core/util/goutil"
3636
)
3737

@@ -97,7 +97,7 @@ func (app *App) RunWith(fn func(ctx context.Context) error) error {
9797
ch := make(chan os.Signal, 1)
9898
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
9999
sig := <-ch
100-
log.Infof(context.Background(), log.TagGS, "Received signal: %v", sig)
100+
log.Infof(context.Background(), log.TagApp, "Received signal: %v", sig)
101101
app.ShutDown()
102102
}()
103103

@@ -145,7 +145,7 @@ func (app *App) Start() error {
145145
}
146146
}()
147147
if err := job.Run(app.ctx); err != nil {
148-
log.Errorf(context.Background(), log.TagGS, "job run error: %v", err)
148+
log.Errorf(context.Background(), log.TagApp, "job run error: %v", err)
149149
app.ShutDown()
150150
}
151151
})
@@ -169,7 +169,7 @@ func (app *App) Start() error {
169169
}()
170170
err := svr.ListenAndServe(sig)
171171
if err != nil && !errors.Is(err, http.ErrServerClosed) {
172-
log.Errorf(context.Background(), log.TagGS, "server serve error: %v", err)
172+
log.Errorf(context.Background(), log.TagApp, "server serve error: %v", err)
173173
sig.Intercept()
174174
app.ShutDown()
175175
}
@@ -179,7 +179,7 @@ func (app *App) Start() error {
179179
if sig.Intercepted() {
180180
return nil
181181
}
182-
log.Infof(context.Background(), log.TagGS, "ready to serve requests")
182+
log.Infof(context.Background(), log.TagApp, "ready to serve requests")
183183
sig.Close()
184184
}
185185
return nil
@@ -196,7 +196,7 @@ func (app *App) Stop() {
196196
for _, svr := range app.Servers {
197197
goutil.GoFunc(func() {
198198
if err := svr.Shutdown(ctx); err != nil {
199-
log.Errorf(context.Background(), log.TagGS, "shutdown server failed: %v", err)
199+
log.Errorf(context.Background(), log.TagApp, "shutdown server failed: %v", err)
200200
}
201201
})
202202
}
@@ -207,9 +207,9 @@ func (app *App) Stop() {
207207

208208
select {
209209
case <-waitChan:
210-
log.Infof(context.Background(), log.TagGS, "shutdown complete")
210+
log.Infof(context.Background(), log.TagApp, "shutdown complete")
211211
case <-ctx.Done():
212-
log.Infof(context.Background(), log.TagGS, "shutdown timeout")
212+
log.Infof(context.Background(), log.TagApp, "shutdown timeout")
213213
}
214214
}
215215

gs/internal/gs_app/app_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626
"testing"
2727
"time"
2828

29+
"github.com/go-spring/log"
2930
"github.com/go-spring/spring-core/conf"
3031
"github.com/go-spring/spring-core/gs/internal/gs"
3132
"github.com/go-spring/spring-core/gs/internal/gs_conf"
32-
"github.com/go-spring/spring-core/log"
3333
"github.com/go-spring/spring-core/util/goutil"
3434
"github.com/lvan100/go-assert"
3535
"go.uber.org/mock/gomock"
@@ -39,7 +39,7 @@ var logBuf = &bytes.Buffer{}
3939

4040
func init() {
4141
goutil.OnPanic = func(ctx context.Context, r any) {
42-
log.Panicf(ctx, log.TagGS, "panic: %v\n%s\n", r, debug.Stack())
42+
log.Panicf(ctx, log.TagDef, "panic: %v\n%s\n", r, debug.Stack())
4343
}
4444
}
4545

0 commit comments

Comments
 (0)