Browse Source

fix statik build
inject dashboard hook to logger form the guerrilla package

flashmob 8 years ago
parent
commit
abf5280212
3 changed files with 23 additions and 4 deletions
  1. 1 1
      dashboard/dashboard.go
  2. 11 1
      guerrilla.go
  3. 11 2
      log/log.go

+ 1 - 1
dashboard/dashboard.go

@@ -7,7 +7,7 @@ import (
 	"time"
 	"time"
 
 
 	log "github.com/Sirupsen/logrus"
 	log "github.com/Sirupsen/logrus"
-	// "github.com/flashmob/go-guerrilla/dashboard/statik"
+	_ "github.com/flashmob/go-guerrilla/dashboard/statik"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/websocket"
 	"github.com/gorilla/websocket"
 	"github.com/rakyll/statik/fs"
 	"github.com/rakyll/statik/fs"

+ 11 - 1
guerrilla.go

@@ -90,6 +90,10 @@ func New(ac *AppConfig, b backends.Backend, l log.Logger) (Guerrilla, error) {
 	if ac.LogLevel != "" {
 	if ac.LogLevel != "" {
 		if h, ok := l.(*log.HookedLogger); ok {
 		if h, ok := l.(*log.HookedLogger); ok {
 			if h, err := log.GetLogger(h.GetLogDest(), ac.LogLevel); err == nil {
 			if h, err := log.GetLogger(h.GetLogDest(), ac.LogLevel); err == nil {
+				// add the dashboard hook
+				if ac.Dashboard.Enabled {
+					h.AddHook(dashboard.LogHook)
+				}
 				g.setMainlog(h)
 				g.setMainlog(h)
 			}
 			}
 		}
 		}
@@ -211,6 +215,9 @@ func (g *guerrilla) subscribeEvents() {
 		var err error
 		var err error
 		var l log.Logger
 		var l log.Logger
 		if l, err = log.GetLogger(c.LogFile, c.LogLevel); err == nil {
 		if l, err = log.GetLogger(c.LogFile, c.LogLevel); err == nil {
+			if c.Dashboard.Enabled {
+				l.AddHook(dashboard.LogHook)
+			}
 			g.setMainlog(l)
 			g.setMainlog(l)
 			g.mapServers(func(server *server) {
 			g.mapServers(func(server *server) {
 				// it will change server's logger when the next client gets accepted
 				// it will change server's logger when the next client gets accepted
@@ -233,7 +240,10 @@ func (g *guerrilla) subscribeEvents() {
 	g.Subscribe(EventConfigLogLevel, func(c *AppConfig) {
 	g.Subscribe(EventConfigLogLevel, func(c *AppConfig) {
 		l, err := log.GetLogger(g.mainlog().GetLogDest(), c.LogLevel)
 		l, err := log.GetLogger(g.mainlog().GetLogDest(), c.LogLevel)
 		if err == nil {
 		if err == nil {
-			g.logStore.Store(l)
+			if c.Dashboard.Enabled {
+				l.AddHook(dashboard.LogHook)
+			}
+			g.setMainlog(l)
 			g.mapServers(func(server *server) {
 			g.mapServers(func(server *server) {
 				server.logStore.Store(l)
 				server.logStore.Store(l)
 			})
 			})

+ 11 - 2
log/log.go

@@ -65,7 +65,6 @@ type Logger interface {
 // Implements the Logger interface
 // Implements the Logger interface
 // It's a logrus logger wrapper that contains an instance of our LoggerHook
 // It's a logrus logger wrapper that contains an instance of our LoggerHook
 type HookedLogger struct {
 type HookedLogger struct {
-
 	// satisfy the log.FieldLogger interface
 	// satisfy the log.FieldLogger interface
 	*log.Logger
 	*log.Logger
 
 
@@ -75,6 +74,8 @@ type HookedLogger struct {
 	dest string
 	dest string
 
 
 	oo OutputOption
 	oo OutputOption
+
+	addHooks map[log.Hook]bool
 }
 }
 
 
 type loggerKey struct {
 type loggerKey struct {
@@ -174,8 +175,16 @@ func newLogrus(o OutputOption, level string) (*log.Logger, error) {
 }
 }
 
 
 // AddHook adds a new logrus hook
 // AddHook adds a new logrus hook
+// ensures same hook can't be added more than once
 func (l *HookedLogger) AddHook(h log.Hook) {
 func (l *HookedLogger) AddHook(h log.Hook) {
-	log.AddHook(h)
+	if l.addHooks == nil {
+		l.addHooks = make(map[log.Hook]bool, 0)
+	}
+	if ok := l.addHooks[h]; ok {
+		return
+	}
+	l.Hooks.Add(h)
+	l.addHooks[h] = true
 }
 }
 
 
 func (l *HookedLogger) IsDebug() bool {
 func (l *HookedLogger) IsDebug() bool {