NewRotatingFile creates a new rotating file writer.
(path string, opts ...Option)
| 39 | |
| 40 | // NewRotatingFile creates a new rotating file writer. |
| 41 | func NewRotatingFile(path string, opts ...Option) (*RotatingFile, error) { |
| 42 | r := &RotatingFile{ |
| 43 | path: path, |
| 44 | maxSize: DefaultMaxSize, |
| 45 | maxBackups: DefaultMaxBackups, |
| 46 | } |
| 47 | |
| 48 | for _, opt := range opts { |
| 49 | opt(r) |
| 50 | } |
| 51 | |
| 52 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | if err := r.openFile(); err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | return r, nil |
| 61 | } |
| 62 | |
| 63 | func (r *RotatingFile) openFile() error { |
| 64 | file, err := os.OpenFile(r.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) |