logger.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  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 conf
  15. import (
  16. "os"
  17. "strings"
  18. filename "github.com/keepeye/logrus-filename"
  19. "github.com/sirupsen/logrus"
  20. )
  21. const (
  22. logFileName = "stream.log"
  23. )
  24. var (
  25. Log *logrus.Logger
  26. logFile *os.File
  27. )
  28. func InitLogger() {
  29. Log = logrus.New()
  30. initSyslog()
  31. filenameHook := filename.NewHook()
  32. filenameHook.Field = "file"
  33. Log.AddHook(filenameHook)
  34. Log.SetFormatter(&logrus.TextFormatter{
  35. TimestampFormat: "2006-01-02 15:04:05",
  36. DisableColors: true,
  37. FullTimestamp: true,
  38. })
  39. Log.Debugf("init with args %s", os.Args)
  40. for _, arg := range os.Args {
  41. if strings.HasPrefix(arg, "-test.") {
  42. IsTesting = true
  43. break
  44. }
  45. }
  46. }
  47. func CloseLogger() {
  48. if logFile != nil {
  49. logFile.Close()
  50. }
  51. }