logger.go 2.7 KB

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