mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 08:02:25 +02:00
build: move log rotator code
This commit is contained in:
53
log.go
53
log.go
@@ -2,10 +2,6 @@ package lnd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/btcsuite/btcd/connmgr"
|
||||
"github.com/btcsuite/btclog"
|
||||
@@ -173,55 +169,6 @@ var subsystemLoggers = map[string]btclog.Logger{
|
||||
"PRNF": prnfLog,
|
||||
}
|
||||
|
||||
// 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, 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, int64(MaxLogFileSize*1024), false, MaxLogFiles)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pr, pw := io.Pipe()
|
||||
go r.Run(pr)
|
||||
|
||||
logWriter.RotatorPipe = pw
|
||||
logRotator = r
|
||||
}
|
||||
|
||||
// setLogLevel sets the logging level for provided subsystem. Invalid
|
||||
// subsystems are ignored. Uninitialized subsystems are dynamically created as
|
||||
// needed.
|
||||
func setLogLevel(subsystemID string, logLevel string) {
|
||||
// Ignore invalid subsystems.
|
||||
logger, ok := subsystemLoggers[subsystemID]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Defaults to info if the log level is invalid.
|
||||
level, _ := btclog.LevelFromString(logLevel)
|
||||
logger.SetLevel(level)
|
||||
}
|
||||
|
||||
// setLogLevels sets the log level for all subsystem loggers to the passed
|
||||
// level. It also dynamically creates the subsystem loggers as needed, so it
|
||||
// can be used to initialize the logging system.
|
||||
func setLogLevels(logLevel string) {
|
||||
// Configure all sub-systems with the new logging level. Dynamically
|
||||
// create loggers as needed.
|
||||
for subsystemID := range subsystemLoggers {
|
||||
setLogLevel(subsystemID, logLevel)
|
||||
}
|
||||
}
|
||||
|
||||
// logClosure is used to provide a closure over expensive logging operations so
|
||||
// don't have to be performed when the logging level doesn't warrant it.
|
||||
type logClosure func() string
|
||||
|
Reference in New Issue
Block a user