zap.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // AddOutputPaths adds output paths to the existing output paths, resolving conflicts.
  48. func AddOutputPaths(cfg zap.Config, outputPaths, errorOutputPaths []string) zap.Config {
  49. outputs := make(map[string]struct{})
  50. for _, v := range cfg.OutputPaths {
  51. outputs[v] = struct{}{}
  52. }
  53. for _, v := range outputPaths {
  54. outputs[v] = struct{}{}
  55. }
  56. outputSlice := make([]string, 0)
  57. if _, ok := outputs["/dev/null"]; ok {
  58. // "/dev/null" to discard all
  59. outputSlice = []string{"/dev/null"}
  60. } else {
  61. for k := range outputs {
  62. outputSlice = append(outputSlice, k)
  63. }
  64. }
  65. cfg.OutputPaths = outputSlice
  66. sort.Strings(cfg.OutputPaths)
  67. errOutputs := make(map[string]struct{})
  68. for _, v := range cfg.ErrorOutputPaths {
  69. errOutputs[v] = struct{}{}
  70. }
  71. for _, v := range errorOutputPaths {
  72. errOutputs[v] = struct{}{}
  73. }
  74. errOutputSlice := make([]string, 0)
  75. if _, ok := errOutputs["/dev/null"]; ok {
  76. // "/dev/null" to discard all
  77. errOutputSlice = []string{"/dev/null"}
  78. } else {
  79. for k := range errOutputs {
  80. errOutputSlice = append(errOutputSlice, k)
  81. }
  82. }
  83. cfg.ErrorOutputPaths = errOutputSlice
  84. sort.Strings(cfg.ErrorOutputPaths)
  85. return cfg
  86. }