Files
gf/frame/gins/gins_log.go

64 lines
2.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2020-02-23 20:25:55 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gins
import (
"context"
2020-02-23 20:25:55 +08:00
"fmt"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/instance"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gutil"
2020-02-23 20:25:55 +08:00
)
// Log returns an instance of glog.Logger.
// The parameter `name` is the name for the instance.
// Note that it panics if any error occurs duration instance creating.
2020-02-23 20:25:55 +08:00
func Log(name ...string) *glog.Logger {
var (
ctx = context.Background()
instanceName = glog.DefaultName
)
2020-02-23 20:25:55 +08:00
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
2020-12-14 13:26:48 +08:00
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName)
return instance.GetOrSetFuncLock(instanceKey, func() interface{} {
2020-02-23 20:25:55 +08:00
logger := glog.Instance(instanceName)
// To avoid file no found error while it's not necessary.
var (
configMap map[string]interface{}
loggerNodeName = consts.ConfigNodeNameLogger
)
// Try to find possible `loggerNodeName` in case-insensitive way.
if configData, _ := Config().Data(ctx); len(configData) > 0 {
if v, _ := gutil.MapPossibleItemByKey(configData, consts.ConfigNodeNameLogger); v != "" {
loggerNodeName = v
2020-07-10 00:20:46 +08:00
}
}
// Retrieve certain logger configuration by logger name.
certainLoggerNodeName := fmt.Sprintf(`%s.%s`, loggerNodeName, instanceName)
if v, _ := Config().Get(ctx, certainLoggerNodeName); !v.IsEmpty() {
configMap = v.Map()
}
// Retrieve global logger configuration if configuration for certain logger name does not exist.
if len(configMap) == 0 {
if v, _ := Config().Get(ctx, loggerNodeName); !v.IsEmpty() {
configMap = v.Map()
2020-02-23 20:25:55 +08:00
}
}
// Set logger config if config map is not empty.
if len(configMap) > 0 {
if err := logger.SetConfigWithMap(configMap); err != nil {
panic(err)
2020-02-23 20:25:55 +08:00
}
}
return logger
}).(*glog.Logger)
}