Improve logging

This commit is contained in:
Torjus Håkestad 2022-09-28 12:28:15 +02:00
parent 837f89e5d3
commit 8084362d22
No known key found for this signature in database
GPG Key ID: C6FD38B820D295FC

View File

@ -57,17 +57,17 @@ func ActionServe(c *cli.Context) error {
// Setup logging
loggers := setupLoggers(cfg)
loggers.rootLogger.Infow("Starting apiary", "version", apiary.FullVersion())
loggers.rootLogger.Infow("Starting apiary.", "version", apiary.FullVersion())
// Setup store
var s store.LoginAttemptStore
switch cfg.Store.Type {
case "MEMORY", "memory":
loggers.rootLogger.Infow("Initialized store", "store_type", "memory")
loggers.rootLogger.Infow("Initialized store.", "store_type", "memory")
s = &store.MemoryStore{}
case "POSTGRES", "postgres":
pgStartTime := time.Now()
loggers.rootLogger.Debugw("Initializing store", "store_type", "postgres")
loggers.rootLogger.Debugw("Initializing store.", "store_type", "postgres")
pgStore, err := store.NewPostgresStore(cfg.Store.Postgres.DSN)
if err != nil {
return err
@ -75,33 +75,33 @@ func ActionServe(c *cli.Context) error {
if err := pgStore.InitDB(); err != nil {
return err
}
loggers.rootLogger.Infow("Initialized store", "store_type", "postgres", "init_time", time.Since(pgStartTime))
loggers.rootLogger.Infow("Initialized store.", "store_type", "postgres", "init_time", time.Since(pgStartTime))
if cfg.Store.EnableCache {
loggers.rootLogger.Debugw("Initializing store", "store_type", "cache-postgres")
loggers.rootLogger.Debugw("Initializing store.", "store_type", "cache-postgres")
startTime := time.Now()
cachingStore := store.NewCachingStore(pgStore)
s = cachingStore
loggers.rootLogger.Infow("Initialized store", "store_type", "cache-postgres", "init_time", time.Since(startTime))
loggers.rootLogger.Infow("Initialized store.", "store_type", "cache-postgres", "init_time", time.Since(startTime))
} else {
s = pgStore
}
case "bolt", "BOLT":
boltStartTime := time.Now()
loggers.rootLogger.Debugw("Initializing store", "store_type", "bolt")
loggers.rootLogger.Debugw("Initializing store.", "store_type", "bolt")
boltStore, err := store.NewBBoltStore(cfg.Store.Bolt.DBPath)
if err != nil {
return err
}
defer boltStore.Close()
loggers.rootLogger.Infow("Initialized store", "store_type", "bolt", "init_time", time.Since(boltStartTime))
loggers.rootLogger.Infow("Initialized store.", "store_type", "bolt", "init_time", time.Since(boltStartTime))
if cfg.Store.EnableCache {
loggers.rootLogger.Debugw("Initializing store", "store_type", "cache-bolt")
loggers.rootLogger.Debugw("Initializing store.", "store_type", "cache-bolt")
startTime := time.Now()
cachingStore := store.NewCachingStore(boltStore)
s = cachingStore
loggers.rootLogger.Infow("Initialized store", "store_type", "cache-bolt", "init_time", time.Since(startTime))
loggers.rootLogger.Infow("Initialized store.", "store_type", "cache-bolt", "init_time", time.Since(startTime))
} else {
s = boltStore
}
@ -160,9 +160,9 @@ func ActionServe(c *cli.Context) error {
}
go func() {
loggers.rootLogger.Info("Starting ports server")
loggers.rootLogger.Info("Starting ports server.")
if err := portsServer.Start(portsCtx); err != nil {
loggers.rootLogger.Warnw("Ports server returned error", "err", err)
loggers.rootLogger.Warnw("Ports server returned error.", "err", err)
}
}()
}
@ -171,7 +171,7 @@ func ActionServe(c *cli.Context) error {
if cfg.SMTP.Enable {
honeypot, err := smtp.NewSMTPHoneypot()
if err != nil {
loggers.rootLogger.Warnw("Error seting up SMTP honeypot", "error", err)
loggers.rootLogger.Warnw("Error seting up SMTP honeypot.", "error", err)
}
honeypot.Addr = cfg.SMTP.Addr
honeypot.Logger = loggers.smtpLogger
@ -183,43 +183,44 @@ func ActionServe(c *cli.Context) error {
// Start smtp honeypot
go func() {
loggers.rootLogger.Info("Starting SMTP server")
loggers.rootLogger.Info("Starting SMTP server.")
if err := honeypot.ListenAndServe(); err != nil {
loggers.rootLogger.Warnw("SMTP server returned error", "error", err)
loggers.rootLogger.Warnw("SMTP server returned error.", "error", err)
}
}()
// Wait for smtp shutdown
go func() {
<-serversCtx.Done()
loggers.rootLogger.Info("SMTP server shutdown started")
loggers.rootLogger.Info("SMTP server shutdown started.")
if err := honeypot.Shutdown(); err != nil {
loggers.rootLogger.Errorw("Error shutting down SMTP server", "error", err)
loggers.rootLogger.Errorw("Error shutting down SMTP server.", "error", err)
}
loggers.rootLogger.Info("SMTP server shutdown complete")
loggers.rootLogger.Info("SMTP server shutdown complete.")
}()
}
// Handle interrupt
go func() {
<-interruptChan
loggers.rootLogger.Info("Interrupt received, shutting down")
loggers.rootLogger.Info("Interrupt received, shutting down.")
serversCancel()
}()
// Start ssh server
go func() {
loggers.rootLogger.Info("Starting SSH server")
loggers.rootLogger.Info("Starting SSH server.")
if err := hs.ListenAndServe(); err != nil && err != sshlib.ErrServerClosed {
loggers.rootLogger.Warnw("SSH server returned error", "error", err)
loggers.rootLogger.Warnw("SSH server returned error.", "error", err)
}
loggers.rootLogger.Infow("SSH server stopped.")
}()
// Start web server
go func() {
loggers.rootLogger.Info("Starting web server")
loggers.rootLogger.Info("Starting web server.")
if err := web.StartServe(); err != nil && err != http.ErrServerClosed {
loggers.rootLogger.Warnw("Web server returned error", "error", err)
loggers.rootLogger.Warnw("Web server returned error.", "error", err)
}
}()
@ -283,21 +284,21 @@ func ActionServe(c *cli.Context) error {
// Stop SSH server
sshShutdownCtx, sshShutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer sshShutdownCancel()
loggers.rootLogger.Info("SSH server shutdown started")
loggers.rootLogger.Info("SSH server shutdown started.")
if err := hs.Shutdown(sshShutdownCtx); err != nil {
loggers.rootLogger.Infow("Error shutting down SSH server", "error", err)
loggers.rootLogger.Infow("Error shutting down SSH server.", "error", err)
}
loggers.rootLogger.Info("SSH server shutdown complete")
loggers.rootLogger.Info("SSH server shutdown complete.")
// Stop Web server
webShutdownCtx, webShutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer webShutdownCancel()
loggers.rootLogger.Info("Web server shutdown started")
loggers.rootLogger.Info("Web server shutdown started.")
if err := web.Shutdown(webShutdownCtx); err != nil {
loggers.rootLogger.Infow("Error shutting down web server", "error", err)
loggers.rootLogger.Infow("Error shutting down web server.", "error", err)
}
loggers.rootLogger.Info("Web server shutdown complete")
loggers.rootLogger.Info("Web server shutdown complete.")
rootCancel()
}()