Skip to content
Myr

logging.go

This file

Path

logging.go

53 lines · 1.4 KiB · Go

Last changed by

ci(lint): fix all lint issues on `main` (#1342)Andrey Neringbefore adoptionb224818

History of this file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–35why, one step back
1package tea
23import (4 "fmt"5 "io"6 "log"7 "os"8 "unicode"9)1011// LogToFile sets up default logging to log to a file. This is helpful as we12// can't print to the terminal since our TUI is occupying it. If the file13// 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}2627// LogOptionsSetter is an interface implemented by stdlib's log and charm's log28// libraries.29type LogOptionsSetter interface {30 SetOutput(io.Writer)31 SetPrefix(string)32}3334// LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter.35func LogToFileWith(path string, prefix string, log LogOptionsSetter) (*os.File, error) {

ci(lint): fix all lint issues on `main` (#1342)Andrey Neringbefore adoptionb224818

line 36why, one step back
36 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) //nolint:mnd

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 37–53why, one step back
37 if err != nil {
38 return nil, fmt.Errorf("error opening file for logging: %w", err)39 }40 log.SetOutput(f)4142 // Add a space after the prefix if a prefix is being specified and it43 // 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)5152 return f, nil53}