blob: 6efe0f158cb2f4916da1f7954579516578c9003a [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 (
Keita NISHIMOTO9617c852019-06-17 21:46:44 +090020 "time"
21
Matteo Scandolo88e91892018-11-06 16:29:19 -080022 lkh "github.com/gfremex/logrus-kafka-hook"
23 log "github.com/sirupsen/logrus"
24)
25
26var (
27 myLogger *log.Entry
28)
29
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020030// Setup logger
Matteo Scandolo88e91892018-11-06 16:29:19 -080031func Setup(kafkaBroker string, level string) {
32
33 logger := log.New()
Shad Ansarief436272018-11-14 15:58:20 -080034 //logger.SetReportCaller(true)
Matteo Scandolo88e91892018-11-06 16:29:19 -080035 myLogger = logger.WithField("topics", []string{"bbsim.log"})
36
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020037 var logLevel = log.DebugLevel
Keita NISHIMOTO9617c852019-06-17 21:46:44 +090038 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 Scandolo88e91892018-11-06 16:29:19 -080049 }
Mahir Gunyel09183342019-01-29 14:26:50 -080050 logger.Println("Setting Log Level ", logLevel)
51 logger.SetLevel(logLevel)
Matteo Scandolo88e91892018-11-06 16:29:19 -080052
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 Williamsba10d092018-11-26 12:00:32 -070059 TimestampFormat: time.RFC3339Nano,
Matteo Scandolo88e91892018-11-06 16:29:19 -080060 FieldMap: log.FieldMap{
61 log.FieldKeyTime: "@timestamp",
Zack Williamscedaf6f2018-11-25 21:56:31 -070062 log.FieldKeyLevel: "levelname",
Matteo Scandolo88e91892018-11-06 16:29:19 -080063 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 Bozakov7401ff22019-05-28 22:45:12 +020080// GetLogger return logger instance
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080081func GetLogger() *log.Entry {
82 return myLogger
83}
84
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020085// WithField logs message with specified field
Matteo Scandolo88e91892018-11-06 16:29:19 -080086func WithField(key string, value interface{}) *log.Entry {
87 return myLogger.WithField(key, value)
88}
89
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020090// WithFields logs message with multiple fields
Matteo Scandolo88e91892018-11-06 16:29:19 -080091func WithFields(fields log.Fields) *log.Entry {
92 return myLogger.WithFields(fields)
93}
94
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020095// Panic logs with log level panic
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080096func Panic(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080097 myLogger.Panicf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080098}
99
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200100// Fatal logs woth loge level fatal
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800101func Fatal(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800102 myLogger.Fatalf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800103}
104
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200105// Error logs with log level error
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800106func Error(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800107 myLogger.Errorf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800108}
109
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200110// Warn logs with log level warn
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800111func Warn(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800112 myLogger.Warnf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800113}
114
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200115// Info logs with log level info
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800116func Info(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800117 myLogger.Infof(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800118}
119
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200120// Debug logs with log level debug
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800121func Debug(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800122 myLogger.Debugf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800123}