logging.go

 1package tea
 2
 3import (
 4	"fmt"
 5	"io"
 6	"log"
 7	"os"
 8	"unicode"
 9)
10
11// LogToFile sets up default logging to log to a file. This is helpful as we
12// can't print to the terminal since our TUI is occupying it. If the file
13// doesn't exist it will be created.
14//
15// Don't forget to close the file when you're done with it.
16//
17//	  f, err := LogToFile("debug.log", "debug")
18//	  if err != nil {
19//			fmt.Println("fatal:", err)
20//			os.Exit(1)
21//	  }
22//	  defer f.Close()
23func LogToFile(path string, prefix string) (*os.File, error) {
24	return LogToFileWith(path, prefix, log.Default())
25}
26
27// LogOptionsSetter is an interface implemented by stdlib's log and charm's log
28// libraries.
29type LogOptionsSetter interface {
30	SetOutput(io.Writer)
31	SetPrefix(string)
32}
33
34// LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter.
35func LogToFileWith(path string, prefix string, log LogOptionsSetter) (*os.File, error) {
36	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) //nolint:mnd
37	if err != nil {
38		return nil, fmt.Errorf("error opening file for logging: %w", err)
39	}
40	log.SetOutput(f)
41
42	// Add a space after the prefix if a prefix is being specified and it
43	// doesn't already have a trailing space.
44	if len(prefix) > 0 {
45		finalChar := prefix[len(prefix)-1]
46		if !unicode.IsSpace(rune(finalChar)) {
47			prefix += " "
48		}
49	}
50	log.SetPrefix(prefix)
51
52	return f, nil
53}