blob: ad67226893ebdde3bca09f2de6cbbb5fd5b8b3d9 [file] [log] [blame]
Matteo Scandolo2bf742a2019-10-01 11:33:34 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo2bf742a2019-10-01 11:33:34 -07003
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
Matteo Scandolo40e067f2019-10-16 16:59:41 -070017package common
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070018
19import log "github.com/sirupsen/logrus"
20
21func SetLogLevel(logger *log.Logger, level string, caller bool) {
22
23 logger.SetReportCaller(caller)
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080024 Formatter := new(log.TextFormatter)
25 Formatter.TimestampFormat = "2006-01-02T15:04:05.999999999Z07:00"
26 Formatter.FullTimestamp = true
27 //Formatter.ForceColors = true
28 logger.SetFormatter(Formatter)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070029
30 switch level {
31 case "trace":
32 logger.SetLevel(log.TraceLevel)
33 case "debug":
34 logger.SetLevel(log.DebugLevel)
35 case "info":
36 logger.SetLevel(log.InfoLevel)
37 case "warn":
38 logger.SetLevel(log.WarnLevel)
39 case "error":
40 logger.SetLevel(log.ErrorLevel)
41 default:
42 logger.SetLevel(log.DebugLevel)
43 logger.WithFields(log.Fields{
44 "level": level,
45 }).Warn("The provided level is unknown. Defaulting to 'debug'")
46 }
47
48}