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