github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/log/log.go (about) 1 // use function 2 package log 3 4 import ( 5 "fmt" 6 "github.com/weedge/lib/log/tapper" 7 "go.uber.org/zap" 8 9 "path/filepath" 10 ) 11 12 // setup log with those params: 13 // project name for tapper log 14 // log.json config path, 15 // default log path for log.json undefined log path 16 // tapper user defined trace log obj 17 func Setup(projectName string, confPath string, defaultLogPath string, traceLogger tapper.ITraceLog) error { 18 logConfPath := filepath.Join(confPath, defaultLogConfFileName) 19 config, err := getLogConfig(logConfPath) 20 if err != nil { 21 return err 22 } 23 if traceLogger != nil { 24 tapper.TraceLogger = traceLogger 25 } 26 27 return createLogger(config, projectName, defaultLogPath) 28 } 29 30 func Info(params ...interface{}) { 31 if mainLogger != nil { 32 mainLogger.Info(params...) 33 return 34 } 35 defaultLogger.Info(params...) 36 } 37 func Debug(params ...interface{}) { 38 if mainLogger != nil { 39 mainLogger.Debug(params...) 40 return 41 } 42 defaultLogger.Debug(params...) 43 } 44 func Warn(params ...interface{}) { 45 if mainLogger != nil { 46 mainLogger.Warn(params...) 47 return 48 } 49 defaultLogger.Warn(params...) 50 } 51 func Error(params ...interface{}) { 52 if mainLogger != nil { 53 mainLogger.Error(params...) 54 return 55 } 56 defaultLogger.Error(params...) 57 } 58 func Infof(format string, params ...interface{}) { 59 if mainLogger != nil { 60 mainLogger.Infof(format, params...) 61 return 62 } 63 defaultLogger.Infof(format, params...) 64 } 65 func Debugf(format string, params ...interface{}) { 66 if mainLogger != nil { 67 mainLogger.Debugf(format, params...) 68 return 69 } 70 defaultLogger.Debugf(format, params...) 71 } 72 func Warnf(format string, params ...interface{}) { 73 if mainLogger != nil { 74 mainLogger.Warnf(format, params...) 75 return 76 } 77 defaultLogger.Warnf(format, params...) 78 } 79 func Errorf(format string, params ...interface{}) { 80 if mainLogger != nil { 81 mainLogger.Errorf(format, params...) 82 return 83 } 84 defaultLogger.Errorf(format, params...) 85 } 86 87 func BizArchive(format string, params ...interface{}) { 88 if bizLogger != nil { 89 bizLogger.Infof(format, params...) 90 return 91 } 92 defaultLogger.Infof(format, params...) 93 } 94 95 func AccessInfo(msg string, fields ...zap.Field) { 96 if accessLogger != nil { 97 accessLogger.Info(msg, fields...) 98 return 99 } 100 defaultLogger.AccessInfo(msg, fields...) 101 } 102 103 func RpcInfo(params ...interface{}) { 104 if rpcLogger != nil { 105 rpcLogger.Info(params...) 106 return 107 } 108 defaultLogger.Info(params...) 109 } 110 111 func RpcInfof(format string, params ...interface{}) { 112 if rpcLogger != nil { 113 rpcLogger.Infof(format, params...) 114 return 115 } 116 defaultLogger.Infof(format, params...) 117 } 118 119 // recover panic log 120 func Recover(v ...interface{}) { 121 if panicLogger != nil { 122 //panicLogger.Fatal(fmt.Sprint(v...)) 123 panicLogger.Error(fmt.Sprint(v...)) 124 return 125 } 126 //defaultLogger.Fatal(fmt.Sprint(v...)) 127 defaultLogger.Error(fmt.Sprint(v...)) 128 } 129 130 // recover panic format log 131 func Recoverf(format string, params ...interface{}) { 132 if panicLogger != nil { 133 //panicLogger.Fatal(fmt.Sprint(params...)) 134 panicLogger.Error(fmt.Sprint(params...)) 135 return 136 } 137 //defaultLogger.Fatal(fmt.Sprint(params...)) 138 defaultLogger.Error(fmt.Sprint(params...)) 139 } 140 141 // flush main, biz, access, panic, rpc log 142 // Sync flushes any buffered log entries. 143 func FlushLog() { 144 if mainLogger != nil { 145 mainLogger.Sync() 146 } 147 if bizLogger != nil { 148 bizLogger.Sync() 149 } 150 if accessLogger != nil { 151 accessLogger.Sync() 152 } 153 if panicLogger != nil { 154 panicLogger.Sync() 155 } 156 if rpcLogger != nil { 157 rpcLogger.Sync() 158 } 159 }