Bläddra i källkod

Define logging interface and basic logger

Signed-off-by: Robert Lin <robertlin1@gmail.com>
Robert Lin 6 år sedan
förälder
incheckning
c94746a4ee
2 ändrade filer med 58 tillägg och 0 borttagningar
  1. 51 0
      internal/core/logger.go
  2. 7 0
      internal/core/pipeline.go

+ 51 - 0
internal/core/logger.go

@@ -0,0 +1,51 @@
+package core
+
+import (
+	"log"
+	"os"
+)
+
+// Logger defines the output interface used by Hercules components.
+type Logger interface {
+	Info(...interface{})
+	Infof(string, ...interface{})
+	Warn(...interface{})
+	Warnf(string, ...interface{})
+	Error(...interface{})
+	Errorf(string, ...interface{})
+}
+
+// DefaultLogger is the default logger used by a pipeline, and wraps the standard
+// log library.
+type DefaultLogger struct {
+	I *log.Logger
+	W *log.Logger
+	E *log.Logger
+}
+
+// NewLogger returns a configured default logger.
+func NewLogger() *DefaultLogger {
+	return &DefaultLogger{
+		I: log.New(os.Stdout, "[INFO] ", log.LstdFlags),
+		W: log.New(os.Stdout, "[WARN] ", log.LstdFlags),
+		E: log.New(os.Stderr, "[ERROR] ", log.LstdFlags),
+	}
+}
+
+// Info writes to info logger
+func (d *DefaultLogger) Info(v ...interface{}) { d.I.Print(v...) }
+
+// Infof writes to info logger
+func (d *DefaultLogger) Infof(f string, v ...interface{}) { d.I.Printf(f, v...) }
+
+// Warn writes to the warning logger
+func (d *DefaultLogger) Warn(v ...interface{}) { d.W.Print(v...) }
+
+// Warnf writes to the warning logger
+func (d *DefaultLogger) Warnf(f string, v ...interface{}) { d.W.Printf(f, v...) }
+
+// Error writes to the error logger
+func (d *DefaultLogger) Error(v ...interface{}) { d.E.Print(v...) }
+
+// Errorf writes to the error logger
+func (d *DefaultLogger) Errorf(f string, v ...interface{}) { d.E.Printf(f, v...) }

+ 7 - 0
internal/core/pipeline.go

@@ -273,6 +273,9 @@ type Pipeline struct {
 
 	// Feature flags which enable the corresponding items.
 	features map[string]bool
+
+	// The logger for printing output.
+	l Logger
 }
 
 const (
@@ -318,9 +321,13 @@ func NewPipeline(repository *git.Repository) *Pipeline {
 		items:      []PipelineItem{},
 		facts:      map[string]interface{}{},
 		features:   map[string]bool{},
+		l:          NewLogger(),
 	}
 }
 
+// SetLogger updates the pipeline's logger.
+func (pipeline *Pipeline) SetLogger(l Logger) { pipeline.l = l }
+
 // GetFact returns the value of the fact with the specified name.
 func (pipeline *Pipeline) GetFact(name string) interface{} {
 	return pipeline.facts[name]