LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter.
(path string, prefix string, log LogOptionsSetter)
| 33 | |
| 34 | // LogToFileWith does allows to call LogToFile with a custom LogOptionsSetter. |
| 35 | func 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 | } |