github.com/decred/dcrlnd@v0.7.6/build/log_shutdown.go (about)

     1  package build
     2  
     3  import (
     4  	"github.com/decred/slog"
     5  )
     6  
     7  // ShutdownLogger wraps an existing logger with a shutdown function which will
     8  // be called on Critical/Criticalf to prompt shutdown.
     9  type ShutdownLogger struct {
    10  	slog.Logger
    11  	shutdown func()
    12  }
    13  
    14  // NewShutdownLogger creates a shutdown logger for the log provided which will
    15  // use the signal package to request shutdown on critical errors.
    16  func NewShutdownLogger(logger slog.Logger, shutdown func()) *ShutdownLogger {
    17  	return &ShutdownLogger{
    18  		Logger:   logger,
    19  		shutdown: shutdown,
    20  	}
    21  }
    22  
    23  // Criticalf formats message according to format specifier and writes to
    24  // log with LevelCritical. It will then call the shutdown logger's shutdown
    25  // function to prompt safe shutdown.
    26  //
    27  // Note: it is part of the slog.Logger interface.
    28  func (s *ShutdownLogger) Criticalf(format string, params ...interface{}) {
    29  	s.Logger.Criticalf(format, params...)
    30  	s.Logger.Info("Sending request for shutdown")
    31  	s.shutdown()
    32  }
    33  
    34  // Critical formats message using the default formats for its operands
    35  // and writes to log with LevelCritical. It will then call the shutdown
    36  // logger's shutdown function to prompt safe shutdown.
    37  //
    38  // Note: it is part of the slog.Logger interface.
    39  func (s *ShutdownLogger) Critical(v ...interface{}) {
    40  	s.Logger.Critical(v)
    41  	s.Logger.Info("Sending request for shutdown")
    42  	s.shutdown()
    43  }