Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
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) |
| 24 | |
| 25 | switch level { |
| 26 | case "trace": |
| 27 | logger.SetLevel(log.TraceLevel) |
| 28 | case "debug": |
| 29 | logger.SetLevel(log.DebugLevel) |
| 30 | case "info": |
| 31 | logger.SetLevel(log.InfoLevel) |
| 32 | case "warn": |
| 33 | logger.SetLevel(log.WarnLevel) |
| 34 | case "error": |
| 35 | logger.SetLevel(log.ErrorLevel) |
| 36 | default: |
| 37 | logger.SetLevel(log.DebugLevel) |
| 38 | logger.WithFields(log.Fields{ |
| 39 | "level": level, |
| 40 | }).Warn("The provided level is unknown. Defaulting to 'debug'") |
| 41 | } |
| 42 | |
| 43 | } |