42 lines
939 B
Go
42 lines
939 B
Go
|
package oclib
|
||
|
|
||
|
import (
|
||
|
"oc-lib/logs"
|
||
|
"os"
|
||
|
"runtime"
|
||
|
"time"
|
||
|
|
||
|
"github.com/rs/zerolog"
|
||
|
)
|
||
|
|
||
|
var logger zerolog.Logger
|
||
|
|
||
|
// 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
|
||
|
func CreateLogger(appname string, url string) zerolog.Logger {
|
||
|
|
||
|
if url != "" {
|
||
|
labels := map[string]string{
|
||
|
"app": "app",
|
||
|
"code": "go",
|
||
|
"platform": runtime.GOOS,
|
||
|
}
|
||
|
|
||
|
lokiWriter := logs.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
|
||
|
}
|