Skip to content

Commit 58d2031

Browse files
committed
chore: make several improvements for logger
1 parent 4db46da commit 58d2031

8 files changed

+54
-34
lines changed

acceptor_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (svr *server) acceptNewConnection(_ netpoll.IOEvent) error {
4545
netAddr := socket.SockaddrToTCPOrUnixAddr(sa)
4646
if svr.opts.TCPKeepAlive > 0 && svr.ln.network == "tcp" {
4747
err = socket.SetKeepAlive(nfd, int(svr.opts.TCPKeepAlive/time.Second))
48-
logging.LogErr(err)
48+
logging.Error(err)
4949
}
5050

5151
el := svr.lb.next(netAddr)
@@ -79,7 +79,7 @@ func (el *eventloop) loopAccept(_ netpoll.IOEvent) error {
7979
netAddr := socket.SockaddrToTCPOrUnixAddr(sa)
8080
if el.svr.opts.TCPKeepAlive > 0 && el.svr.ln.network == "tcp" {
8181
err = socket.SetKeepAlive(nfd, int(el.svr.opts.TCPKeepAlive/time.Second))
82-
logging.LogErr(err)
82+
logging.Error(err)
8383
}
8484

8585
c := newTCPConn(nfd, el, sa, el.svr.opts.Codec, el.ln.lnaddr, netAddr)

client.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,23 @@ func (cli *Client) Dial(network, address string) (Conn, error) {
143143
return nil, e
144144
}
145145

146-
opts := cli.el.svr.opts
147-
if opts.TCPNoDelay == TCPNoDelay {
146+
if cli.opts.TCPNoDelay == TCPNoDelay {
148147
if err = socket.SetNoDelay(DupFD, 1); err != nil {
149148
return nil, err
150149
}
151150
}
152-
if opts.TCPKeepAlive > 0 {
153-
if err = socket.SetKeepAlive(DupFD, int(opts.TCPKeepAlive/time.Second)); err != nil {
151+
if cli.opts.TCPKeepAlive > 0 {
152+
if err = socket.SetKeepAlive(DupFD, int(cli.opts.TCPKeepAlive/time.Second)); err != nil {
154153
return nil, err
155154
}
156155
}
157-
if opts.SocketSendBuffer > 0 {
158-
if err = socket.SetSendBuffer(DupFD, opts.SocketSendBuffer); err != nil {
156+
if cli.opts.SocketSendBuffer > 0 {
157+
if err = socket.SetSendBuffer(DupFD, cli.opts.SocketSendBuffer); err != nil {
159158
return nil, err
160159
}
161160
}
162-
if opts.SocketRecvBuffer > 0 {
163-
if err = socket.SetRecvBuffer(DupFD, opts.SocketRecvBuffer); err != nil {
161+
if cli.opts.SocketRecvBuffer > 0 {
162+
if err = socket.SetRecvBuffer(DupFD, cli.opts.SocketRecvBuffer); err != nil {
164163
return nil, err
165164
}
166165
}

client_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828

2929
"github.com/stretchr/testify/assert"
3030
"github.com/stretchr/testify/require"
31-
"go.uber.org/zap/zapcore"
3231

32+
"github.com/panjf2000/gnet/logging"
3333
"github.com/panjf2000/gnet/pool/goroutine"
3434
)
3535

@@ -265,7 +265,7 @@ func testCodecServeWithGnetClient(
265265
codec: codec, workerPool: goroutine.Default(),
266266
}
267267
ts.clientEV = &clientEvents{}
268-
ts.client, err = NewClient(ts.clientEV, WithLogLevel(zapcore.DebugLevel), WithCodec(codec))
268+
ts.client, err = NewClient(ts.clientEV, WithLogLevel(logging.DebugLevel), WithCodec(codec))
269269
assert.NoError(t, err)
270270
err = ts.client.Start()
271271
assert.NoError(t, err)
@@ -274,7 +274,7 @@ func testCodecServeWithGnetClient(
274274
network+"://"+addr,
275275
WithMulticore(multicore),
276276
WithTicker(true),
277-
WithLogLevel(zapcore.DebugLevel),
277+
WithLogLevel(logging.DebugLevel),
278278
WithSocketRecvBuffer(8*1024),
279279
WithSocketSendBuffer(8*1024),
280280
WithCodec(codec),

gnet_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/stretchr/testify/assert"
3131
"github.com/stretchr/testify/require"
3232
"go.uber.org/zap"
33-
"go.uber.org/zap/zapcore"
3433

3534
"github.com/panjf2000/gnet/errors"
3635
"github.com/panjf2000/gnet/logging"
@@ -259,7 +258,7 @@ func testCodecServe(
259258
network+"://"+addr,
260259
WithMulticore(multicore),
261260
WithTicker(true),
262-
WithLogLevel(zapcore.DebugLevel),
261+
WithLogLevel(logging.DebugLevel),
263262
WithTCPKeepAlive(
264263
time.Minute*5,
265264
),

listener_unix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ func (ln *listener) close() {
7070
ln.once.Do(
7171
func() {
7272
if ln.fd > 0 {
73-
logging.LogErr(os.NewSyscallError("close", unix.Close(ln.fd)))
73+
logging.Error(os.NewSyscallError("close", unix.Close(ln.fd)))
7474
}
7575
if ln.network == "unix" {
76-
logging.LogErr(os.RemoveAll(ln.addr))
76+
logging.Error(os.RemoveAll(ln.addr))
7777
}
7878
})
7979
}

listener_windows.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (ln *listener) dup() (int, string, error) {
3939
func (ln *listener) normalize() (err error) {
4040
switch ln.network {
4141
case "unix":
42-
logging.LogErr(os.RemoveAll(ln.addr))
42+
logging.Error(os.RemoveAll(ln.addr))
4343
fallthrough
4444
case "tcp", "tcp4", "tcp6":
4545
if ln.ln, err = net.Listen(ln.network, ln.addr); err != nil {
@@ -60,13 +60,13 @@ func (ln *listener) normalize() (err error) {
6060
func (ln *listener) close() {
6161
ln.once.Do(func() {
6262
if ln.ln != nil {
63-
logging.LogErr(ln.ln.Close())
63+
logging.Error(ln.ln.Close())
6464
}
6565
if ln.pconn != nil {
66-
logging.LogErr(ln.pconn.Close())
66+
logging.Error(ln.pconn.Close())
6767
}
6868
if ln.network == "unix" {
69-
logging.LogErr(os.RemoveAll(ln.addr))
69+
logging.Error(os.RemoveAll(ln.addr))
7070
}
7171
})
7272
}

logging/logger.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,30 @@ import (
5959
var (
6060
flushLogs func() error
6161
defaultLogger Logger
62-
defaultLoggingLevel zapcore.Level
62+
defaultLoggingLevel Level
63+
)
64+
65+
type Level = zapcore.Level
66+
67+
const (
68+
// DebugLevel logs are typically voluminous, and are usually disabled in
69+
// production.
70+
DebugLevel Level = iota - 1
71+
// InfoLevel is the default logging priority.
72+
InfoLevel
73+
// WarnLevel logs are more important than Info, but don't need individual
74+
// human review.
75+
WarnLevel
76+
// ErrorLevel logs are high-priority. If an application is running smoothly,
77+
// it shouldn't generate any error-level logs.
78+
ErrorLevel
79+
// DPanicLevel logs are particularly important errors. In development the
80+
// logger panics after writing the message.
81+
DPanicLevel
82+
// PanicLevel logs a message, then panics.
83+
PanicLevel
84+
// FatalLevel logs a message, then calls os.Exit(1).
85+
FatalLevel
6386
)
6487

6588
func init() {
@@ -69,7 +92,7 @@ func init() {
6992
if err != nil {
7093
panic("invalid GNET_LOGGING_LEVEL, " + err.Error())
7194
}
72-
defaultLoggingLevel = zapcore.Level(loggingLevel)
95+
defaultLoggingLevel = Level(loggingLevel)
7396
}
7497

7598
// Initializes the inside default logger of gnet.
@@ -83,14 +106,15 @@ func init() {
83106
} else {
84107
cfg := zap.NewDevelopmentConfig()
85108
cfg.Level = zap.NewAtomicLevelAt(defaultLoggingLevel)
109+
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
86110
zapLogger, _ := cfg.Build()
87111
defaultLogger = zapLogger.Sugar()
88112
}
89113
}
90114

91115
func getEncoder() zapcore.Encoder {
92116
encoderConfig := zap.NewProductionEncoderConfig()
93-
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
117+
encoderConfig.EncodeTime = zapcore.RFC3339NanoTimeEncoder
94118
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
95119
return zapcore.NewConsoleEncoder(encoderConfig)
96120
}
@@ -106,7 +130,7 @@ func LogLevel() string {
106130
}
107131

108132
// CreateLoggerAsLocalFile setups the logger by local file path.
109-
func CreateLoggerAsLocalFile(localFilePath string, logLevel zapcore.Level) (logger Logger, flush func() error, err error) {
133+
func CreateLoggerAsLocalFile(localFilePath string, logLevel Level) (logger Logger, flush func() error, err error) {
110134
if len(localFilePath) == 0 {
111135
return nil, nil, errors.New("invalid local logger path")
112136
}
@@ -115,15 +139,15 @@ func CreateLoggerAsLocalFile(localFilePath string, logLevel zapcore.Level) (logg
115139
lumberJackLogger := &lumberjack.Logger{
116140
Filename: localFilePath,
117141
MaxSize: 100, // megabytes
118-
MaxBackups: 3,
119-
MaxAge: 28, // days
142+
MaxBackups: 2,
143+
MaxAge: 15, // days
120144
}
121145

122146
encoder := getEncoder()
123147
ws := zapcore.AddSync(lumberJackLogger)
124148
zapcore.Lock(ws)
125149

126-
levelEnabler := zap.LevelEnablerFunc(func(level zapcore.Level) bool {
150+
levelEnabler := zap.LevelEnablerFunc(func(level Level) bool {
127151
return level >= logLevel
128152
})
129153
core := zapcore.NewCore(encoder, ws, levelEnabler)
@@ -140,8 +164,8 @@ func Cleanup() {
140164
}
141165
}
142166

143-
// LogErr prints err if it's not nil.
144-
func LogErr(err error) {
167+
// Error prints err if it's not nil.
168+
func Error(err error) {
145169
if err != nil {
146170
defaultLogger.Errorf("error occurs during runtime, %v", err)
147171
}

options.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package gnet
1717
import (
1818
"time"
1919

20-
"go.uber.org/zap/zapcore"
21-
2220
"github.com/panjf2000/gnet/logging"
2321
)
2422

@@ -110,7 +108,7 @@ type Options struct {
110108
LogPath string
111109

112110
// LogLevel indicates the logging level, it should be used along with LogPath.
113-
LogLevel zapcore.Level
111+
LogLevel logging.Level
114112

115113
// Logger is the customized logger for logging info, if it is not set,
116114
// then gnet will use the default logger powered by go.uber.org/zap.
@@ -223,7 +221,7 @@ func WithLogPath(fileName string) Option {
223221
}
224222

225223
// WithLogLevel is an option to set up the logging level.
226-
func WithLogLevel(lvl zapcore.Level) Option {
224+
func WithLogLevel(lvl logging.Level) Option {
227225
return func(opts *Options) {
228226
opts.LogLevel = lvl
229227
}

0 commit comments

Comments
 (0)