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.

logrus.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  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. package log
  15. import (
  16. "bufio"
  17. "io"
  18. "os"
  19. "time"
  20. nested "github.com/antonfisher/nested-logrus-formatter"
  21. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  22. "github.com/rifflock/lfshook"
  23. "github.com/sirupsen/logrus"
  24. "github.com/OpenIMSDK/OpenKF/server/internal/config"
  25. )
  26. var _logger *logrus.Logger
  27. // InitLogger init logger.
  28. func InitLogger() {
  29. _logger = loggerInit()
  30. }
  31. func loggerInit() *logrus.Logger {
  32. logger := logrus.New()
  33. // set log level
  34. if config.Config.App.Debug {
  35. logger.SetLevel(logrus.DebugLevel)
  36. } else {
  37. logger.SetLevel(logrus.InfoLevel)
  38. }
  39. // set output
  40. src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
  41. if err != nil {
  42. panic(err.Error())
  43. }
  44. writer := bufio.NewWriter(src)
  45. // write to stdout and file
  46. logger.SetOutput(io.MultiWriter(os.Stdout, writer))
  47. // set formatter
  48. logger.SetFormatter(&nested.Formatter{
  49. TimestampFormat: "2006-01-02 15:04:05.000",
  50. HideKeys: false,
  51. FieldsOrder: []string{"PID", "FilePath", "OperationID"},
  52. })
  53. // add print fileline hook
  54. logger.AddHook(newFilelineHook())
  55. // add rotate file hook, default is 24h
  56. logger.AddHook(NewLfsHook(24*time.Hour, 7))
  57. return logger
  58. }
  59. // NewLfsHook add fileline hook.
  60. func NewLfsHook(rotationTime time.Duration, maxRemainNum uint) logrus.Hook {
  61. lfsHook := lfshook.NewHook(lfshook.WriterMap{
  62. logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum),
  63. logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum),
  64. logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum),
  65. logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum),
  66. }, &nested.Formatter{
  67. TimestampFormat: "2006-01-02 15:04:05.000",
  68. HideKeys: false,
  69. FieldsOrder: []string{"PID", "FilePath", "OperationID"},
  70. })
  71. return lfsHook
  72. }
  73. func initRotateLogs(rotationTime time.Duration, maxRemainNum uint) *rotatelogs.RotateLogs {
  74. writer, err := rotatelogs.New(
  75. config.Config.App.LogFile+".%Y%m%d%H%M",
  76. rotatelogs.WithRotationTime(rotationTime),
  77. rotatelogs.WithRotationCount(maxRemainNum),
  78. )
  79. if err != nil {
  80. panic(err.Error())
  81. } else {
  82. return writer
  83. }
  84. }
  85. // GetLogger get logger instance.
  86. func GetLogger() *logrus.Logger {
  87. return _logger
  88. }
  89. // Info log info.
  90. func Info(Operation string, args ...interface{}) {
  91. _logger.WithFields(logrus.Fields{
  92. "Operation": Operation,
  93. }).Infoln(args...)
  94. }
  95. // Error log error.
  96. func Error(Operation string, args ...interface{}) {
  97. _logger.WithFields(logrus.Fields{
  98. "Operation": Operation,
  99. }).Errorln(args...)
  100. }
  101. // Debug log debug.
  102. func Debug(Operation string, args ...interface{}) {
  103. _logger.WithFields(logrus.Fields{
  104. "Operation": Operation,
  105. }).Debugln(args...)
  106. }
  107. // Panic log panic.
  108. func Panic(Operation string, args ...interface{}) {
  109. _logger.WithFields(logrus.Fields{
  110. "Operation": Operation,
  111. }).Panicln(args...)
  112. }
  113. // Infof log info with format.
  114. func Infof(Operation string, format string, args ...interface{}) {
  115. _logger.WithFields(logrus.Fields{
  116. "Operation": Operation,
  117. }).Infof(format, args...)
  118. }
  119. // Errorf log error with format.
  120. func Errorf(Operation string, format string, args ...interface{}) {
  121. _logger.WithFields(logrus.Fields{
  122. "Operation": Operation,
  123. }).Errorf(format, args...)
  124. }
  125. // Debugf log debug with format.
  126. func Debugf(Operation string, format string, args ...interface{}) {
  127. _logger.WithFields(logrus.Fields{
  128. "Operation": Operation,
  129. }).Debugf(format, args...)
  130. }
  131. // Panicf log panic with format.
  132. func Panicf(Operation string, format string, args ...interface{}) {
  133. _logger.WithFields(logrus.Fields{
  134. "Operation": Operation,
  135. }).Panicf(format, args...)
  136. }