github.com/gogf/gf@v1.16.9/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  	"fmt"
    11  	"github.com/gogf/gf/os/glog"
    12  	"github.com/gogf/gf/util/gutil"
    13  )
    14  
    15  const (
    16  	frameCoreComponentNameLogger = "gf.core.component.logger"
    17  	configNodeNameLogger         = "logger"
    18  )
    19  
    20  // Log returns an instance of glog.Logger.
    21  // The parameter <name> is the name for the instance.
    22  func Log(name ...string) *glog.Logger {
    23  	instanceName := glog.DefaultName
    24  	if len(name) > 0 && name[0] != "" {
    25  		instanceName = name[0]
    26  	}
    27  	instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName)
    28  	return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
    29  		logger := glog.Instance(instanceName)
    30  		// To avoid file no found error while it's not necessary.
    31  		if Config().Available() {
    32  			var m map[string]interface{}
    33  			nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameLogger)
    34  			if nodeKey == "" {
    35  				nodeKey = configNodeNameLogger
    36  			}
    37  			m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
    38  			if len(m) == 0 {
    39  				m = Config().GetMap(nodeKey)
    40  			}
    41  			if len(m) > 0 {
    42  				if err := logger.SetConfigWithMap(m); err != nil {
    43  					panic(err)
    44  				}
    45  			}
    46  		}
    47  		return logger
    48  	}).(*glog.Logger)
    49  }