-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.go
59 lines (50 loc) · 1.25 KB
/
logging.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2021 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package logging
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/whamcloud/logging/alert"
"github.com/whamcloud/logging/audit"
)
const (
// LogFileFlags are suitable for appending to a log file
LogFileFlags = os.O_CREATE | os.O_APPEND | os.O_RDWR
// LogFileMode is suitable for root-only log access
LogFileMode = 0600
)
// CreateWriter is a convenience function to ensure that the given input
// results in an io.Writer
func CreateWriter(w interface{}) (io.Writer, error) {
switch w := w.(type) {
case io.Writer:
return w, nil
case string:
switch strings.ToLower(w) {
case "stderr":
return os.Stderr, nil
case "stdout":
return os.Stdout, nil
case "":
return ioutil.Discard, nil
default:
return os.OpenFile(w, LogFileFlags, LogFileMode)
}
default:
return nil, fmt.Errorf("CreateWriter() called with unhandled input: %v", w)
}
}
// SetWriter sets up the writer for non-interactive logging libraries
func SetWriter(w interface{}) error {
writer, err := CreateWriter(w)
if err != nil {
return err
}
audit.SetOutput(writer)
alert.SetOutput(writer)
return nil
}