logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package pack
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "strings"
  9. "github.com/buildpacks/pack/pkg/logging"
  10. )
  11. type packLogger struct {
  12. outDiscard *log.Logger
  13. outStderr *log.Logger
  14. safeWriter *safeWriter
  15. }
  16. // Replicate the exact behavior of https://github.com/buildpacks/pack/blob/main/pkg/logging/logger_simple.go
  17. func newPackLogger() logging.Logger {
  18. discard := log.New(ioutil.Discard, "", log.LstdFlags|log.Lmicroseconds)
  19. stderr := log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds)
  20. return &packLogger{
  21. outDiscard: discard,
  22. outStderr: stderr,
  23. safeWriter: &safeWriter{discard, stderr},
  24. }
  25. }
  26. const (
  27. debugPrefix = "DEBUG:"
  28. infoPrefix = "INFO:"
  29. warnPrefix = "WARN:"
  30. errorPrefix = "ERROR:"
  31. prefixFmt = "%-7s %s"
  32. )
  33. func (l *packLogger) Debug(msg string) {
  34. l.outStderr.Printf(prefixFmt, debugPrefix, msg)
  35. }
  36. func (l *packLogger) Debugf(format string, v ...interface{}) {
  37. // We do not want to print the environment variables for now as they might
  38. // contain sensitive information like client IDs and secrets
  39. // Refer: https://github.com/buildpacks/pack/blob/main/internal/builder/builder.go#L349
  40. if strings.HasPrefix(format, "Provided Environment Variables") {
  41. return
  42. }
  43. // We do not print the registry auth credentials -- this should also be treated as sensitive information
  44. if strings.Contains(fmt.Sprintf(format, v...), "CNB_REGISTRY_AUTH") {
  45. return
  46. }
  47. l.outStderr.Printf(prefixFmt, debugPrefix, fmt.Sprintf(format, v...))
  48. }
  49. func (l *packLogger) Info(msg string) {
  50. l.outStderr.Printf(prefixFmt, infoPrefix, msg)
  51. }
  52. func (l *packLogger) Infof(format string, v ...interface{}) {
  53. l.outStderr.Printf(prefixFmt, infoPrefix, fmt.Sprintf(format, v...))
  54. }
  55. func (l *packLogger) Warn(msg string) {
  56. l.outStderr.Printf(prefixFmt, warnPrefix, msg)
  57. }
  58. func (l *packLogger) Warnf(format string, v ...interface{}) {
  59. l.outStderr.Printf(prefixFmt, warnPrefix, fmt.Sprintf(format, v...))
  60. }
  61. func (l *packLogger) Error(msg string) {
  62. l.outStderr.Printf(prefixFmt, errorPrefix, msg)
  63. }
  64. func (l *packLogger) Errorf(format string, v ...interface{}) {
  65. l.outStderr.Printf(prefixFmt, errorPrefix, fmt.Sprintf(format, v...))
  66. }
  67. type safeWriter struct {
  68. outDiscard *log.Logger
  69. outStderr *log.Logger
  70. }
  71. func (s *safeWriter) Write(p []byte) (n int, err error) {
  72. if strings.Contains(string(p), "Unable to delete previous cache image") {
  73. return s.outDiscard.Writer().Write(p)
  74. }
  75. return s.outStderr.Writer().Write(p)
  76. }
  77. func (l *packLogger) Writer() io.Writer {
  78. return l.safeWriter
  79. }
  80. func (l *packLogger) IsVerbose() bool {
  81. return false
  82. }