This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
121 lines (103 loc) · 2.98 KB
/
config.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package rec
import (
"fmt"
"os"
"time"
)
const (
defaultHostnameFieldValue = "localhost"
defaultCallerSkip = 4
defaultLineSeparator = "\n"
)
// Config is configuration struct for *rec.Logger.
type Config struct {
// [timestamp] Set true if you want to output the timestamp field in the log.
UseTimestampField bool
// [timestamp] Set the key name in the timestamp field.
TimestampFieldKey string
// [timestamp] Set the Go time format for timestamp field.
TimestampFieldFormat string
// [severity] Set true if you want to output the severity field in the log.
UseSeverityField bool
// [severity] Set the key name in the severity field.
SeverityFieldKey string
// [severity] Set the severity of the log output.
SeverityThreshold Severity
// [severity] Set true if you want to output severity in uppercase.
UseUppercaseSeverity bool
// [severity] default severity for io.Writer
DefaultSeverity Severity
// [hostname] Set true if you want to output the hostname field in the log.
UseHostnameField bool
// [hostname] Set the key name in the hostname field.
HostnameFieldKey string
// [hostname] Set the value in the hostname field.
HostnameFieldValue string
// [caller]
UseCallerField bool
// [caller]
CallerFieldKey string
// [caller]
CallerSkip int
// [caller]
UseShortCaller bool
// [message]
UseMessageField bool
// [message]
MessageFieldKey string
// [lineseparator]
LineSeparator string
}
// NewConfig returns *rec.Config that set default values.
func NewConfig() *Config {
return newConfig(os.Hostname)
}
func newConfig(osHostname func() (string, error)) *Config {
config := &Config{
// "timestamp":"...",
UseTimestampField: true,
TimestampFieldKey: "timestamp",
TimestampFieldFormat: time.RFC3339Nano,
// "severity":"...",
UseSeverityField: true,
SeverityFieldKey: "severity",
SeverityThreshold: DEFAULT,
UseUppercaseSeverity: true,
DefaultSeverity: DEFAULT,
// "hostname":"...",
UseHostnameField: false,
HostnameFieldKey: "hostname",
HostnameFieldValue: defaultHostnameFieldValue,
// "caller":"...",
UseCallerField: true,
CallerFieldKey: "caller",
CallerSkip: defaultCallerSkip,
UseShortCaller: true,
// "message":"...",
UseMessageField: true,
MessageFieldKey: "message",
// \n
LineSeparator: defaultLineSeparator,
}
var err error
config.HostnameFieldValue, err = osHostname()
if err != nil {
config.HostnameFieldValue = defaultHostnameFieldValue
}
return config
}
func (c *Config) validate() error {
if c.UseSeverityField && c.SeverityFieldKey == "" {
return fmt.Errorf("*Config.SeverityFieldKey %w", ErrIsEmpty)
}
if c.UseTimestampField && c.TimestampFieldKey == "" {
return fmt.Errorf("*Config.TimestampFieldKey %w", ErrIsEmpty)
}
if c.UseCallerField && c.CallerFieldKey == "" {
return fmt.Errorf("*Config.CallerFieldKey %w", ErrIsEmpty)
}
if c.UseMessageField && c.MessageFieldKey == "" {
return fmt.Errorf("*Config.MessageFieldKey %w", ErrIsEmpty)
}
return nil
}