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 ( |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 20 | lkh "github.com/gfremex/logrus-kafka-hook" |
| 21 | log "github.com/sirupsen/logrus" |
Zack Williams | ba10d09 | 2018-11-26 12:00:32 -0700 | [diff] [blame] | 22 | "time" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | myLogger *log.Entry |
| 27 | ) |
| 28 | |
| 29 | func Setup(kafkaBroker string, level string) { |
| 30 | |
| 31 | logger := log.New() |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame] | 32 | //logger.SetReportCaller(true) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 33 | myLogger = logger.WithField("topics", []string{"bbsim.log"}) |
| 34 | |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 35 | 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 Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 47 | } |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 48 | logger.Println("Setting Log Level ", logLevel) |
| 49 | logger.SetLevel(logLevel) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 50 | |
| 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 Williams | ba10d09 | 2018-11-26 12:00:32 -0700 | [diff] [blame] | 57 | TimestampFormat: time.RFC3339Nano, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 58 | FieldMap: log.FieldMap{ |
| 59 | log.FieldKeyTime: "@timestamp", |
Zack Williams | cedaf6f | 2018-11-25 21:56:31 -0700 | [diff] [blame] | 60 | log.FieldKeyLevel: "levelname", |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 61 | 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 Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 78 | func GetLogger() *log.Entry { |
| 79 | return myLogger |
| 80 | } |
| 81 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 82 | func WithField(key string, value interface{}) *log.Entry { |
| 83 | return myLogger.WithField(key, value) |
| 84 | } |
| 85 | |
| 86 | func WithFields(fields log.Fields) *log.Entry { |
| 87 | return myLogger.WithFields(fields) |
| 88 | } |
| 89 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 90 | func Panic(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 91 | myLogger.Panicf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 94 | func Fatal(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 95 | myLogger.Fatalf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 98 | func Error(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 99 | myLogger.Errorf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 102 | func Warn(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 103 | myLogger.Warnf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 106 | func Info(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 107 | myLogger.Infof(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 110 | func Debug(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 111 | myLogger.Debugf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 112 | } |