gins_log.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gins
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/os/glog"
  10. "github.com/gogf/gf/util/gutil"
  11. )
  12. const (
  13. frameCoreComponentNameLogger = "gf.core.component.logger"
  14. configNodeNameLogger = "logger"
  15. )
  16. // Log returns an instance of glog.Logger.
  17. // The parameter <name> is the name for the instance.
  18. func Log(name ...string) *glog.Logger {
  19. instanceName := glog.DefaultName
  20. if len(name) > 0 && name[0] != "" {
  21. instanceName = name[0]
  22. }
  23. instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName)
  24. return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
  25. logger := glog.Instance(instanceName)
  26. // To avoid file no found error while it's not necessary.
  27. if Config().Available() {
  28. var m map[string]interface{}
  29. nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameLogger)
  30. if nodeKey == "" {
  31. nodeKey = configNodeNameLogger
  32. }
  33. m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
  34. if len(m) == 0 {
  35. m = Config().GetMap(nodeKey)
  36. }
  37. if len(m) > 0 {
  38. if err := logger.SetConfigWithMap(m); err != nil {
  39. panic(err)
  40. }
  41. }
  42. }
  43. return logger
  44. }).(*glog.Logger)
  45. }