]> git.r.bdr.sh - rbdr/mobius/commitdiff
Clean up logging
authorJeff Halter <redacted>
Sun, 21 Jul 2024 19:47:18 +0000 (12:47 -0700)
committerJeff Halter <redacted>
Sun, 21 Jul 2024 19:47:18 +0000 (12:47 -0700)
cmd/mobius-hotline-server/main.go
hotline/file_transfer.go
hotline/server.go
internal/mobius/logger.go [new file with mode: 0644]
internal/mobius/transaction_handlers.go

index 33ba7c2125155d511b507d1a34aa86674310db79..6b82764280ba1b480f6947502bffb5d24d8922b0 100644 (file)
@@ -7,10 +7,8 @@ import (
        "fmt"
        "github.com/jhalter/mobius/hotline"
        "github.com/jhalter/mobius/internal/mobius"
        "fmt"
        "github.com/jhalter/mobius/hotline"
        "github.com/jhalter/mobius/internal/mobius"
-       "gopkg.in/natefinch/lumberjack.v2"
        "io"
        "log"
        "io"
        "log"
-       "log/slog"
        "os"
        "os/signal"
        "path"
        "os"
        "os/signal"
        "path"
@@ -21,12 +19,6 @@ import (
 //go:embed mobius/config
 var cfgTemplate embed.FS
 
 //go:embed mobius/config
 var cfgTemplate embed.FS
 
-var logLevels = map[string]slog.Level{
-       "debug": slog.LevelDebug,
-       "info":  slog.LevelInfo,
-       "error": slog.LevelError,
-}
-
 // Values swapped in by go-releaser at build time
 var (
        version = "dev"
 // Values swapped in by go-releaser at build time
 var (
        version = "dev"
@@ -52,21 +44,11 @@ func main() {
        flag.Parse()
 
        if *printVersion {
        flag.Parse()
 
        if *printVersion {
-               fmt.Printf("mobius-hotline-server %s, commit %s, built at %s", version, commit, date)
+               fmt.Printf("mobius-hotline-server %s, commit %s, built on %s\n", version, commit, date)
                os.Exit(0)
        }
 
                os.Exit(0)
        }
 
-       slogger := slog.New(
-               slog.NewTextHandler(
-                       io.MultiWriter(os.Stdout, &lumberjack.Logger{
-                               Filename:   *logFile,
-                               MaxSize:    100, // MB
-                               MaxBackups: 3,
-                               MaxAge:     365, // days
-                       }),
-                       &slog.HandlerOptions{Level: logLevels[*logLevel]},
-               ),
-       )
+       slogger := mobius.NewLogger(logLevel, logFile)
 
        // It's important for Windows compatibility to use path.Join and not filepath.Join for the config dir initialization.
        // https://github.com/golang/go/issues/44305
 
        // It's important for Windows compatibility to use path.Join and not filepath.Join for the config dir initialization.
        // https://github.com/golang/go/issues/44305
@@ -181,12 +163,7 @@ func main() {
                }
        }()
 
                }
        }()
 
-       slogger.Info("Hotline server started",
-               "version", version,
-               "config", *configDir,
-               "API port", fmt.Sprintf("%s:%v", *netInterface, *basePort),
-               "Transfer port", fmt.Sprintf("%s:%v", *netInterface, *basePort+1),
-       )
+       slogger.Info("Hotline server started", "version", version, "config", *configDir)
 
        // Assign functions to handle specific Hotline transaction types
        mobius.RegisterHandlers(srv)
 
        // Assign functions to handle specific Hotline transaction types
        mobius.RegisterHandlers(srv)
index 12725db620779d7ba4e1f197ba2683c960986dc7..847c7feedb4defa7c4eae7234f7d1c05741349e4 100644 (file)
@@ -421,7 +421,6 @@ func DownloadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *Fil
                }
 
                subPath := path[basePathLen+1:]
                }
 
                subPath := path[basePathLen+1:]
-               rLogger.Debug("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir())
 
                if i == 1 {
                        return nil
 
                if i == 1 {
                        return nil
@@ -437,8 +436,6 @@ func DownloadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *Fil
                        return err
                }
 
                        return err
                }
 
-               rLogger.Debug("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
-
                var dataOffset int64
 
                switch nextAction[1] {
                var dataOffset int64
 
                switch nextAction[1] {
index c4d8f7f429d22bf27bbd4cca397a56d9b712ce9c..5531bd3c59512916f927c705cd0ea4dc74d0a631 100644 (file)
@@ -400,7 +400,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                        return err
                }
 
                        return err
                }
 
-               c.Logger.Info("Login failed", "clientVersion", fmt.Sprintf("%x", c.Version))
+               c.Logger.Info("Incorrect login")
 
                return nil
        }
 
                return nil
        }
diff --git a/internal/mobius/logger.go b/internal/mobius/logger.go
new file mode 100644 (file)
index 0000000..909e6ab
--- /dev/null
@@ -0,0 +1,44 @@
+package mobius
+
+import (
+       "gopkg.in/natefinch/lumberjack.v2"
+       "io"
+       "log/slog"
+       "os"
+       "time"
+)
+
+const (
+       logMaxSize    = 100 // MB
+       logMaxBackups = 3
+       logMaxAge     = 365 // days
+)
+
+var logLevels = map[string]slog.Level{
+       "debug": slog.LevelDebug,
+       "info":  slog.LevelInfo,
+       "error": slog.LevelError,
+}
+
+func NewLogger(logLevel, logFile *string) *slog.Logger {
+       return slog.New(
+               slog.NewTextHandler(
+                       io.MultiWriter(os.Stdout, &lumberjack.Logger{
+                               Filename:   *logFile,
+                               MaxSize:    logMaxSize,
+                               MaxBackups: logMaxBackups,
+                               MaxAge:     logMaxAge,
+                       }),
+                       &slog.HandlerOptions{
+                               Level: logLevels[*logLevel],
+                               ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
+                                       if a.Key == slog.TimeKey {
+                                               // Remove the milliseconds from the time field to save a few columns.
+                                               a.Value = slog.StringValue(a.Value.Time().Format(time.RFC3339))
+                                       }
+                                       return a
+                               },
+                       },
+               ),
+       )
+}
index 759ee66ed20db7a72f0c94f62ec84c348e52018c..3253ff247a58d5285df4625e94c7aa4da6197b5f 100644 (file)
@@ -1772,8 +1772,6 @@ func HandleMakeAlias(cc *hotline.ClientConn, t *hotline.Transaction) (res []hotl
                return res
        }
 
                return res
        }
 
-       cc.Logger.Debug("Make alias", "src", fullFilePath, "dst", fullNewFilePath)
-
        if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
                return cc.NewErrReply(t, "Error creating alias")
        }
        if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil {
                return cc.NewErrReply(t, "Error creating alias")
        }