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 ( |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 20 | "time" |
| 21 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 22 | lkh "github.com/gfremex/logrus-kafka-hook" |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | myLogger *log.Entry |
| 28 | ) |
| 29 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 30 | // Setup logger |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 31 | func Setup(kafkaBroker string, level string) { |
| 32 | |
| 33 | logger := log.New() |
Shad Ansari | ef43627 | 2018-11-14 15:58:20 -0800 | [diff] [blame] | 34 | //logger.SetReportCaller(true) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 35 | myLogger = logger.WithField("topics", []string{"bbsim.log"}) |
| 36 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 37 | var logLevel = log.DebugLevel |
Keita NISHIMOTO | 9617c85 | 2019-06-17 21:46:44 +0900 | [diff] [blame] | 38 | switch level { |
| 39 | case "TRACE": |
| 40 | logLevel = log.TraceLevel |
| 41 | case "INFO": |
| 42 | logLevel = log.InfoLevel |
| 43 | case "WARN": |
| 44 | logLevel = log.WarnLevel |
| 45 | case "ERROR": |
| 46 | logLevel = log.ErrorLevel |
| 47 | default: |
| 48 | logLevel = log.DebugLevel |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 49 | } |
Mahir Gunyel | 0918334 | 2019-01-29 14:26:50 -0800 | [diff] [blame] | 50 | logger.Println("Setting Log Level ", logLevel) |
| 51 | logger.SetLevel(logLevel) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 52 | |
| 53 | if len(kafkaBroker) > 0 { |
| 54 | myLogger.Debug("Setting up kafka integration") |
| 55 | hook, err := lkh.NewKafkaHook( |
| 56 | "kh", |
| 57 | []log.Level{log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel}, |
| 58 | &log.JSONFormatter{ |
Zack Williams | ba10d09 | 2018-11-26 12:00:32 -0700 | [diff] [blame] | 59 | TimestampFormat: time.RFC3339Nano, |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 60 | FieldMap: log.FieldMap{ |
| 61 | log.FieldKeyTime: "@timestamp", |
Zack Williams | cedaf6f | 2018-11-25 21:56:31 -0700 | [diff] [blame] | 62 | log.FieldKeyLevel: "levelname", |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 63 | log.FieldKeyMsg: "message", |
| 64 | }, |
| 65 | }, |
| 66 | []string{kafkaBroker}, |
| 67 | ) |
| 68 | |
| 69 | if err != nil { |
| 70 | myLogger.Error(err) |
| 71 | } |
| 72 | |
| 73 | logger.Hooks.Add(hook) |
| 74 | |
| 75 | } |
| 76 | |
| 77 | myLogger.WithField("kafkaBroker", kafkaBroker).Debug("Logger setup done") |
| 78 | } |
| 79 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 80 | // GetLogger return logger instance |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 81 | func GetLogger() *log.Entry { |
| 82 | return myLogger |
| 83 | } |
| 84 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 85 | // WithField logs message with specified field |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 86 | func WithField(key string, value interface{}) *log.Entry { |
| 87 | return myLogger.WithField(key, value) |
| 88 | } |
| 89 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 90 | // WithFields logs message with multiple fields |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 91 | func WithFields(fields log.Fields) *log.Entry { |
| 92 | return myLogger.WithFields(fields) |
| 93 | } |
| 94 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 95 | // Panic logs with log level panic |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 96 | func Panic(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 97 | myLogger.Panicf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 100 | // Fatal logs woth loge level fatal |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 101 | func Fatal(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 102 | myLogger.Fatalf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 105 | // Error logs with log level error |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 106 | func Error(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 107 | myLogger.Errorf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 110 | // Warn logs with log level warn |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 111 | func Warn(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 112 | myLogger.Warnf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 115 | // Info logs with log level info |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 116 | func Info(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 117 | myLogger.Infof(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 120 | // Debug logs with log level debug |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 121 | func Debug(msg string, args ...interface{}) { |
Matteo Scandolo | 2a65914 | 2018-11-15 11:23:45 -0800 | [diff] [blame] | 122 | myLogger.Debugf(msg, args...) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 123 | } |