From adb0065256ced56090d0f9854aa094b3dabea7ac Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 9 Mar 2023 11:46:44 -0300 Subject: [PATCH] feat: LogToFileWith (#692) Allows to log to file with custom loggers, provided they implement SetOutput and SetPrefix. --- logging.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/logging.go b/logging.go index 97ffcb7..59258d4 100644 --- a/logging.go +++ b/logging.go @@ -1,6 +1,7 @@ package tea import ( + "io" "log" "os" "unicode" @@ -19,6 +20,18 @@ import ( // } // defer f.Close() func LogToFile(path string, prefix string) (*os.File, error) { + return LogToFileWith(path, prefix, log.Default()) +} + +// LogOptionsSetter is an interface implemented by stdlib's log and charm's log +// libraries. +type LogOptionsSetter interface { + SetOutput(io.Writer) + SetPrefix(string) +} + +// LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter. +func LogToFileWith(path string, prefix string, log LogOptionsSetter) (*os.File, error) { f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644) if err != nil { return nil, err