fixed logs and config locations, updated connection closure handling
This commit is contained in:
@@ -3,6 +3,7 @@ package logger
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"homestead/homestead_gateway/util"
|
||||
"homestead/homestead_gateway/util/config"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -23,6 +24,7 @@ func New(id string, cfg config.LogConfig) (*slog.Logger, func() error, error) {
|
||||
cfg.Rotation = 7
|
||||
}
|
||||
|
||||
cfg.Directory = util.NormalizeLogPath(cfg.Directory)
|
||||
console := slog.NewTextHandler(&prefixWriter{inner: os.Stderr, prefix: []byte("[" + id + "] "), startLine: true}, &slog.HandlerOptions{AddSource: true, Level: cfg.Level})
|
||||
router := newFileRouter(cfg.Directory, cfg.Rotation, id)
|
||||
root := slogmulti.Fanout(console, router)
|
||||
|
||||
51
util/util.go
Normal file
51
util/util.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func GetPath() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
exe, err = filepath.EvalSymlinks(exe)
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
return filepath.Dir(exe)
|
||||
}
|
||||
|
||||
func NormalizePath(path string) string {
|
||||
return filepath.Clean(filepath.FromSlash(path))
|
||||
}
|
||||
|
||||
func NormalizeLogPath(path string) string {
|
||||
if filepath.IsAbs(path) {
|
||||
return path
|
||||
}
|
||||
|
||||
return NormalizePath(filepath.Join(GetPath(), path))
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
func CloseConnWithControlMessage(conn *websocket.Conn, typ int, text string) {
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(time.Second))
|
||||
_ = conn.WriteControl(
|
||||
typ, websocket.FormatCloseMessage(typ, text), time.Now().Add(time.Second),
|
||||
)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
func CloseConn(conn *websocket.Conn) {
|
||||
CloseConnWithControlMessage(
|
||||
conn, websocket.CloseNormalClosure,
|
||||
"Disconnecting.",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user