LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter.
(path string, prefix string, log LogOptionsSetter)
| 34 | |
| 35 | // LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter. |
| 36 | func LogToFileWith(path string, prefix string, log LogOptionsSetter) (*os.File, error) { |
| 37 | f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600) //nolint:mnd |
| 38 | if err != nil { |
| 39 | return nil, fmt.Errorf("error opening file for logging: %w", err) |
| 40 | } |
| 41 | log.SetOutput(f) |
| 42 | |
| 43 | // Add a space after the prefix if a prefix is being specified and it |
| 44 | // doesn't already have a trailing space. |
| 45 | if len(prefix) > 0 { |
| 46 | finalRune, _ := utf8.DecodeLastRuneInString(prefix) |
| 47 | if !unicode.IsSpace(finalRune) { |
| 48 | prefix += " " |
| 49 | } |
| 50 | } |
| 51 | log.SetPrefix(prefix) |
| 52 | |
| 53 | return f, nil |
| 54 | } |