Skip to content

Commit

Permalink
Feat: added Shutdown method to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Mar 25, 2024
1 parent 37ee252 commit 9c1add1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
9 changes: 5 additions & 4 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ type Logger interface {
// ParentLogger returns the parent of this Logger (or nil if it is the root).
ParentLogger() Logger

// UnsubscribeFromParentLogger unsubscribes the logger from its parent logger (e.g. updates about the log level).
// It is important to call this method whenever we remove all references to the logger, otherwise the logger will
// not be garbage collected.
UnsubscribeFromParentLogger()
// Shutdown shuts down the logger by either unsubscribing from its parent logger or shutting down the root logger.
//
// Note: It is important to call this method whenever we remove all references to a child logger, otherwise the
// logger will not be garbage collected until the root logger is garbage collected.
Shutdown()
}

// NewLogger creates a new logger with the given options.
Expand Down
13 changes: 11 additions & 2 deletions log/logger_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,18 @@ func (l *logger) ParentLogger() Logger {
// UnsubscribeFromParentLogger unsubscribes the logger from its parent logger (e.g. updates about the log level).
// It is important to call this method whenever we remove all references to the logger, otherwise the logger will
// not be garbage collected.
func (l *logger) UnsubscribeFromParentLogger() {
if l != nil && l.unsubscribeFromParent != nil {
func (l *logger) Shutdown() {
if l == nil {
return
}

switch isChildLogger := l.parentLogger != nil; isChildLogger {
case true:
l.unsubscribeFromParent()
case false:
if asyncTextHandler, isAsyncTextHandler := l.rootLogger.Handler().(*textHandler); isAsyncTextHandler {
asyncTextHandler.ioWorker.Shutdown().PendingTasksCounter.WaitIsZero()
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestLogger(t *testing.T) {
logger.LogTrace("created chain")

networkLogger := logger.NewChildLogger("network")
defer networkLogger.UnsubscribeFromParentLogger()
defer networkLogger.Shutdown()
networkLogger.SetLogLevel(log.LevelInfo)
networkLogger.LogInfo("instantiated chain (invisible)", "id", 1)

chainLogger := logger.NewChildLogger("chain1")
defer chainLogger.UnsubscribeFromParentLogger()
defer chainLogger.Shutdown()
chainLogger.SetLogLevel(log.LevelDebug)
chainLogger.LogDebug("attested weight updated (visible)", "oldWeight", 7, "newWeight", 10)
logger.LogTrace("shutdown")
Expand Down
2 changes: 1 addition & 1 deletion log/text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewTextHandler(options *Options) slog.Handler {
t := &textHandler{
output: options.Output,
timeFormat: options.TimeFormat,
ioWorker: workerpool.New("log.TextHandler", workerpool.WithWorkerCount(1)),
ioWorker: workerpool.New("log.TextHandler", workerpool.WithWorkerCount(1)).Start(),
}

formatString := "%s\t%-7s\t%s\t%s %s\n"
Expand Down

0 comments on commit 9c1add1

Please sign in to comment.