logger.go 786 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package logger
  5. import (
  6. "sync"
  7. log_prefixed "github.com/chappjc/logrus-prefix"
  8. "github.com/sirupsen/logrus"
  9. )
  10. var (
  11. globalLogger *logrus.Logger
  12. getLoggerMutex sync.Mutex
  13. )
  14. // GetLogger returns a configured logger instance
  15. func GetLogger(prefix string) *logrus.Entry {
  16. if prefix == "" {
  17. prefix = "<no prefix>"
  18. }
  19. if globalLogger == nil {
  20. getLoggerMutex.Lock()
  21. defer getLoggerMutex.Unlock()
  22. logger := logrus.New()
  23. logger.SetFormatter(&log_prefixed.TextFormatter{
  24. FullTimestamp: true,
  25. })
  26. globalLogger = logger
  27. }
  28. return globalLogger.WithField("prefix", prefix)
  29. }