另客网go项目公用的代码库

12345678910111213141516171819202122232425262728293031323334
  1. package log
  2. import (
  3. "git.links123.net/links123.com/pkg/log/hooks"
  4. "github.com/gin-gonic/gin"
  5. "github.com/sirupsen/logrus"
  6. )
  7. var logger = logrus.New()
  8. func NewDefaultLogger(project *hooks.Project) error {
  9. return NewLogger(project, hooks.DefaultMaintainers(), hooks.DefaultNotificationService())
  10. }
  11. func NewLogger(project *hooks.Project, maintainers []*hooks.Maintainer, ns *hooks.NotificationService) error {
  12. notificationHook, err := hooks.NewNotificationHook(project, maintainers, ns)
  13. if err != nil {
  14. return err
  15. }
  16. logger.AddHook(notificationHook)
  17. switch gin.Mode() {
  18. case gin.DebugMode, gin.TestMode:
  19. logger.SetLevel(logrus.DebugLevel)
  20. case gin.ReleaseMode:
  21. logger.SetLevel(logrus.InfoLevel)
  22. default:
  23. panic("unsupported running mode")
  24. }
  25. return nil
  26. }