github.com/gogf/gf/v2@v2.7.4/frame/gins/gins_log.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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 7 package gins 8 9 import ( 10 "context" 11 "fmt" 12 13 "github.com/gogf/gf/v2/internal/consts" 14 "github.com/gogf/gf/v2/internal/instance" 15 "github.com/gogf/gf/v2/os/glog" 16 "github.com/gogf/gf/v2/util/gutil" 17 ) 18 19 // Log returns an instance of glog.Logger. 20 // The parameter `name` is the name for the instance. 21 // Note that it panics if any error occurs duration instance creating. 22 func Log(name ...string) *glog.Logger { 23 var ( 24 ctx = context.Background() 25 instanceName = glog.DefaultName 26 ) 27 if len(name) > 0 && name[0] != "" { 28 instanceName = name[0] 29 } 30 instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName) 31 return instance.GetOrSetFuncLock(instanceKey, func() interface{} { 32 logger := glog.Instance(instanceName) 33 // To avoid file no found error while it's not necessary. 34 var ( 35 configMap map[string]interface{} 36 loggerNodeName = consts.ConfigNodeNameLogger 37 ) 38 // Try to find possible `loggerNodeName` in case-insensitive way. 39 if configData, _ := Config().Data(ctx); len(configData) > 0 { 40 if v, _ := gutil.MapPossibleItemByKey(configData, consts.ConfigNodeNameLogger); v != "" { 41 loggerNodeName = v 42 } 43 } 44 // Retrieve certain logger configuration by logger name. 45 certainLoggerNodeName := fmt.Sprintf(`%s.%s`, loggerNodeName, instanceName) 46 if v, _ := Config().Get(ctx, certainLoggerNodeName); !v.IsEmpty() { 47 configMap = v.Map() 48 } 49 // Retrieve global logger configuration if configuration for certain logger name does not exist. 50 if len(configMap) == 0 { 51 if v, _ := Config().Get(ctx, loggerNodeName); !v.IsEmpty() { 52 configMap = v.Map() 53 } 54 } 55 // Set logger config if config map is not empty. 56 if len(configMap) > 0 { 57 if err := logger.SetConfigWithMap(configMap); err != nil { 58 panic(err) 59 } 60 } 61 return logger 62 }).(*glog.Logger) 63 }