blob: bd1738577058d93c69a3aba2decc2be71b29e5fa [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 (
Matteo Scandolo88e91892018-11-06 16:29:19 -080020 lkh "github.com/gfremex/logrus-kafka-hook"
21 log "github.com/sirupsen/logrus"
22)
23
24var (
25 myLogger *log.Entry
26)
27
28func Setup(kafkaBroker string, level string) {
29
30 logger := log.New()
Shad Ansarief436272018-11-14 15:58:20 -080031 //logger.SetReportCaller(true)
Matteo Scandolo88e91892018-11-06 16:29:19 -080032 myLogger = logger.WithField("topics", []string{"bbsim.log"})
33
34 // TODO make this configurable via cli arg
35 if level == "DEBUG" {
36 logger.SetLevel(log.DebugLevel)
37 }
38
39 if len(kafkaBroker) > 0 {
40 myLogger.Debug("Setting up kafka integration")
41 hook, err := lkh.NewKafkaHook(
42 "kh",
43 []log.Level{log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel},
44 &log.JSONFormatter{
45 FieldMap: log.FieldMap{
46 log.FieldKeyTime: "@timestamp",
Zack Williamscedaf6f2018-11-25 21:56:31 -070047 log.FieldKeyLevel: "levelname",
Matteo Scandolo88e91892018-11-06 16:29:19 -080048 log.FieldKeyMsg: "message",
49 },
50 },
51 []string{kafkaBroker},
52 )
53
54 if err != nil {
55 myLogger.Error(err)
56 }
57
58 logger.Hooks.Add(hook)
59
60 }
61
62 myLogger.WithField("kafkaBroker", kafkaBroker).Debug("Logger setup done")
63}
64
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080065func GetLogger() *log.Entry {
66 return myLogger
67}
68
Matteo Scandolo88e91892018-11-06 16:29:19 -080069func WithField(key string, value interface{}) *log.Entry {
70 return myLogger.WithField(key, value)
71}
72
73func WithFields(fields log.Fields) *log.Entry {
74 return myLogger.WithFields(fields)
75}
76
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080077func Panic(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080078 myLogger.Panicf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080079}
80
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080081func Fatal(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080082 myLogger.Fatalf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080083}
84
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080085func Error(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080086 myLogger.Errorf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080087}
88
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080089func Warn(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080090 myLogger.Warnf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080091}
92
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080093func Info(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080094 myLogger.Infof(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080095}
96
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080097func Debug(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080098 myLogger.Debugf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080099}