zap.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "sort"
  17. "go.uber.org/zap"
  18. "go.uber.org/zap/zapcore"
  19. )
  20. // DefaultZapLoggerConfig defines default zap logger configuration.
  21. var DefaultZapLoggerConfig = zap.Config{
  22. Level: zap.NewAtomicLevelAt(ConvertToZapLevel(DefaultLogLevel)),
  23. Development: false,
  24. Sampling: &zap.SamplingConfig{
  25. Initial: 100,
  26. Thereafter: 100,
  27. },
  28. Encoding: "json",
  29. // copied from "zap.NewProductionEncoderConfig" with some updates
  30. EncoderConfig: zapcore.EncoderConfig{
  31. TimeKey: "ts",
  32. LevelKey: "level",
  33. NameKey: "logger",
  34. CallerKey: "caller",
  35. MessageKey: "msg",
  36. StacktraceKey: "stacktrace",
  37. LineEnding: zapcore.DefaultLineEnding,
  38. EncodeLevel: zapcore.LowercaseLevelEncoder,
  39. EncodeTime: zapcore.ISO8601TimeEncoder,
  40. EncodeDuration: zapcore.StringDurationEncoder,
  41. EncodeCaller: zapcore.ShortCallerEncoder,
  42. },
  43. // Use "/dev/null" to discard all
  44. OutputPaths: []string{"stderr"},
  45. ErrorOutputPaths: []string{"stderr"},
  46. }
  47. // MergeOutputPaths merges logging output paths, resolving conflicts.
  48. func MergeOutputPaths(cfg zap.Config) zap.Config {
  49. outputs := make(map[string]struct{})
  50. for _, v := range cfg.OutputPaths {
  51. outputs[v] = struct{}{}
  52. }
  53. outputSlice := make([]string, 0)
  54. if _, ok := outputs["/dev/null"]; ok {
  55. // "/dev/null" to discard all
  56. outputSlice = []string{"/dev/null"}
  57. } else {
  58. for k := range outputs {
  59. outputSlice = append(outputSlice, k)
  60. }
  61. }
  62. cfg.OutputPaths = outputSlice
  63. sort.Strings(cfg.OutputPaths)
  64. errOutputs := make(map[string]struct{})
  65. for _, v := range cfg.ErrorOutputPaths {
  66. errOutputs[v] = struct{}{}
  67. }
  68. errOutputSlice := make([]string, 0)
  69. if _, ok := errOutputs["/dev/null"]; ok {
  70. // "/dev/null" to discard all
  71. errOutputSlice = []string{"/dev/null"}
  72. } else {
  73. for k := range errOutputs {
  74. errOutputSlice = append(errOutputSlice, k)
  75. }
  76. }
  77. cfg.ErrorOutputPaths = errOutputSlice
  78. sort.Strings(cfg.ErrorOutputPaths)
  79. return cfg
  80. }