Make logging to file optional
This commit is contained in:
parent
6449a300a4
commit
6e1095019f
4 changed files with 27 additions and 16 deletions
3
TODO.md
3
TODO.md
|
@ -1,8 +1,7 @@
|
||||||
# TODO
|
# TODO
|
||||||
List of things to add to this project
|
List of things to add to this project
|
||||||
|
|
||||||
* Make scram configurable
|
* Improve config and use reflection when loading it
|
||||||
* Use reflection in config loading
|
|
||||||
* Check export function for improvements
|
* Check export function for improvements
|
||||||
* Add notes to frontend?
|
* Add notes to frontend?
|
||||||
* More slog.Debug and a debug flag?
|
* More slog.Debug and a debug flag?
|
||||||
|
|
15
config.go
15
config.go
|
@ -14,10 +14,11 @@ import (
|
||||||
var ConfigFile = "config/config.txt"
|
var ConfigFile = "config/config.txt"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Username string `config:"username"`
|
Username string `config:"username"`
|
||||||
Password string `config:"password"`
|
Password string `config:"password"`
|
||||||
Port int `config:"port"`
|
Port int `config:"port"`
|
||||||
Scram bool `config:"enable_scram"`
|
LogToFile bool `config:"log_to_file"`
|
||||||
|
Scram bool `config:"enable_scram"`
|
||||||
|
|
||||||
TelegramToken string `config:"tg_token"`
|
TelegramToken string `config:"tg_token"`
|
||||||
TelegramChat string `config:"tg_chat"`
|
TelegramChat string `config:"tg_chat"`
|
||||||
|
@ -87,6 +88,12 @@ func (c *Config) Reload() error {
|
||||||
} else {
|
} else {
|
||||||
c.Scram = false
|
c.Scram = false
|
||||||
}
|
}
|
||||||
|
} else if key == "log_to_file" {
|
||||||
|
if value == "true" {
|
||||||
|
c.LogToFile = true
|
||||||
|
} else {
|
||||||
|
c.LogToFile = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
username=admin
|
username=admin
|
||||||
password=admin
|
password=admin
|
||||||
port=7101
|
port=7101
|
||||||
|
log_to_file=false
|
||||||
enable_scram=false
|
enable_scram=false
|
||||||
|
|
24
logger.go
24
logger.go
|
@ -10,18 +10,22 @@ import (
|
||||||
|
|
||||||
var LogFile = "config/log.txt"
|
var LogFile = "config/log.txt"
|
||||||
|
|
||||||
// LogInit makes slog output to both stdout and a file
|
// LogInit makes slog output to both stdout and a file if needed
|
||||||
func LogInit() {
|
func LogInit() {
|
||||||
f, err := os.OpenFile(LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
var w io.Writer
|
||||||
if err != nil {
|
if Cfg.LogToFile {
|
||||||
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil))) // Fallback to stdout
|
f, err := os.OpenFile(LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
slog.Error("error opening log file, logging to stdout", "path", LogFile, "error", err)
|
if err != nil {
|
||||||
return
|
slog.Error("error opening log file, logging to stdout", "path", LogFile, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// No defer f.Close() because that breaks the MultiWriter
|
||||||
|
w = io.MultiWriter(f, os.Stdout)
|
||||||
|
} else {
|
||||||
|
w = os.Stdout
|
||||||
}
|
}
|
||||||
// No defer f.Close() because that breaks the MultiWriter
|
|
||||||
w := io.MultiWriter(f, os.Stdout)
|
|
||||||
slog.SetDefault(slog.New(slog.NewTextHandler(w, nil)))
|
|
||||||
|
|
||||||
// Make chi log to file too
|
// Make slog and chi use intended format
|
||||||
|
slog.SetDefault(slog.New(slog.NewTextHandler(w, nil)))
|
||||||
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true})
|
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue