validateLogFileOutput ensures the given file has permission to be written to.
(logFileOutput string)
| 302 | |
| 303 | // validateLogFileOutput ensures the given file has permission to be written to. |
| 304 | func validateLogFileOutput(logFileOutput string) error { |
| 305 | if logFileOutput == "" { |
| 306 | return fmt.Errorf("empty log file output") |
| 307 | } |
| 308 | |
| 309 | // Validate containing directory |
| 310 | logFileOutputDirectory := filepath.Dir(logFileOutput) |
| 311 | // Make full directory structure if it doesn't already exist |
| 312 | err := os.MkdirAll(logFileOutputDirectory, 0700) |
| 313 | if err != nil { |
| 314 | return errors.Wrap(err, ErrInvalidLogFilePath.Error()) |
| 315 | } |
| 316 | |
| 317 | // Ensure LogFilePath is a directory. We'll check file permissions when it opens/creates the logfile. |
| 318 | if f, err := os.Stat(logFileOutputDirectory); err != nil { |
| 319 | return errors.Wrap(err, ErrInvalidLogFilePath.Error()) |
| 320 | } else if !f.IsDir() { |
| 321 | return errors.Wrap(ErrInvalidLogFilePath, "not a directory") |
| 322 | } |
| 323 | |
| 324 | // Validate given file is writeable by touching it. |
| 325 | // If the file does not exist, this will open a zero sized log file intentionally to set permissions. |
| 326 | // The permissions will then be used by lumberjack. Otherwise, lumberjack will use 0600 permissions. |
| 327 | file, err := os.OpenFile(logFileOutput, os.O_WRONLY|os.O_CREATE, logFilePermission) |
| 328 | if err != nil { |
| 329 | if os.IsPermission(err) { |
| 330 | return errors.Wrap(err, "invalid file output") |
| 331 | } |
| 332 | return err |
| 333 | } |
| 334 | |
| 335 | return file.Close() |
| 336 | } |
| 337 | |
| 338 | // nilAllNonConsoleLoggers will nil out all loggers except the console logger. |
| 339 | func nilAllNonConsoleLoggers() { |