main logger

This commit is contained in:
mr 2024-07-18 17:55:27 +02:00
parent 3e9eae007b
commit 0a441cd3df
7 changed files with 40 additions and 25 deletions

View File

@ -39,20 +39,19 @@ type MongoDB struct {
func (m *MongoDB) Init(collections []string, config MongoConf) {
// var baseConfig string
m.Logger = logs.CreateLogger("oc-lib", "")
m.Logger = logs.GetLogger()
ResourceMap = make(map[string]interface{})
logger := logs.CreateLogger("oclib", "")
logger.Info().Msg("Connecting to" + config.GetUrl())
m.Logger.Info().Msg("Connecting to" + config.GetUrl())
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
m.createClient(config.GetUrl())
logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase())
m.Logger.Info().Msg("Connecting mongo client to db " + config.GetDatabase())
m.prepareDB(collections, config)
logger.Info().Msg("Database is READY")
m.Logger.Info().Msg("Database is READY")
}

View File

@ -2,6 +2,7 @@ package oclib
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models"
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
@ -27,27 +28,29 @@ type LibData struct {
Err error `bson:"error" json:"error"`
}
func Init() {
func Init(appName string) {
logs.SetAppName(appName)
logs.SetLogger(logs.CreateLogger("main", ""))
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
}
func LoadOne(col LibDataEnum, id string) LibData {
d, err := models.Model(col.EnumIndex()).GetAccessor().LoadOne(id)
func LoadOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().LoadOne(id)
return LibData{Data: d, Err: err}
}
func UpdateOne(col LibDataEnum, set map[string]interface{}, id string) LibData {
d, err := models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id)
func UpdateOne(collection LibDataEnum, set map[string]interface{}, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().UpdateOne(set, id)
return LibData{Data: d, Err: err}
}
func DeleteOne(col LibDataEnum, id string) LibData {
d, err := models.Model(col.EnumIndex()).GetAccessor().DeleteOne(id)
func DeleteOne(collection LibDataEnum, id string) LibData {
d, err := models.Model(collection.EnumIndex()).GetAccessor().DeleteOne(id)
return LibData{Data: d, Err: err}
}
func StoreOne(col LibDataEnum, object map[string]interface{}) LibData {
model := models.Model(col.EnumIndex())
func StoreOne(collection LibDataEnum, object map[string]interface{}) LibData {
model := models.Model(collection.EnumIndex())
d, err := model.GetAccessor().StoreOne(model.Deserialize(object))
return LibData{Data: d, Err: err}
}

View File

@ -9,7 +9,7 @@ import (
)
var logger zerolog.Logger
var appname string
// logs.CreateLogger
// Create a new logger
// Parameters:
@ -17,13 +17,30 @@ var logger zerolog.Logger
// - url: string : the url of a loki logger, console log only if ""
// Returns:
// - zerolog.Logger : the logger that will log for the library and the app
func CreateLogger(appname string, url string) zerolog.Logger {
func SetAppName(name string) {
appname = name
}
func GetAppName() string {
return appname
}
func GetLogger() zerolog.Logger {
return logger
}
func SetLogger(l zerolog.Logger) {
logger = l
}
func CreateLogger(funcName string, url string) zerolog.Logger {
if url != "" {
labels := map[string]string{
"app": "app",
"app": appname,
"code": "go",
"platform": runtime.GOOS,
"function": funcName,
}
lokiWriter := NewLokiWriter(url, labels)

View File

@ -20,7 +20,7 @@ var models = map[string]func() utils.DBObject{
}
func Model(model int) utils.DBObject {
log := logs.CreateLogger("oclib", "")
log := logs.GetLogger()
if _, ok := models[r.FromInt(model)]; ok {
return models[r.FromInt(model)]()
}

View File

@ -2,7 +2,6 @@ package processing
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -33,8 +32,7 @@ func (pma *ProcessingMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
res_mongo, err := mongo.MONGOService.LoadOne(id, pma.GetType())
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
pma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
}

View File

@ -2,7 +2,6 @@ package storage
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
@ -33,8 +32,7 @@ func (sma *StorageMongoAccessor) LoadOne(id string) (utils.DBObject, error) {
res_mongo, err := mongo.MONGOService.LoadOne(id, sma.GetType())
if err != nil {
l := logs.CreateLogger("oclib", "")
l.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
sma.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, err
}

View File

@ -17,7 +17,7 @@ func (dma *AbstractAccessor) GetType() string {
}
func (dma *AbstractAccessor) SetLogger(t resources.ResourceType) {
dma.Logger = logs.CreateLogger("oclib", t.String())
dma.Logger = logs.CreateLogger(t.String(), "")
dma.Type = t.String()
}
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, error) {