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 (
|
2021-09-19 10:01:09 +08:00
|
|
|
"context"
|
2020-02-23 20:25:55 +08:00
|
|
|
"fmt"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2022-04-13 21:08:12 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/consts"
|
2023-01-06 14:15:30 +08:00
|
|
|
"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.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The parameter `name` is the name for the instance.
|
2021-09-19 10:01:09 +08:00
|
|
|
// 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 {
|
2021-09-19 10:01:09 +08:00
|
|
|
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)
|
2023-01-06 14:15:30 +08:00
|
|
|
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.
|
2021-09-19 10:01:09 +08:00
|
|
|
var (
|
|
|
|
|
configMap map[string]interface{}
|
2022-04-13 21:08:12 +08:00
|
|
|
loggerNodeName = consts.ConfigNodeNameLogger
|
2021-09-19 10:01:09 +08:00
|
|
|
)
|
|
|
|
|
// Try to find possible `loggerNodeName` in case-insensitive way.
|
|
|
|
|
if configData, _ := Config().Data(ctx); len(configData) > 0 {
|
2022-04-13 21:08:12 +08:00
|
|
|
if v, _ := gutil.MapPossibleItemByKey(configData, consts.ConfigNodeNameLogger); v != "" {
|
2021-09-19 10:01:09 +08:00
|
|
|
loggerNodeName = v
|
2020-07-10 00:20:46 +08:00
|
|
|
}
|
2021-09-19 10:01:09 +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
|
|
|
}
|
2021-09-19 10:01:09 +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)
|
|
|
|
|
}
|