Skip to content

Commit

Permalink
Merge pull request #15 from nkryuchkov/feature/discord-start-stop-logs
Browse files Browse the repository at this point in the history
Add Discord start/stop logs
  • Loading branch information
nkryuchkov authored Oct 8, 2020
2 parents 34b362c + 53ee4fb commit acbe6d7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
23 changes: 22 additions & 1 deletion cmd/dmsg-discovery/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"log"
"net/http"
"os"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/skycoin/dmsg/cmd/dmsg-discovery/internal/api"
"github.com/skycoin/dmsg/cmd/dmsg-discovery/internal/store"
"github.com/skycoin/dmsg/cmdutil"
"github.com/skycoin/dmsg/discord"
)

const redisPasswordEnvName = "REDIS_PASSWORD"
Expand Down Expand Up @@ -44,14 +46,33 @@ var rootCmd = &cobra.Command{

log := sf.Logger()

if discordWebhookURL := discord.GetWebhookURLFromEnv(); discordWebhookURL != "" {
// Workaround for Discord logger hook. Actually, it's Info.
log.Error(discord.StartLogMessage)
defer log.Error(discord.StopLogMessage)
} else {
log.Info(discord.StartLogMessage)
defer log.Info(discord.StopLogMessage)
}

m := sf.HTTPMetrics()

db := prepareDB(log)

a := api.New(log, db, testMode)

ctx, cancel := cmdutil.SignalContext(context.Background(), log)
defer cancel()

log.WithField("addr", addr).Info("Serving discovery API...")
log.Fatal(http.ListenAndServe(addr, m.Handle(a)))
go func() {
if err := http.ListenAndServe(addr, m.Handle(a)); err != nil {
log.Errorf("ListenAndServe: %v", err)
cancel()
}
}()

<-ctx.Done()
},
}

Expand Down
29 changes: 24 additions & 5 deletions cmd/dmsg-server/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"log"
"net"
"net/http"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/cmdutil"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/dmsg/discord"
"github.com/skycoin/dmsg/promutil"
"github.com/skycoin/dmsg/servermetrics"
)
Expand All @@ -30,10 +32,19 @@ var rootCmd = &cobra.Command{
Short: "Dmsg Server for Skywire.",
PreRunE: func(cmd *cobra.Command, args []string) error { return sf.Check() },
Run: func(_ *cobra.Command, args []string) {
if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
}

log := sf.Logger()

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.WithError(err).Warn("Failed to output build info.")
if discordWebhookURL := discord.GetWebhookURLFromEnv(); discordWebhookURL != "" {
// Workaround for Discord logger hook. Actually, it's Info.
log.Error(discord.StartLogMessage)
defer log.Error(discord.StopLogMessage)
} else {
log.Info(discord.StartLogMessage)
defer log.Info(discord.StopLogMessage)
}

var conf Config
Expand All @@ -57,9 +68,17 @@ var rootCmd = &cobra.Command{

defer func() { log.WithError(srv.Close()).Info("Closed server.") }()

if err := srv.Serve(lis, conf.PublicAddress); err != nil {
log.Fatal(err)
}
ctx, cancel := cmdutil.SignalContext(context.Background(), log)
defer cancel()

go func() {
if err := srv.Serve(lis, conf.PublicAddress); err != nil {
log.Errorf("Serve: %v", err)
cancel()
}
}()

<-ctx.Done()
},
}

Expand Down
23 changes: 21 additions & 2 deletions discord/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import (
"github.com/sirupsen/logrus"
)

const webhookURLEnvName = "DISCORD_WEBHOOK_URL"

const (
loggedLevel = logrus.ErrorLevel
startStopLogLevel = logrus.InfoLevel
)

const (
webhookURLEnvName = "DISCORD_WEBHOOK_URL"
// StartLogMessage defines a message on binary starting.
StartLogMessage = "Starting"
// StopLogMessage defines a message on binary stopping.
StopLogMessage = "Stopping"
)

// Hook is a Discord logger hook.
Expand All @@ -32,7 +42,7 @@ func WithLimit(limit time.Duration) Option {

// NewHook returns a new Hook.
func NewHook(tag, webHookURL string, opts ...Option) logrus.Hook {
parent := discordrus.NewHook(webHookURL, logrus.ErrorLevel, discordOpts(tag))
parent := discordrus.NewHook(webHookURL, loggedLevel, discordOpts(tag))

hook := &Hook{
Hook: parent,
Expand All @@ -47,6 +57,15 @@ func NewHook(tag, webHookURL string, opts ...Option) logrus.Hook {

// Fire checks whether rate is fine and fires the underlying hook.
func (h *Hook) Fire(entry *logrus.Entry) error {
switch entry.Message {
case StartLogMessage, StopLogMessage:
// Start and stop messages should be logged by Hook but they should have Info level.
// With Info level, they would not be passed to hook.
// So we can use Error level in the codebase and change level to Info in the hook,
// then it appears as Info in logs.
entry.Level = startStopLogLevel
}

if h.shouldFire(entry) {
return h.Hook.Fire(entry)
}
Expand Down

0 comments on commit acbe6d7

Please sign in to comment.