Setup logging to file.

This commit is contained in:
GRMrGecko 2025-01-07 22:21:50 -06:00
parent 0e11c52c80
commit 56c843cfc1
3 changed files with 27 additions and 2 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@ CHANGELOG.md
.cache .cache
virtual-vxlan.exe virtual-vxlan.exe
virtual-vxlan virtual-vxlan
virtual-vxlan.log
dist dist
sysroot/linux* sysroot/linux*

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io"
"net" "net"
"net/netip" "net/netip"
"os" "os"
@ -30,6 +31,7 @@ type Config struct {
type LogConfig struct { type LogConfig struct {
Level string `fig:"level" yaml:"level" enum:"debug,info,warn,error" default:"info"` Level string `fig:"level" yaml:"level" enum:"debug,info,warn,error" default:"info"`
Type string `fig:"type" yaml:"type" enum:"json,console" default:"console"` Type string `fig:"type" yaml:"type" enum:"json,console" default:"console"`
Path string `fig:"path" yaml:"path" default:""`
} }
// Configuration for updating. // Configuration for updating.
@ -80,7 +82,7 @@ type MACEntryConfig struct {
func (c *Config) ApplyFilters(configDir string) { func (c *Config) ApplyFilters(configDir string) {
// If the RPC path isn't set, set it to temp dir. // If the RPC path isn't set, set it to temp dir.
if c.RPCPath == "" { if c.RPCPath == "" {
c.RPCPath = filepath.Join(configDir, "virtual-vxlan.sock") c.RPCPath = filepath.Join(configDir, fmt.Sprintf("%s.sock", serviceName))
} }
// Check if the RPC socket already exists. // Check if the RPC socket already exists.
_, err := os.Stat(c.RPCPath) _, err := os.Stat(c.RPCPath)
@ -492,7 +494,9 @@ func SaveConfig() error {
return err return err
} }
// Apply log config.
func (l *LogConfig) Apply() { func (l *LogConfig) Apply() {
// Apply level.
switch l.Level { switch l.Level {
case "debug": case "debug":
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
@ -503,10 +507,30 @@ func (l *LogConfig) Apply() {
default: default:
log.SetLevel(log.ErrorLevel) log.SetLevel(log.ErrorLevel)
} }
// Apply type.
switch l.Type { switch l.Type {
case "json": case "json":
log.SetFormatter(&log.JSONFormatter{}) log.SetFormatter(&log.JSONFormatter{})
default: default:
log.SetFormatter(&log.TextFormatter{}) log.SetFormatter(&log.TextFormatter{})
} }
// If path isn't set, make it the executable location.
if l.Path == "" {
exe, err := os.Executable()
if err != nil {
panic(err)
}
l.Path = filepath.Join(filepath.Dir(exe), fmt.Sprintf("%s.log", serviceName))
}
// Set the log to save to the logpath.
f, err := os.OpenFile(l.Path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Println("Failed to open log file: %s %v", l.Path, err)
} else {
mw := io.MultiWriter(f, os.Stdout)
log.SetOutput(mw)
}
} }

View File

@ -6,7 +6,7 @@ const (
serviceDisplayName = "Virtual VXLAN" serviceDisplayName = "Virtual VXLAN"
serviceVendor = "com.mrgeckosmedia" serviceVendor = "com.mrgeckosmedia"
serviceDescription = "Virtual VXLAN using TUN interfaces" serviceDescription = "Virtual VXLAN using TUN interfaces"
serviceVersion = "0.1.4" serviceVersion = "0.1.5"
defaultConfigFile = "config.yaml" defaultConfigFile = "config.yaml"
) )