Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package logger |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | |
| 22 | lkh "github.com/gfremex/logrus-kafka-hook" |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | myLogger *log.Entry |
| 28 | ) |
| 29 | |
| 30 | func Setup(kafkaBroker string, level string) { |
| 31 | |
| 32 | logger := log.New() |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame] | 33 | //logger.SetReportCaller(true) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 34 | myLogger = logger.WithField("topics", []string{"bbsim.log"}) |
| 35 | |
| 36 | // TODO make this configurable via cli arg |
| 37 | if level == "DEBUG" { |
| 38 | logger.SetLevel(log.DebugLevel) |
| 39 | } |
| 40 | |
| 41 | if len(kafkaBroker) > 0 { |
| 42 | myLogger.Debug("Setting up kafka integration") |
| 43 | hook, err := lkh.NewKafkaHook( |
| 44 | "kh", |
| 45 | []log.Level{log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel}, |
| 46 | &log.JSONFormatter{ |
| 47 | FieldMap: log.FieldMap{ |
| 48 | log.FieldKeyTime: "@timestamp", |
| 49 | log.FieldKeyLevel: "level", |
| 50 | log.FieldKeyMsg: "message", |
| 51 | }, |
| 52 | }, |
| 53 | []string{kafkaBroker}, |
| 54 | ) |
| 55 | |
| 56 | if err != nil { |
| 57 | myLogger.Error(err) |
| 58 | } |
| 59 | |
| 60 | logger.Hooks.Add(hook) |
| 61 | |
| 62 | } |
| 63 | |
| 64 | myLogger.WithField("kafkaBroker", kafkaBroker).Debug("Logger setup done") |
| 65 | } |
| 66 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 67 | func GetLogger() *log.Entry { |
| 68 | return myLogger |
| 69 | } |
| 70 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 71 | func WithField(key string, value interface{}) *log.Entry { |
| 72 | return myLogger.WithField(key, value) |
| 73 | } |
| 74 | |
| 75 | func WithFields(fields log.Fields) *log.Entry { |
| 76 | return myLogger.WithFields(fields) |
| 77 | } |
| 78 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 79 | func Panic(msg string, args ...interface{}) { |
| 80 | myLogger.Panic(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 83 | func Fatal(msg string, args ...interface{}) { |
| 84 | myLogger.Fatal(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 87 | func Error(msg string, args ...interface{}) { |
| 88 | myLogger.Error(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 91 | func Warn(msg string, args ...interface{}) { |
| 92 | myLogger.Warn(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 95 | func Info(msg string, args ...interface{}) { |
| 96 | myLogger.Info(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 99 | func Debug(msg string, args ...interface{}) { |
| 100 | myLogger.Debug(fmt.Sprintf(msg, args...)) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 101 | } |