Skip to content

Commit

Permalink
feat: LogToFileWith
Browse files Browse the repository at this point in the history
Allows to log to file with custom loggers, provided they implement
SetOutput and SetPrefix.
  • Loading branch information
caarlos0 committed Mar 9, 2023
1 parent de6740d commit 5d6d2f1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tea

import (
"io"
"log"
"os"
"unicode"
Expand All @@ -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
Expand Down

0 comments on commit 5d6d2f1

Please sign in to comment.