adept/config/log.go
Gregory Ballantine 674327e0cf
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Added a multi-writer so that log messages go to both the console and log file
2022-09-03 10:51:25 -04:00

33 lines
680 B
Go

package config
import (
"io"
"log"
"os"
"github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/util"
)
func InitLogging() *os.File {
var fileHandle *os.File = nil
if viper.GetBool("log_to_file") {
// open a file
var err error
fileHandle, err = os.OpenFile(util.ResolveTilde(viper.GetString("log_file")), os.O_APPEND | os.O_CREATE | os.O_RDWR, 0644)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
os.Exit(1)
}
}
// create a MultiWriter instance so we can write to both console AND file
mw := io.MultiWriter(os.Stdout, fileHandle)
// set our multiwriter object as the output for logging
log.SetOutput(mw)
return fileHandle
}