github.com/metaworking/channeld@v0.7.3/pkg/channeld/logging.go (about)

     1  package channeld
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"time"
     7  
     8  	"go.uber.org/zap"
     9  	"go.uber.org/zap/zapcore"
    10  )
    11  
    12  type LogLevel zapcore.Level
    13  
    14  type Logger struct {
    15  	*zap.Logger
    16  }
    17  
    18  var rootLogger *Logger //*zap.Logger
    19  var zapConfig zap.Config
    20  var securityLogger *Logger
    21  
    22  func RootLogger() *Logger {
    23  	return rootLogger
    24  }
    25  
    26  const VerboseLevel LogLevel = -2
    27  const VeryVerboseLevel LogLevel = -3
    28  const TraceLevel LogLevel = -4
    29  
    30  /*
    31  func (l LogLevel) String() string {
    32  	if l == TraceLevel {
    33  		return "trace"
    34  	} else {
    35  		return zapcore.Level(l).String()
    36  	}
    37  }
    38  */
    39  
    40  func (logger *Logger) Verbose(msg string, fields ...zap.Field) {
    41  	if logger == nil {
    42  		return
    43  	}
    44  	if ce := logger.Check(zapcore.Level(VerboseLevel), msg); ce != nil {
    45  		ce.Write(fields...)
    46  	}
    47  }
    48  
    49  func (logger *Logger) VeryVerbose(msg string, fields ...zap.Field) {
    50  	if logger == nil {
    51  		return
    52  	}
    53  	if ce := logger.Check(zapcore.Level(VeryVerboseLevel), msg); ce != nil {
    54  		ce.Write(fields...)
    55  	}
    56  }
    57  
    58  func (logger *Logger) Trace(msg string, fields ...zap.Field) {
    59  	if logger == nil {
    60  		return
    61  	}
    62  	if ce := logger.Check(zapcore.Level(TraceLevel), msg); ce != nil {
    63  		ce.Write(fields...)
    64  	}
    65  }
    66  
    67  func InitLogs() {
    68  	if rootLogger != nil {
    69  		return
    70  	}
    71  
    72  	if GlobalSettings.Development {
    73  		zapConfig = zap.NewDevelopmentConfig()
    74  	} else {
    75  		zapConfig = zap.NewProductionConfig()
    76  	}
    77  	if GlobalSettings.LogLevel.HasValue {
    78  		zapConfig.Level = zap.NewAtomicLevelAt(zapcore.Level(GlobalSettings.LogLevel.Value))
    79  	}
    80  	if GlobalSettings.LogFile.HasValue {
    81  		zapConfig.OutputPaths = append(zapConfig.OutputPaths, strings.ReplaceAll(GlobalSettings.LogFile.Value, "{time}", time.Now().Format("20060102150405")))
    82  	}
    83  
    84  	zapLogger, err := zapConfig.Build()
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	rootLogger = &Logger{zapLogger}
    89  
    90  	zapConfig.OutputPaths = append(zapConfig.OutputPaths, filepath.Dir(GlobalSettings.LogFile.Value)+"/security.log")
    91  	zapLogger, _ = zapConfig.Build()
    92  	securityLogger = &Logger{zapLogger}
    93  
    94  	zap.Hooks(func(e zapcore.Entry) error {
    95  		if e.Level >= zapcore.WarnLevel {
    96  			logNum.WithLabelValues(e.Level.String()).Inc()
    97  		}
    98  		return nil
    99  	})
   100  
   101  	// defer rootLogger.Sync()
   102  }
   103  
   104  func SetLogLevel(level zapcore.Level) {
   105  	// atomicLevel.SetLevel(level)
   106  	zapConfig.Level.SetLevel(level)
   107  }