diff --git a/main.go b/main.go index fb99c1f..0e0b25b 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "beelzebub/parser" "beelzebub/protocols" + "beelzebub/tracer" log "github.com/sirupsen/logrus" "io" "os" @@ -31,8 +32,7 @@ func main() { hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{} // Protocol manager - manager := protocols.ProtocolManager{} - serviceManager := manager.InitServiceManager() + serviceManager := protocols.InitProtocolManager(logStrategy, hypertextTransferProtocolStrategy) for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration { switch beelzebubServiceConfiguration.Protocol { @@ -54,15 +54,20 @@ func main() { } <-quit } +func logStrategy(event tracer.Event) { + log.WithFields(log.Fields{ + "status": event.Status.String(), + "event": event, + }).Info("New Event") +} func configureLogging(configurations parser.Logging) *os.File { file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) if err != nil { log.Fatalf("error opening file: %v", err) } - mw := io.MultiWriter(os.Stdout, file) - log.SetOutput(mw) + log.SetOutput(io.MultiWriter(os.Stdout, file)) log.SetFormatter(&log.JSONFormatter{ DisableTimestamp: configurations.LogDisableTimestamp, diff --git a/protocols/protocolManager.go b/protocols/protocolManager.go index a9b0566..1d5c029 100644 --- a/protocols/protocolManager.go +++ b/protocols/protocolManager.go @@ -5,14 +5,19 @@ import ( "beelzebub/tracer" ) +type ServiceStrategy interface { + Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error +} + type ProtocolManager struct { strategy ServiceStrategy tracer *tracer.Tracer } -func (pm *ProtocolManager) InitServiceManager() *ProtocolManager { +func InitProtocolManager(tracerStrategy tracer.Strategy, strategy ServiceStrategy) *ProtocolManager { return &ProtocolManager{ - tracer: tracer.Init(), + tracer: tracer.Init(tracerStrategy), + strategy: strategy, } } diff --git a/protocols/protocolStrategy.go b/protocols/protocolStrategy.go deleted file mode 100644 index 29da640..0000000 --- a/protocols/protocolStrategy.go +++ /dev/null @@ -1,10 +0,0 @@ -package protocols - -import ( - "beelzebub/parser" - "beelzebub/tracer" -) - -type ServiceStrategy interface { - Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error -} diff --git a/tracer/tracer.go b/tracer/tracer.go index 27ec9e0..9978c55 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -1,22 +1,23 @@ package tracer import ( - log "github.com/sirupsen/logrus" "net/http" ) +type Strategy func(event Event) + type Tracer struct { + strategy Strategy } -func Init() *Tracer { - return &Tracer{} +func Init(strategy Strategy) *Tracer { + return &Tracer{ + strategy: strategy, + } } func (tracer *Tracer) TraceEvent(event Event) { - log.WithFields(log.Fields{ - "status": event.Status.String(), - "event": event, - }).Info("New Event") + tracer.strategy(event) } type Event struct {