forked from Mirrors/bubbletea
feat: LogToFileWith (#692)
Allows to log to file with custom loggers, provided they implement SetOutput and SetPrefix.
This commit is contained in:
parent
de6740db2e
commit
adb0065256
13
logging.go
13
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
|
||||
|
|
Loading…
Reference in New Issue