logger.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package logger
  14. import (
  15. "fmt"
  16. "go.uber.org/zap"
  17. "go.uber.org/zap/zapcore"
  18. "github.com/ipfs/go-log"
  19. )
  20. var _ log.StandardLogger = &Logger{}
  21. type Logger struct {
  22. level log.LogLevel
  23. zap *zap.SugaredLogger
  24. }
  25. func New(lvl log.LogLevel) *Logger {
  26. cfg := zap.Config{
  27. Encoding: "json",
  28. OutputPaths: []string{"stdout"},
  29. ErrorOutputPaths: []string{"stderr"},
  30. Level: zap.NewAtomicLevelAt(zapcore.Level(lvl)),
  31. EncoderConfig: zapcore.EncoderConfig{
  32. MessageKey: "message",
  33. LevelKey: "level",
  34. EncodeLevel: zapcore.CapitalLevelEncoder,
  35. TimeKey: "time",
  36. EncodeTime: zapcore.ISO8601TimeEncoder,
  37. CallerKey: "caller",
  38. EncodeCaller: zapcore.ShortCallerEncoder,
  39. },
  40. }
  41. logger, err := cfg.Build(zap.AddCallerSkip(1))
  42. if err != nil {
  43. panic(err)
  44. }
  45. defer logger.Sync()
  46. sugar := logger.Sugar()
  47. return &Logger{level: lvl, zap: sugar}
  48. }
  49. func joinMsg(args ...interface{}) (message string) {
  50. for _, m := range args {
  51. message += " " + fmt.Sprintf("%v", m)
  52. }
  53. return
  54. }
  55. func (l Logger) Debug(args ...interface{}) {
  56. l.zap.Debug(joinMsg(args...))
  57. }
  58. func (l Logger) Debugf(f string, args ...interface{}) {
  59. l.zap.Debugf(f+"\n", args...)
  60. }
  61. func (l Logger) Error(args ...interface{}) {
  62. l.zap.Error(joinMsg(args...))
  63. }
  64. func (l Logger) Errorf(f string, args ...interface{}) {
  65. l.zap.Errorf(f+"\n", args...)
  66. }
  67. func (l Logger) Fatal(args ...interface{}) {
  68. l.zap.Fatal(joinMsg(args...))
  69. }
  70. func (l Logger) Fatalf(f string, args ...interface{}) {
  71. l.zap.Fatalf(f+"\n", args...)
  72. }
  73. func (l Logger) Info(args ...interface{}) {
  74. l.zap.Info(joinMsg(args...))
  75. }
  76. func (l Logger) Infof(f string, args ...interface{}) {
  77. l.zap.Infof(f+"\n", args...)
  78. }
  79. func (l Logger) Panic(args ...interface{}) {
  80. l.Fatal(args...)
  81. }
  82. func (l Logger) Panicf(f string, args ...interface{}) {
  83. l.Fatalf(f, args...)
  84. }
  85. func (l Logger) Warn(args ...interface{}) {
  86. l.zap.Warn(joinMsg(args...))
  87. }
  88. func (l Logger) Warnf(f string, args ...interface{}) {
  89. l.zap.Warnf(f+"\n", args...)
  90. }
  91. func (l Logger) Warning(args ...interface{}) {
  92. l.Warn(args...)
  93. }
  94. func (l Logger) Warningf(f string, args ...interface{}) {
  95. l.Warnf(f, args...)
  96. }