1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-11-03 04:27:16 -05:00

Implemented log file

This commit is contained in:
マリウス 2022-12-31 13:00:32 -05:00
parent a516b08d56
commit 2a272a7976
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
2 changed files with 42 additions and 5 deletions

View File

@ -1,6 +1,10 @@
package cmd
import (
"net/url"
"os"
"runtime"
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/config"
"github.com/mrusme/gobbs/system"
@ -29,6 +33,34 @@ func init() {
)
}
func loadLogger(filename string, debug bool) (*zap.Logger, error) {
if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
}
var cfg zap.Config
if debug {
cfg = zap.NewDevelopmentConfig()
} else {
cfg = zap.NewProductionConfig()
}
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{
"stdout",
"winfile:///" + filename,
}
} else {
cfg.OutputPaths = []string{
filename,
}
}
return cfg.Build()
}
func load() {
var err error
var logger *zap.Logger
@ -38,10 +70,9 @@ func load() {
panic(err)
}
if CFG.Debug == "true" {
logger, _ = zap.NewDevelopment()
} else {
logger, _ = zap.NewProduction()
logger, err = loadLogger(CFG.Log, CFG.Debug)
if err != nil {
panic(err)
}
defer logger.Sync()
LOG = logger.Sugar()

View File

@ -25,7 +25,8 @@ type SystemConfig struct {
}
type Config struct {
Debug string
Debug bool
Log string
Systems []SystemConfig
}
@ -39,8 +40,13 @@ func Load() (Config, error) {
if err != nil {
return Config{}, err
}
cacheDir, err := os.UserCacheDir()
if err != nil {
return Config{}, err
}
viper.SetDefault("Debug", "true")
viper.SetDefault("Log", path.Join(cacheDir, "gobbs.log"))
viper.SetConfigName("gobbs")
viper.SetConfigType("toml")