oc-lib/logs/logger.go

52 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-07-17 18:02:30 +02:00
package logs
import (
"os"
"runtime"
"time"
2024-09-04 10:53:12 +02:00
"cloud.o-forge.io/core/oc-lib/config"
2024-07-17 18:02:30 +02:00
"github.com/rs/zerolog"
)
var logger zerolog.Logger
2024-09-04 10:53:12 +02:00
2024-07-17 18:02:30 +02:00
// logs.CreateLogger
// Create a new logger
// Parameters:
// - appname: string : the name of the application using oclib
// - 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
2024-07-18 17:55:27 +02:00
func GetLogger() zerolog.Logger {
return logger
}
func SetLogger(l zerolog.Logger) {
logger = l
}
2024-09-04 10:53:12 +02:00
func CreateLogger(funcName string) zerolog.Logger {
url := config.GetConfig().LokiUrl
2024-07-17 18:02:30 +02:00
if url != "" {
labels := map[string]string{
2024-09-04 10:53:12 +02:00
"app": config.GetAppName(),
2024-07-17 18:02:30 +02:00
"code": "go",
"platform": runtime.GOOS,
2024-07-18 17:55:27 +02:00
"function": funcName,
2024-07-17 18:02:30 +02:00
}
lokiWriter := NewLokiWriter(url, labels)
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
multiWriter := zerolog.MultiLevelWriter(consoleWriter, lokiWriter)
logger = zerolog.New(multiWriter).With().Timestamp().Logger()
} else {
logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
return logger
}