mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 00:33:14 +00:00
option to log to a file
This commit is contained in:
parent
37ed856d8b
commit
9d701678e1
18
src/main.go
18
src/main.go
@ -26,10 +26,7 @@ func opt(envVar, defaultValue string) string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||
|
||||
var addr, db, authfile, certfile, keyfile, basepath string
|
||||
var addr, db, authfile, certfile, keyfile, basepath, logfile string
|
||||
var ver, open bool
|
||||
flag.StringVar(&addr, "addr", opt("YARR_ADDR", "127.0.0.1:7070"), "address to run server on")
|
||||
flag.StringVar(&authfile, "auth-file", opt("YARR_AUTH_FILE", ""), "path to a file containing username:password")
|
||||
@ -37,6 +34,7 @@ func main() {
|
||||
flag.StringVar(&certfile, "cert-file", opt("YARR_CERT_FILE", ""), "path to cert file for https")
|
||||
flag.StringVar(&keyfile, "key-file", opt("YARR_KEY_FILE", ""), "path to key file for https")
|
||||
flag.StringVar(&db, "db", opt("YARR_DB", ""), "storage file path")
|
||||
flag.StringVar(&logfile, "log-file", opt("YARR_LOG_FILE", ""), "path to log file to use instead of stdout")
|
||||
flag.BoolVar(&ver, "version", false, "print application version")
|
||||
flag.BoolVar(&open, "open", false, "open the server in browser")
|
||||
flag.Parse()
|
||||
@ -46,6 +44,18 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||
if logfile != "" {
|
||||
file, err := os.OpenFile(logfile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to setup log file: ", err)
|
||||
}
|
||||
defer file.Close()
|
||||
log.SetOutput(file)
|
||||
} else {
|
||||
log.SetOutput(os.Stdout)
|
||||
}
|
||||
|
||||
configPath, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to get config dir: ", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user