blob: e3d1e90a6bff2ded9aa6aabcf69727d8b0985ab1 [file] [log] [blame]
Matteo Scandolo88e91892018-11-06 16:29:19 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package logger
18
19import (
Matteo Scandolo88e91892018-11-06 16:29:19 -080020 lkh "github.com/gfremex/logrus-kafka-hook"
21 log "github.com/sirupsen/logrus"
Zack Williamsba10d092018-11-26 12:00:32 -070022 "time"
Matteo Scandolo88e91892018-11-06 16:29:19 -080023)
24
25var (
26 myLogger *log.Entry
27)
28
29func Setup(kafkaBroker string, level string) {
30
31 logger := log.New()
Shad Ansarief436272018-11-14 15:58:20 -080032 //logger.SetReportCaller(true)
Matteo Scandolo88e91892018-11-06 16:29:19 -080033 myLogger = logger.WithField("topics", []string{"bbsim.log"})
34
Mahir Gunyel09183342019-01-29 14:26:50 -080035 var logLevel log.Level = log.DebugLevel
36 switch level{
37 case "TRACE":
38 logLevel = log.TraceLevel
39 case "INFO":
40 logLevel = log.InfoLevel
41 case "WARN":
42 logLevel = log.WarnLevel
43 case "ERROR":
44 logLevel = log.ErrorLevel
45 default:
46 logLevel = log.DebugLevel
Matteo Scandolo88e91892018-11-06 16:29:19 -080047 }
Mahir Gunyel09183342019-01-29 14:26:50 -080048 logger.Println("Setting Log Level ", logLevel)
49 logger.SetLevel(logLevel)
Matteo Scandolo88e91892018-11-06 16:29:19 -080050
51 if len(kafkaBroker) > 0 {
52 myLogger.Debug("Setting up kafka integration")
53 hook, err := lkh.NewKafkaHook(
54 "kh",
55 []log.Level{log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel},
56 &log.JSONFormatter{
Zack Williamsba10d092018-11-26 12:00:32 -070057 TimestampFormat: time.RFC3339Nano,
Matteo Scandolo88e91892018-11-06 16:29:19 -080058 FieldMap: log.FieldMap{
59 log.FieldKeyTime: "@timestamp",
Zack Williamscedaf6f2018-11-25 21:56:31 -070060 log.FieldKeyLevel: "levelname",
Matteo Scandolo88e91892018-11-06 16:29:19 -080061 log.FieldKeyMsg: "message",
62 },
63 },
64 []string{kafkaBroker},
65 )
66
67 if err != nil {
68 myLogger.Error(err)
69 }
70
71 logger.Hooks.Add(hook)
72
73 }
74
75 myLogger.WithField("kafkaBroker", kafkaBroker).Debug("Logger setup done")
76}
77
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080078func GetLogger() *log.Entry {
79 return myLogger
80}
81
Matteo Scandolo88e91892018-11-06 16:29:19 -080082func WithField(key string, value interface{}) *log.Entry {
83 return myLogger.WithField(key, value)
84}
85
86func WithFields(fields log.Fields) *log.Entry {
87 return myLogger.WithFields(fields)
88}
89
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080090func Panic(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080091 myLogger.Panicf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080092}
93
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080094func Fatal(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080095 myLogger.Fatalf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080096}
97
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080098func Error(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080099 myLogger.Errorf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800100}
101
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800102func Warn(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800103 myLogger.Warnf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800104}
105
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800106func Info(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800107 myLogger.Infof(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800108}
109
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800110func Debug(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800111 myLogger.Debugf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800112}