From 9d175b5ed713b9ec58e94222826ef4c94c044a90 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 21 May 2020 16:56:47 -0400 Subject: [PATCH] Add ability to log directly to a file --- logging.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/logging.go b/logging.go index b17fa7f..8d0f18b 100644 --- a/logging.go +++ b/logging.go @@ -5,13 +5,35 @@ package boba import ( "log" "log/syslog" + "os" ) +// LogToFile sets up default logging to log to a file This is helpful as we +// can't print to the terminal since our TUI is occupying it. If the file +// doesn't exist it will be created. +// +// Don't forget to close the file when you're done with it. +// +// f, err := LogToFile("debug.log", "debug") +// if err != nil { +// fmt.Println("fatal:", err) +// os.Exit(1) +// } +// defer f.Close() +func LogToFile(path string, prefix string) (*os.File, error) { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return nil, err + } + log.SetOutput(f) + return f, nil +} + // UseSysLog sets up logging to log the system log. This becomes helpful when -// debugging since we can't easily print to the terminal since our TUI is -// occupying it! +// debugging since we can't print to the terminal since our TUI is occupying it. // // On macOS this is a just a matter of: tail -f /var/log/system.log +// On Linux this varies depending on distribution. func UseSysLog(programName string) error { l, err := syslog.New(syslog.LOG_NOTICE, programName) if err != nil {