github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/log/log.go (about) 1 package log 2 3 const ( 4 logLevelNameInfo = "info" 5 logLevelNameDebug = "debug" 6 logLevelNameWarn = "warn" 7 logLevelNameError = "error" 8 ) 9 10 // Logger represents a contract for implementing a logging module 11 type Logger interface { 12 Infof(msg string, args ...any) 13 Warnf(msg string, args ...any) 14 Errorf(msg string, args ...any) 15 Debugf(msg string, args ...any) 16 Fatalf(msg string, args ...any) 17 18 With(args map[string]any) Logger 19 } 20 21 // Global logger instance 22 var globalLogger Logger 23 24 func init() { 25 globalLogger = NewNopLogger() 26 } 27 28 // SetGlobal sets the global logger instance 29 func SetGlobal(logger Logger) { 30 globalLogger = logger 31 } 32 33 // Nop logger for internal modules (default) 34 type nopLogger struct{} 35 36 // NewNopLogger creates a No Operation (NOP) logger 37 func NewNopLogger() Logger { 38 return &nopLogger{} 39 } 40 41 func (*nopLogger) Infof(msg string, args ...any) {} 42 func (*nopLogger) Warnf(msg string, args ...any) {} 43 func (*nopLogger) Errorf(msg string, args ...any) {} 44 func (*nopLogger) Debugf(msg string, args ...any) {} 45 func (*nopLogger) Fatalf(msg string, args ...any) {} 46 func (n *nopLogger) With(args map[string]any) Logger { return n } 47 48 // Constants to standardise logger keys 49 const ( 50 loggerKeyServiceName = "service" 51 loggerKeyServiceEnv = "env" 52 loggerKeyLoggerType = "l" 53 loggerKeyEnvLogFileName = "APP_LOG_FILE" 54 loggerKeyEnvLogLevel = "APP_LOG_LEVEL" 55 ) 56 57 // Initialize logger for the given service name 58 // This is the contract for initializing logger. The actual implementation 59 // of the logger may vary based on runtime environment 60 func Init(name, env string) { 61 InitZapLogger(name, env) 62 }