coredhcp/logger/logger.go
Andrea Barberio db61ab1cea Replace use of deprecated io/ioutil
All the calls have been converted to their new equivalent.

Signed-off-by: Andrea Barberio <insomniac@slackware.it>
2023-10-18 10:42:47 +02:00

47 lines
1.1 KiB
Go

// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package logger
import (
"io"
"sync"
log_prefixed "github.com/chappjc/logrus-prefix"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
var (
globalLogger *logrus.Logger
getLoggerMutex sync.Mutex
)
// GetLogger returns a configured logger instance
func GetLogger(prefix string) *logrus.Entry {
if prefix == "" {
prefix = "<no prefix>"
}
if globalLogger == nil {
getLoggerMutex.Lock()
defer getLoggerMutex.Unlock()
logger := logrus.New()
logger.SetFormatter(&log_prefixed.TextFormatter{
FullTimestamp: true,
})
globalLogger = logger
}
return globalLogger.WithField("prefix", prefix)
}
// WithFile logs to the specified file in addition to the existing output.
func WithFile(log *logrus.Entry, logfile string) {
log.Logger.AddHook(lfshook.NewHook(logfile, &logrus.TextFormatter{}))
}
// WithNoStdOutErr disables logging to stdout/stderr.
func WithNoStdOutErr(log *logrus.Entry) {
log.Logger.SetOutput(io.Discard)
}