feat: LogToFileWith (#692)

Allows to log to file with custom loggers, provided they implement
SetOutput and SetPrefix.
This commit is contained in:
Carlos Alexandro Becker 2023-03-09 11:46:44 -03:00 committed by GitHub
parent de6740db2e
commit adb0065256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package tea package tea
import ( import (
"io"
"log" "log"
"os" "os"
"unicode" "unicode"
@ -19,6 +20,18 @@ import (
// } // }
// defer f.Close() // defer f.Close()
func LogToFile(path string, prefix string) (*os.File, error) { 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) f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil { if err != nil {
return nil, err return nil, err