1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (c) 2020 Intel Corporation
12 "gitlab.com/project-emco/core/emco-base/src/orchestrator/pkg/infra/config"
13 log "github.com/sirupsen/logrus"
16 //Fields is type that will be used by the calling function
17 type Fields map[string]interface{}
20 // Log as JSON instead of the default ASCII formatter.
21 log.SetFormatter(&log.JSONFormatter{TimestampFormat: "2006-01-02T15:04:05.999999999Z07:00"})
22 if strings.EqualFold(config.GetConfiguration().LogLevel, "warn") {
23 log.SetLevel(log.WarnLevel)
26 if strings.EqualFold(config.GetConfiguration().LogLevel, "info") {
27 log.SetLevel(log.InfoLevel)
31 // Error uses the fields provided and logs
32 func Error(msg string, fields Fields) {
33 if pc, file, line, ok := runtime.Caller(1); ok {
35 fields["SOURCE"] = fmt.Sprintf("file[%s:%d] func[%s]", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
37 log.WithFields(log.Fields(fields)).Error(msg)
39 log.WithFields(log.Fields(fields)).Error(msg)
43 // Warn uses the fields provided and logs
44 func Warn(msg string, fields Fields) {
45 if pc, file, line, ok := runtime.Caller(1); ok {
47 fields["SOURCE"] = fmt.Sprintf("file[%s:%d] func[%s]", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
49 log.WithFields(log.Fields(fields)).Warn(msg)
51 log.WithFields(log.Fields(fields)).Warn(msg)
55 // Info uses the fields provided and logs
56 func Info(msg string, fields Fields) {
57 if pc, file, line, ok := runtime.Caller(1); ok {
59 fields["SOURCE"] = fmt.Sprintf("file[%s:%d] func[%s]", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
61 log.WithFields(log.Fields(fields)).Info(msg)
63 log.WithFields(log.Fields(fields)).Info(msg)
67 // SetLoglevel .. Set Log level
68 func SetLoglevel(level log.Level) {