config: make log rotation configurable

This commit is contained in:
vegardengen 2018-04-07 00:11:42 +02:00 committed by Olaoluwa Osuntokun
parent 80d57f3ddf
commit 5eed171187
3 changed files with 32 additions and 21 deletions

View File

@ -46,6 +46,8 @@ const (
defaultMaxPendingChannels = 1
defaultNoEncryptWallet = false
defaultTrickleDelay = 30 * 1000
defaultMaxLogFiles = 3
defaultMaxLogFileSize = 10
defaultBroadcastDelta = 10
@ -157,7 +159,8 @@ type config struct {
ReadMacPath string `long:"readonlymacaroonpath" description:"Path to write the read-only macaroon for lnd's RPC and REST services if it doesn't exist"`
InvoiceMacPath string `long:"invoicemacaroonpath" description:"Path to the invoice-only macaroon for lnd's RPC and REST services if it doesn't exist"`
LogDir string `long:"logdir" description:"Directory to log output."`
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
MaxLogFileSize int `long:"maxlogfilesize" description:"Maximum logfile size in MB"`
RPCListeners []string `long:"rpclisten" description:"Add an interface/port to listen for RPC connections"`
RESTListeners []string `long:"restlisten" description:"Add an interface/port to listen for REST connections"`
Listeners []string `long:"listen" description:"Add an interface/port to listen for peer connections"`
@ -222,6 +225,8 @@ func loadConfig() (*config, error) {
InvoiceMacPath: defaultInvoiceMacPath,
ReadMacPath: defaultReadMacPath,
LogDir: defaultLogDir,
MaxLogFiles: defaultMaxLogFiles,
MaxLogFileSize: defaultMaxLogFileSize,
Bitcoin: &chainConfig{
MinHTLC: defaultBitcoinMinHTLCMSat,
BaseFee: defaultBitcoinBaseFeeMSat,
@ -662,7 +667,7 @@ func loadConfig() (*config, error) {
normalizeNetwork(activeNetParams.Name))
// Initialize logging at the default logging level.
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename), cfg.MaxLogFileSize, cfg.MaxLogFiles)
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {

4
log.go
View File

@ -116,14 +116,14 @@ var subsystemLoggers = map[string]btclog.Logger{
// initLogRotator initializes the logging rotator to write logs to logFile and
// create roll files in the same directory. It must be called before the
// package-global log rotator variables are used.
func initLogRotator(logFile string) {
func initLogRotator(logFile string, MaxLogFileSize int, MaxLogFiles int) {
logDir, _ := filepath.Split(logFile)
err := os.MkdirAll(logDir, 0700)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
os.Exit(1)
}
r, err := rotator.New(logFile, 10*1024, false, 3)
r, err := rotator.New(logFile, int64(MaxLogFileSize*1024), false, MaxLogFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
os.Exit(1)

View File

@ -12,6 +12,12 @@
; Rotated logs are compressed in place.
; logdir=~/.lnd/logs
; Number of logfiles that the log rotation should keep. Setting it to 0 disables deletion of old log files.
; maxlogfiles=3
;
; Max log file size in MB before it is rotated.
; maxlogfilesize=10
; Path to TLS certificate for lnd's RPC and REST services.
; tlscertpath=~/.lnd/tls.cert