Add space after prefix if one doesn't exist

This commit is contained in:
Christian Rocha 2021-02-01 15:19:50 -05:00
parent ef8a6895a1
commit 479a1ceb35
1 changed files with 11 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package tea
import ( import (
"log" "log"
"os" "os"
"unicode"
) )
// LogToFile sets up default logging to log to a file. This is helpful as we // LogToFile sets up default logging to log to a file. This is helpful as we
@ -23,6 +24,16 @@ func LogToFile(path string, prefix string) (*os.File, error) {
return nil, err return nil, err
} }
log.SetOutput(f) log.SetOutput(f)
// Add a space after the prefix if a prefix is being specified and it
// doesn't already have a trailing space.
if len(prefix) > 0 {
finalChar := prefix[len(prefix)-1]
if unicode.IsSpace(rune(finalChar)) {
prefix += " "
}
}
log.SetPrefix(prefix) log.SetPrefix(prefix)
return f, nil return f, nil
} }