Scott Baker | beb3cfa | 2019-10-01 14:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2019 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package logutil |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "github.com/coreos/pkg/capnslog" |
| 21 | "go.uber.org/zap" |
| 22 | "go.uber.org/zap/zapcore" |
| 23 | ) |
| 24 | |
| 25 | var DefaultLogLevel = "info" |
| 26 | |
| 27 | // ConvertToZapLevel converts log level string to zapcore.Level. |
| 28 | func ConvertToZapLevel(lvl string) zapcore.Level { |
| 29 | switch lvl { |
| 30 | case "debug": |
| 31 | return zap.DebugLevel |
| 32 | case "info": |
| 33 | return zap.InfoLevel |
| 34 | case "warn": |
| 35 | return zap.WarnLevel |
| 36 | case "error": |
| 37 | return zap.ErrorLevel |
| 38 | case "dpanic": |
| 39 | return zap.DPanicLevel |
| 40 | case "panic": |
| 41 | return zap.PanicLevel |
| 42 | case "fatal": |
| 43 | return zap.FatalLevel |
| 44 | default: |
| 45 | panic(fmt.Sprintf("unknown level %q", lvl)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // ConvertToCapnslogLogLevel convert log level string to capnslog.LogLevel. |
| 50 | // TODO: deprecate this in 3.5 |
| 51 | func ConvertToCapnslogLogLevel(lvl string) capnslog.LogLevel { |
| 52 | switch lvl { |
| 53 | case "debug": |
| 54 | return capnslog.DEBUG |
| 55 | case "info": |
| 56 | return capnslog.INFO |
| 57 | case "warn": |
| 58 | return capnslog.WARNING |
| 59 | case "error": |
| 60 | return capnslog.ERROR |
| 61 | case "dpanic": |
| 62 | return capnslog.CRITICAL |
| 63 | case "panic": |
| 64 | return capnslog.CRITICAL |
| 65 | case "fatal": |
| 66 | return capnslog.CRITICAL |
| 67 | default: |
| 68 | panic(fmt.Sprintf("unknown level %q", lvl)) |
| 69 | } |
| 70 | } |