terminal.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "sort"
  6. )
  7. type TerminalLogger struct {
  8. // fieldOut is used to do log.WithFields correctly
  9. fieldOut string
  10. }
  11. func (t TerminalLogger) log(args ...interface{}) {
  12. fmt.Print(args...)
  13. fmt.Print(t.fieldOut, "\n")
  14. t.fieldOut = ""
  15. }
  16. func (t TerminalLogger) logf(fmtString string, args ...interface{}) {
  17. fmt.Printf(fmtString, args...)
  18. fmt.Print(t.fieldOut, "\n")
  19. t.fieldOut = ""
  20. }
  21. func (t TerminalLogger) err(args ...interface{}) {
  22. fmt.Fprint(os.Stderr, args...)
  23. fmt.Fprint(os.Stderr, t.fieldOut, "\n")
  24. t.fieldOut = ""
  25. }
  26. func (t TerminalLogger) errf(fmtString string, args ...interface{}) {
  27. fmt.Fprintf(os.Stderr, fmtString, args...)
  28. fmt.Fprint(os.Stderr, t.fieldOut, "\n")
  29. t.fieldOut = ""
  30. }
  31. func (t TerminalLogger) Debug(args ...interface{}) {
  32. if isDebug() {
  33. t.log(args...)
  34. }
  35. }
  36. func (t TerminalLogger) Debugf(fmtString string, args ...interface{}) {
  37. if isDebug() {
  38. t.logf(fmtString, args...)
  39. }
  40. }
  41. func (t TerminalLogger) Error(args ...interface{}) {
  42. t.err(args...)
  43. }
  44. func (t TerminalLogger) Errorf(fmtString string, args ...interface{}) {
  45. t.errf(fmtString, args...)
  46. }
  47. func (t TerminalLogger) Errorln(args ...interface{}) {
  48. t.err(args...)
  49. }
  50. func (t TerminalLogger) Info(args ...interface{}) {
  51. t.log(args...)
  52. }
  53. func (t TerminalLogger) Infof(fmtString string, args ...interface{}) {
  54. t.logf(fmtString, args...)
  55. }
  56. func (t TerminalLogger) Infoln(args ...interface{}) {
  57. t.log(args...)
  58. }
  59. func (t TerminalLogger) Fatal(args ...interface{}) {
  60. t.err(args...)
  61. os.Exit(1)
  62. }
  63. func (t TerminalLogger) Fatalf(fmtString string, args ...interface{}) {
  64. t.errf(fmtString, args...)
  65. os.Exit(1)
  66. }
  67. func (t TerminalLogger) Print(args ...interface{}) {
  68. t.log(args...)
  69. }
  70. func (t TerminalLogger) Printf(fmtString string, args ...interface{}) {
  71. t.logf(fmtString, args...)
  72. }
  73. func (t TerminalLogger) Warn(args ...interface{}) {
  74. t.log(args...)
  75. }
  76. func (t TerminalLogger) Warnf(fmtString string, args ...interface{}) {
  77. t.logf(fmtString, args...)
  78. }
  79. func (t TerminalLogger) WithFields(fields Fields) Logger {
  80. // When the user calls WithFields, we make a string which gets appended
  81. // to the output of the final [Info|Warn|Error] call for the
  82. // descriptive fields. Because WithFields returns the proper Logger
  83. // (with the fieldOut string set correctly), the logrus syntax will
  84. // still work.
  85. kvpairs := []string{}
  86. // Why the string slice song and dance? Because Go's map iteration
  87. // order is random, we will get inconsistent results if we don't sort
  88. // the fields (or their resulting string K/V pairs, like we have here).
  89. // Otherwise, we couldn't test this reliably.
  90. for k, v := range fields {
  91. kvpairs = append(kvpairs, fmt.Sprintf("%s=%v", k, v))
  92. }
  93. sort.Strings(kvpairs)
  94. // TODO:
  95. // 1. Is this thread-safe?
  96. // 2. Add more tabs?
  97. t.fieldOut = "\t\t"
  98. for _, s := range kvpairs {
  99. // TODO: Is %v the correct format string here?
  100. t.fieldOut = fmt.Sprintf("%s %s", t.fieldOut, s)
  101. }
  102. return t
  103. }