blob: 370128452099095d0a1fdc4d1cbf41201d1e3930 [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()
Zdravko Bozakov078a2712019-07-19 23:25:15 +020034 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 Ansarief436272018-11-14 15:58:20 -080050 //logger.SetReportCaller(true)
Matteo Scandolo88e91892018-11-06 16:29:19 -080051 myLogger = logger.WithField("topics", []string{"bbsim.log"})
52
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020053 var logLevel = log.DebugLevel
Keita NISHIMOTO9617c852019-06-17 21:46:44 +090054 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 Scandolo88e91892018-11-06 16:29:19 -080065 }
Mahir Gunyel09183342019-01-29 14:26:50 -080066 logger.Println("Setting Log Level ", logLevel)
67 logger.SetLevel(logLevel)
Matteo Scandolo88e91892018-11-06 16:29:19 -080068
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 Williamsba10d092018-11-26 12:00:32 -070075 TimestampFormat: time.RFC3339Nano,
Matteo Scandolo88e91892018-11-06 16:29:19 -080076 FieldMap: log.FieldMap{
77 log.FieldKeyTime: "@timestamp",
Zack Williamscedaf6f2018-11-25 21:56:31 -070078 log.FieldKeyLevel: "levelname",
Matteo Scandolo88e91892018-11-06 16:29:19 -080079 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 Bozakov7401ff22019-05-28 22:45:12 +020096// GetLogger return logger instance
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080097func GetLogger() *log.Entry {
98 return myLogger
99}
100
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200101// WithField logs message with specified field
Matteo Scandolo88e91892018-11-06 16:29:19 -0800102func WithField(key string, value interface{}) *log.Entry {
103 return myLogger.WithField(key, value)
104}
105
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200106// WithFields logs message with multiple fields
Matteo Scandolo88e91892018-11-06 16:29:19 -0800107func WithFields(fields log.Fields) *log.Entry {
108 return myLogger.WithFields(fields)
109}
110
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200111// Panic logs with log level panic
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800112func Panic(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800113 myLogger.Panicf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800114}
115
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200116// Fatal logs woth loge level fatal
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800117func Fatal(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800118 myLogger.Fatalf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800119}
120
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200121// Error logs with log level error
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800122func Error(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800123 myLogger.Errorf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800124}
125
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200126// Warn logs with log level warn
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800127func Warn(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800128 myLogger.Warnf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800129}
130
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200131// Info logs with log level info
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800132func Info(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800133 myLogger.Infof(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800134}
135
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200136// Debug logs with log level debug
Matteo Scandolo2aca22c2018-11-08 14:12:07 -0800137func Debug(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800138 myLogger.Debugf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800139}
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200140
141// Trace logs with log level trace
142func Trace(msg string, args ...interface{}) {
143 myLogger.Tracef(msg, args...)
144}