log_level.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. package logutil
  15. import (
  16. "fmt"
  17. "github.com/coreos/pkg/capnslog"
  18. "go.uber.org/zap"
  19. "go.uber.org/zap/zapcore"
  20. )
  21. var DefaultLogLevel = "info"
  22. // ConvertToZapLevel converts log level string to zapcore.Level.
  23. func ConvertToZapLevel(lvl string) zapcore.Level {
  24. switch lvl {
  25. case "debug":
  26. return zap.DebugLevel
  27. case "info":
  28. return zap.InfoLevel
  29. case "warn":
  30. return zap.WarnLevel
  31. case "error":
  32. return zap.ErrorLevel
  33. case "dpanic":
  34. return zap.DPanicLevel
  35. case "panic":
  36. return zap.PanicLevel
  37. case "fatal":
  38. return zap.FatalLevel
  39. default:
  40. panic(fmt.Sprintf("unknown level %q", lvl))
  41. }
  42. }
  43. // ConvertToCapnslogLogLevel convert log level string to capnslog.LogLevel.
  44. // TODO: deprecate this in 3.5
  45. func ConvertToCapnslogLogLevel(lvl string) capnslog.LogLevel {
  46. switch lvl {
  47. case "debug":
  48. return capnslog.DEBUG
  49. case "info":
  50. return capnslog.INFO
  51. case "warn":
  52. return capnslog.WARNING
  53. case "error":
  54. return capnslog.ERROR
  55. case "dpanic":
  56. return capnslog.CRITICAL
  57. case "panic":
  58. return capnslog.CRITICAL
  59. case "fatal":
  60. return capnslog.CRITICAL
  61. default:
  62. panic(fmt.Sprintf("unknown level %q", lvl))
  63. }
  64. }