1
0

customlogger.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package logx
  2. import (
  3. "fmt"
  4. "io"
  5. "time"
  6. "github.com/tal-tech/go-zero/core/timex"
  7. )
  8. const customCallerDepth = 3
  9. type customLog logEntry
  10. func WithDuration(d time.Duration) Logger {
  11. return customLog{
  12. Duration: timex.ReprOfDuration(d),
  13. }
  14. }
  15. func (l customLog) Error(v ...interface{}) {
  16. if shouldLog(ErrorLevel) {
  17. l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), customCallerDepth))
  18. }
  19. }
  20. func (l customLog) Errorf(format string, v ...interface{}) {
  21. if shouldLog(ErrorLevel) {
  22. l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), customCallerDepth))
  23. }
  24. }
  25. func (l customLog) Info(v ...interface{}) {
  26. if shouldLog(InfoLevel) {
  27. l.write(infoLog, levelInfo, fmt.Sprint(v...))
  28. }
  29. }
  30. func (l customLog) Infof(format string, v ...interface{}) {
  31. if shouldLog(InfoLevel) {
  32. l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
  33. }
  34. }
  35. func (l customLog) Slow(v ...interface{}) {
  36. if shouldLog(ErrorLevel) {
  37. l.write(slowLog, levelSlow, fmt.Sprint(v...))
  38. }
  39. }
  40. func (l customLog) Slowf(format string, v ...interface{}) {
  41. if shouldLog(ErrorLevel) {
  42. l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
  43. }
  44. }
  45. func (l customLog) write(writer io.Writer, level, content string) {
  46. l.Timestamp = getTimestamp()
  47. l.Level = level
  48. l.Content = content
  49. outputJson(writer, logEntry(l))
  50. }