Selaa lähdekoodia

get logs into dashboard module

Jordan Schalm 8 vuotta sitten
vanhempi
commit
7ad2f0ce86
4 muutettua tiedostoa jossa 60 lisäystä ja 3 poistoa
  1. 5 1
      cmd/guerrillad/serve.go
  2. 2 2
      dashboard/dashboard.go
  3. 48 0
      dashboard/datastore.go
  4. 5 0
      server.go

+ 5 - 1
cmd/guerrillad/serve.go

@@ -18,6 +18,8 @@ import (
 
 	"github.com/flashmob/go-guerrilla"
 	"github.com/flashmob/go-guerrilla/backends"
+	"github.com/flashmob/go-guerrilla/dashboard"
+	// "github.com/flashmob/go-guerrilla/dashboard"
 )
 
 var (
@@ -42,6 +44,8 @@ func init() {
 		"/var/run/go-guerrilla.pid", "Path to the pid file")
 
 	rootCmd.AddCommand(serveCmd)
+
+	log.AddHook(dashboard.NewLogHook())
 }
 
 func sigHandler(app guerrilla.Guerrilla) {
@@ -86,7 +90,7 @@ func serve(cmd *cobra.Command, args []string) {
 			maxClients += s.MaxClients
 		}
 		if maxClients > fileLimit {
-			log.Fatalf("Combined max clients for all servers (%d) is greater than open file limit (%d). "+
+			log.Warnf("Combined max clients for all servers (%d) is greater than open file limit (%d). "+
 				"Please increase your open file limit or decrease max clients.", maxClients, fileLimit)
 		}
 	}

+ 2 - 2
dashboard/dashboard.go

@@ -13,8 +13,8 @@ import (
 const (
 	dashboard      = "index.html"
 	login          = "login.html"
-	dashboardPath  = "html/index.html"
-	loginPath      = "html/login.html"
+	dashboardPath  = "dashboard/html/index.html"
+	loginPath      = "dashboard/html/login.html"
 	sessionTimeout = time.Hour * 24 // TODO replace with config
 )
 

+ 48 - 0
dashboard/datastore.go

@@ -3,6 +3,8 @@ package dashboard
 import (
 	"runtime"
 	"time"
+
+	log "github.com/Sirupsen/logrus"
 )
 
 const (
@@ -64,3 +66,49 @@ func ramListener(interval time.Duration, store *dataStore) {
 		store.addPoint(&point{t, memStats.Alloc})
 	}
 }
+
+type SendEvent struct {
+	timeStamp     time.Time
+	helo          string
+	remoteAddress string
+}
+
+type LogHook struct {
+	events chan *SendEvent
+}
+
+func NewLogHook() *LogHook {
+	events := make(chan *SendEvent)
+	return &LogHook{events}
+}
+
+func (h *LogHook) Levels() []log.Level {
+	return []log.Level{log.InfoLevel}
+}
+
+func (h *LogHook) Fire(e *log.Entry) error {
+	// helo, ok := e.Data["helo"]
+	// if !ok {
+	// 	return nil
+	// }
+	// heloStr, ok := helo.(string)
+	// if !ok {
+	// 	return nil
+	// }
+	//
+	// addr, ok := e.Data["remoteAddress"]
+	// if !ok {
+	// 	return nil
+	// }
+	// addrStr, ok := addr.(string)
+	// if !ok {
+	// 	return nil
+	// }
+	//
+	// h.events <- &SendEvent{
+	// 	timeStamp:     e.Time,
+	// 	helo:          heloStr,
+	// 	remoteAddress: addrStr,
+	// }
+	return nil
+}

+ 5 - 0
server.go

@@ -395,6 +395,11 @@ func (server *server) handleClient(client *client) {
 			res := server.backend.Process(client.Envelope)
 			if res.Code() < 300 {
 				client.messagesSent++
+				log.WithFields(map[string]interface{}{
+					"helo":          client.Helo,
+					"remoteAddress": client.RemoteAddress,
+					"success":       true,
+				}).Info("Received message")
 			}
 			client.responseAdd(res.String())
 			client.state = ClientCmd