forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
34 lines (28 loc) · 897 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package audit
import (
"os"
"github.com/mattermost/logr"
"github.com/mattermost/logr/target"
)
type FileOptions target.FileOptions
// NewFileTarget creates a target capable of outputting log records to a rotated file.
func NewFileTarget(filter logr.Filter, formatter logr.Formatter, opts FileOptions, maxQSize int) (*target.File, error) {
fopts := target.FileOptions(opts)
err := checkFileWritable(fopts.Filename)
if err != nil {
return nil, err
}
target := target.NewFileTarget(filter, formatter, fopts, maxQSize)
return target, nil
}
func checkFileWritable(filename string) error {
// try opening/creating the file for writing
file, err := os.OpenFile(filename, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return err
}
file.Close()
return nil
}