Skip to content

Commit

Permalink
bug & feat: replace the global default logger with custom logger
Browse files Browse the repository at this point in the history
Fixes #467
  • Loading branch information
panjf2000 committed May 18, 2023
1 parent d67fe1f commit 3c66bce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
26 changes: 12 additions & 14 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,28 @@ import (

// Client of gnet.
type Client struct {
opts *Options
el *eventloop
logFlush func() error
opts *Options
el *eventloop
}

// NewClient creates an instance of Client.
func NewClient(eh EventHandler, opts ...Option) (cli *Client, err error) {
options := loadOptions(opts...)
cli = new(Client)
cli.opts = options
var logger logging.Logger
if options.LogPath != "" {
if logger, cli.logFlush, err = logging.CreateLoggerAsLocalFile(options.LogPath, options.LogLevel); err != nil {
return
}
} else {
logger = logging.GetDefaultLogger()
}

logger, logFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher()
if options.Logger == nil {
if options.LogPath != "" {
logger, logFlusher, _ = logging.CreateLoggerAsLocalFile(options.LogPath, options.LogLevel)
}
options.Logger = logger
} else {
logger = options.Logger
logFlusher = nil
}
logging.SetDefaultLoggerAndFlusher(logger, logFlusher)

var p *netpoll.Poller
if p, err = netpoll.OpenPoller(); err != nil {
return
Expand Down Expand Up @@ -130,9 +131,6 @@ func (cli *Client) Stop() (err error) {
_ = cli.el.engine.workerPool.Wait()
logging.Error(cli.el.poller.Close())
cli.el.eventHandler.OnShutdown(Engine{})
if cli.logFlush != nil {
err = cli.logFlush()
}
logging.Cleanup()
return
}
Expand Down
31 changes: 12 additions & 19 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,28 +429,21 @@ var MaxStreamBufferCap = 64 * 1024 // 64KB
func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error) {
options := loadOptions(opts...)

logging.Debugf("default logging level is %s", logging.LogLevel())

var (
logger logging.Logger
flush func() error
)
if options.LogPath != "" {
if logger, flush, err = logging.CreateLoggerAsLocalFile(options.LogPath, options.LogLevel); err != nil {
return
}
} else {
logger = logging.GetDefaultLogger()
}
logger, logFlusher := logging.GetDefaultLogger(), logging.GetDefaultFlusher()
if options.Logger == nil {
if options.LogPath != "" {
logger, logFlusher, _ = logging.CreateLoggerAsLocalFile(options.LogPath, options.LogLevel)
}
options.Logger = logger
} else {
logger = options.Logger
logFlusher = nil
}
defer func() {
if flush != nil {
_ = flush()
}
logging.Cleanup()
}()
logging.SetDefaultLoggerAndFlusher(logger, logFlusher)

defer logging.Cleanup()

logging.Debugf("default logging level is %s", logging.LogLevel())

// The maximum number of operating system threads that the Go program can use is initially set to 10000,
// which should also be the maximum amount of I/O event-loops locked to OS threads that users can start up.
Expand Down
29 changes: 25 additions & 4 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ import (
"errors"
"os"
"strconv"
"sync"

"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

type Flusher = func() error

var (
flushLogs func() error
defaultLogger Logger
defaultLoggingLevel Level
defaultFlusher Flusher
)

// Level is the alias of zapcore.Level.
Expand Down Expand Up @@ -101,7 +104,7 @@ func init() {
fileName := os.Getenv("GNET_LOGGING_FILE")
if len(fileName) > 0 {
var err error
defaultLogger, flushLogs, err = CreateLoggerAsLocalFile(fileName, defaultLoggingLevel)
defaultLogger, defaultFlusher, err = CreateLoggerAsLocalFile(fileName, defaultLoggingLevel)
if err != nil {
panic("invalid GNET_LOGGING_FILE, " + err.Error())
}
Expand Down Expand Up @@ -169,6 +172,24 @@ func GetDefaultLogger() Logger {
return defaultLogger
}

// GetDefaultFlusher returns the default flusher.
func GetDefaultFlusher() Flusher {
return defaultFlusher
}

var setupOnce sync.Once

// SetDefaultLoggerAndFlusher sets the default logger and its flusher.
//
// Note that this function should only be called once at the
// start of the program and not thereafter for the entire runtime,
// otherwise it will only keep the first setup.
func SetDefaultLoggerAndFlusher(logger Logger, flusher Flusher) {
setupOnce.Do(func() {
defaultLogger, defaultFlusher = logger, flusher
})
}

// LogLevel tells what the default logging level is.
func LogLevel() string {
return defaultLoggingLevel.String()
Expand Down Expand Up @@ -204,8 +225,8 @@ func CreateLoggerAsLocalFile(localFilePath string, logLevel Level) (logger Logge

// Cleanup does something windup for logger, like closing, flushing, etc.
func Cleanup() {
if flushLogs != nil {
_ = flushLogs()
if defaultFlusher != nil {
_ = defaultFlusher()
}
}

Expand Down

0 comments on commit 3c66bce

Please sign in to comment.