blob: 444cabb9bc48b46860d9bec81cf397cc25b9c32d [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"
Zack Williamsba10d092018-11-26 12:00:32 -070022 "time"
Matteo Scandolo88e91892018-11-06 16:29:19 -080023)
24
25var (
26 myLogger *log.Entry
27)
28
29func Setup(kafkaBroker string, level string) {
30
31 logger := log.New()
Shad Ansarief436272018-11-14 15:58:20 -080032 //logger.SetReportCaller(true)
Matteo Scandolo88e91892018-11-06 16:29:19 -080033 myLogger = logger.WithField("topics", []string{"bbsim.log"})
34
35 // TODO make this configurable via cli arg
36 if level == "DEBUG" {
37 logger.SetLevel(log.DebugLevel)
38 }
39
40 if len(kafkaBroker) > 0 {
41 myLogger.Debug("Setting up kafka integration")
42 hook, err := lkh.NewKafkaHook(
43 "kh",
44 []log.Level{log.DebugLevel, log.InfoLevel, log.WarnLevel, log.ErrorLevel},
45 &log.JSONFormatter{
Zack Williamsba10d092018-11-26 12:00:32 -070046 TimestampFormat: time.RFC3339Nano,
Matteo Scandolo88e91892018-11-06 16:29:19 -080047 FieldMap: log.FieldMap{
48 log.FieldKeyTime: "@timestamp",
Zack Williamscedaf6f2018-11-25 21:56:31 -070049 log.FieldKeyLevel: "levelname",
Matteo Scandolo88e91892018-11-06 16:29:19 -080050 log.FieldKeyMsg: "message",
51 },
52 },
53 []string{kafkaBroker},
54 )
55
56 if err != nil {
57 myLogger.Error(err)
58 }
59
60 logger.Hooks.Add(hook)
61
62 }
63
64 myLogger.WithField("kafkaBroker", kafkaBroker).Debug("Logger setup done")
65}
66
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080067func GetLogger() *log.Entry {
68 return myLogger
69}
70
Matteo Scandolo88e91892018-11-06 16:29:19 -080071func WithField(key string, value interface{}) *log.Entry {
72 return myLogger.WithField(key, value)
73}
74
75func WithFields(fields log.Fields) *log.Entry {
76 return myLogger.WithFields(fields)
77}
78
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080079func Panic(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080080 myLogger.Panicf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080081}
82
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080083func Fatal(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080084 myLogger.Fatalf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080085}
86
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080087func Error(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080088 myLogger.Errorf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080089}
90
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080091func Warn(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080092 myLogger.Warnf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080093}
94
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080095func Info(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -080096 myLogger.Infof(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -080097}
98
Matteo Scandolo2aca22c2018-11-08 14:12:07 -080099func Debug(msg string, args ...interface{}) {
Matteo Scandolo2a659142018-11-15 11:23:45 -0800100 myLogger.Debugf(msg, args...)
Matteo Scandolo88e91892018-11-06 16:29:19 -0800101}