You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging.go 5.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package log
  18. import (
  19. "bytes"
  20. "errors"
  21. "fmt"
  22. )
  23. import (
  24. "github.com/natefinch/lumberjack"
  25. "go.uber.org/zap"
  26. "go.uber.org/zap/zapcore"
  27. )
  28. // Level represents the level of logging.
  29. type LogLevel int8
  30. const (
  31. // DebugLevel logs are typically voluminous, and are usually disabled in
  32. // production.
  33. DebugLevel = LogLevel(zapcore.DebugLevel)
  34. // InfoLevel is the default logging priority.
  35. InfoLevel = LogLevel(zapcore.InfoLevel)
  36. // WarnLevel logs are more important than Info, but don't need individual
  37. // human review.
  38. WarnLevel = LogLevel(zapcore.WarnLevel)
  39. // ErrorLevel logs are high-priority. If an application is running smoothly,
  40. // it shouldn't generate any error-level logs.
  41. ErrorLevel = LogLevel(zapcore.ErrorLevel)
  42. // PanicLevel logs a message, then panics.
  43. PanicLevel = LogLevel(zapcore.PanicLevel)
  44. // FatalLevel logs a message, then calls os.Exit(1).
  45. FatalLevel = LogLevel(zapcore.FatalLevel)
  46. )
  47. func (l *LogLevel) UnmarshalText(text []byte) error {
  48. if l == nil {
  49. return errors.New("can't unmarshal a nil *Level")
  50. }
  51. if !l.unmarshalText(text) && !l.unmarshalText(bytes.ToLower(text)) {
  52. return fmt.Errorf("unrecognized level: %q", text)
  53. }
  54. return nil
  55. }
  56. func (l *LogLevel) unmarshalText(text []byte) bool {
  57. switch string(text) {
  58. case "debug", "DEBUG":
  59. *l = DebugLevel
  60. case "info", "INFO", "": // make the zero value useful
  61. *l = InfoLevel
  62. case "warn", "WARN":
  63. *l = WarnLevel
  64. case "error", "ERROR":
  65. *l = ErrorLevel
  66. case "panic", "PANIC":
  67. *l = PanicLevel
  68. case "fatal", "FATAL":
  69. *l = FatalLevel
  70. default:
  71. return false
  72. }
  73. return true
  74. }
  75. type Logger interface {
  76. Debug(v ...interface{})
  77. Debugf(format string, v ...interface{})
  78. Info(v ...interface{})
  79. Infof(format string, v ...interface{})
  80. Warn(v ...interface{})
  81. Warnf(format string, v ...interface{})
  82. Error(v ...interface{})
  83. Errorf(format string, v ...interface{})
  84. Panic(v ...interface{})
  85. Panicf(format string, v ...interface{})
  86. Fatal(v ...interface{})
  87. Fatalf(format string, v ...interface{})
  88. }
  89. var (
  90. log Logger
  91. zapLogger *zap.Logger
  92. zapLoggerConfig = zap.NewDevelopmentConfig()
  93. zapLoggerEncoderConfig = zapcore.EncoderConfig{
  94. TimeKey: "time",
  95. LevelKey: "level",
  96. NameKey: "logger",
  97. CallerKey: "caller",
  98. MessageKey: "message",
  99. StacktraceKey: "stacktrace",
  100. EncodeLevel: zapcore.CapitalColorLevelEncoder,
  101. EncodeTime: zapcore.ISO8601TimeEncoder,
  102. EncodeDuration: zapcore.SecondsDurationEncoder,
  103. EncodeCaller: zapcore.ShortCallerEncoder,
  104. }
  105. )
  106. func init() {
  107. zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
  108. zapLogger, _ = zapLoggerConfig.Build(zap.AddCallerSkip(1))
  109. log = zapLogger.Sugar()
  110. }
  111. func Init(logPath string, level LogLevel) {
  112. lumberJackLogger := &lumberjack.Logger{
  113. Filename: logPath,
  114. MaxSize: 10,
  115. MaxBackups: 5,
  116. MaxAge: 30,
  117. Compress: false,
  118. }
  119. syncer := zapcore.AddSync(lumberJackLogger)
  120. encoderConfig := zap.NewProductionEncoderConfig()
  121. encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
  122. encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
  123. encoder := zapcore.NewConsoleEncoder(encoderConfig)
  124. core := zapcore.NewCore(encoder, syncer, zap.NewAtomicLevelAt(zapcore.Level(level)))
  125. zapLogger = zap.New(core, zap.AddCaller())
  126. log = zapLogger.Sugar()
  127. }
  128. // SetLogger: customize yourself logger.
  129. func SetLogger(logger Logger) {
  130. log = logger
  131. }
  132. // GetLogger get logger
  133. func GetLogger() Logger {
  134. return log
  135. }
  136. // Debug ...
  137. func Debug(v ...interface{}) {
  138. log.Debug(v...)
  139. }
  140. // Debugf ...
  141. func Debugf(format string, v ...interface{}) {
  142. log.Debugf(format, v...)
  143. }
  144. // Info ...
  145. func Info(v ...interface{}) {
  146. log.Info(v...)
  147. }
  148. // Infof ...
  149. func Infof(format string, v ...interface{}) {
  150. log.Infof(format, v...)
  151. }
  152. // Warn ...
  153. func Warn(v ...interface{}) {
  154. log.Warn(v...)
  155. }
  156. // Warnf ...
  157. func Warnf(format string, v ...interface{}) {
  158. log.Warnf(format, v...)
  159. }
  160. // Error ...
  161. func Error(v ...interface{}) {
  162. log.Error(v...)
  163. }
  164. // Errorf ...
  165. func Errorf(format string, v ...interface{}) {
  166. log.Errorf(format, v...)
  167. }
  168. // Panic ...
  169. func Panic(v ...interface{}) {
  170. log.Panic(v...)
  171. }
  172. // Panicf ...
  173. func Panicf(format string, v ...interface{}) {
  174. log.Panicf(format, v...)
  175. }
  176. // Fatal ...
  177. func Fatal(v ...interface{}) {
  178. log.Fatal(v...)
  179. }
  180. // Fatalf ...
  181. func Fatalf(format string, v ...interface{}) {
  182. log.Fatalf(format, v...)
  183. }

Go Implementation For Seata