log.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Just logrus wrapper, maybe replace in the future
  15. package log
  16. import (
  17. "runtime"
  18. "runtime/debug"
  19. "sync"
  20. "github.com/sirupsen/logrus"
  21. "yunion.io/x/log/hooks"
  22. )
  23. var (
  24. verbosity int32 = 0
  25. verbosityLock sync.Mutex = sync.Mutex{}
  26. logger *logrus.Logger = logrus.StandardLogger()
  27. )
  28. func init() {
  29. level := logrus.DebugLevel
  30. AddHookFormatter(logger)
  31. SetLogLevel(logger, level)
  32. }
  33. func Logger() *logrus.Logger {
  34. return logger
  35. }
  36. func SetVerboseLevel(level int32) {
  37. verbosityLock.Lock()
  38. defer verbosityLock.Unlock()
  39. verbosity = level
  40. }
  41. // Verbose is a boolean type that implements Infof (like Printf) etc.
  42. type Verbose bool
  43. // V reports whether verbosity at the call site is at least the requested level.
  44. // The returned value is a boolean of type Verbose, which implements Infof, and Printf etc.
  45. // These methods will write to the Info log if called.
  46. // Thus, one may write either
  47. // if log.V(2) { log.Infof("log this") }
  48. // or
  49. // log.V(2).Infof("log this")
  50. // The second form is shorter but the first is cheaper if logging is off because it does
  51. // not evaluate its arguments.
  52. //
  53. // Whether an individual call to V generates a log record depends on the setting of
  54. // the --log-level flags; both are off by default. If the level in the call to
  55. // V is at least the value of --log-level for the source file containing the
  56. // call, the V call will log.
  57. func V(level int32) Verbose {
  58. if verbosity >= level {
  59. return Verbose(true)
  60. }
  61. return Verbose(false)
  62. }
  63. func (v Verbose) Debugf(format string, args ...interface{}) {
  64. if v {
  65. logrus.Debugf(format, args...)
  66. }
  67. }
  68. func (v Verbose) Printf(format string, args ...interface{}) {
  69. if v {
  70. logrus.Printf(format, args...)
  71. }
  72. }
  73. func (v Verbose) Infof(format string, args ...interface{}) {
  74. if v {
  75. logrus.Infof(format, args...)
  76. }
  77. }
  78. func (v Verbose) Warningf(format string, args ...interface{}) {
  79. if v {
  80. logrus.Warningf(format, args...)
  81. }
  82. }
  83. func Debugf(format string, args ...interface{}) {
  84. logrus.Debugf(format, args...)
  85. }
  86. func Debugln(args ...interface{}) {
  87. logrus.Debugln(args...)
  88. }
  89. func Printf(format string, args ...interface{}) {
  90. logrus.Printf(format, args...)
  91. }
  92. func Println(args ...interface{}) {
  93. logrus.Println(args...)
  94. }
  95. func Infof(format string, args ...interface{}) {
  96. logrus.Infof(format, args...)
  97. }
  98. func Infoln(args ...interface{}) {
  99. logrus.Infoln(args...)
  100. }
  101. func Warningf(format string, args ...interface{}) {
  102. logrus.Warnf(format, args...)
  103. }
  104. func Warningln(args ...interface{}) {
  105. logrus.Warnln(args...)
  106. }
  107. func Errorf(format string, args ...interface{}) {
  108. logrus.Errorf(format, args...)
  109. }
  110. func Errorln(args ...interface{}) {
  111. logrus.Errorln(args...)
  112. }
  113. func Fatalf(format string, args ...interface{}) {
  114. debug.PrintStack()
  115. logrus.Fatalf(format, args...)
  116. }
  117. func Fatalln(args ...interface{}) {
  118. debug.PrintStack()
  119. logrus.Fatalln(args...)
  120. }
  121. func DisableColors() {
  122. formatter, ok := logger.Formatter.(*TextFormatter)
  123. if ok {
  124. formatter.DisableColors = true
  125. }
  126. }
  127. func AddHookFormatter(logger *logrus.Logger) {
  128. pcs := make([]uintptr, 1)
  129. npcs := runtime.Callers(1, pcs)
  130. frames := runtime.CallersFrames(pcs[:npcs])
  131. var myName string
  132. for {
  133. f, more := frames.Next()
  134. myName = f.Function
  135. if !more {
  136. break
  137. }
  138. }
  139. logrusPackage := hooks.GetPackageName(myName)
  140. logger.Hooks.Add(&hooks.CallerHook{logrusPackage})
  141. logger.Formatter = &TextFormatter{
  142. TimestampFormat: "060102 15:04:05",
  143. SpacePadding: 0,
  144. }
  145. }
  146. func SetLogLevelByString(logger *logrus.Logger, lvl string) error {
  147. level, err := logrus.ParseLevel(lvl)
  148. if err != nil {
  149. return err
  150. }
  151. SetLogLevel(logger, level)
  152. return nil
  153. }
  154. func SetLogLevel(logger *logrus.Logger, level logrus.Level) {
  155. logger.Level = level
  156. }