"fmt"
"github.com/jhalter/mobius/hotline"
"github.com/jhalter/mobius/internal/mobius"
- "gopkg.in/natefinch/lumberjack.v2"
"io"
"log"
- "log/slog"
"os"
"os/signal"
"path"
//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"
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)
}
- 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
}
}()
- 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)
}
subPath := path[basePathLen+1:]
- rLogger.Debug("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir())
if i == 1 {
return nil
return err
}
- rLogger.Debug("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
-
var dataOffset int64
switch nextAction[1] {
--- /dev/null
+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
+ },
+ },
+ ),
+ )
+}