Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 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 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 17 | package common |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 18 | |
| 19 | import log "github.com/sirupsen/logrus" |
| 20 | |
| 21 | func SetLogLevel(logger *log.Logger, level string, caller bool) { |
| 22 | |
| 23 | logger.SetReportCaller(caller) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 24 | 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 Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 29 | |
| 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 | } |