-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse.go
More file actions
66 lines (59 loc) · 1.63 KB
/
Copy pathparse.go
File metadata and controls
66 lines (59 loc) · 1.63 KB
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
package hyperfleetlogger
import (
"fmt"
"io"
"log/slog"
"os"
"strings"
)
const (
levelDebug = "debug"
levelInfo = "info"
levelWarn = "warn"
levelWarning = "warning"
levelError = "error"
formatJSON = "json"
formatText = "text"
outputStdout = "stdout"
outputStderr = "stderr"
)
// ParseLevel converts a level string to slog.Level.
// Accepts debug, info, warn, warning, error (case-insensitive).
func ParseLevel(s string) (slog.Level, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case levelDebug:
return slog.LevelDebug, nil
case levelInfo, "":
return slog.LevelInfo, nil
case levelWarn, levelWarning:
return slog.LevelWarn, nil
case levelError:
return slog.LevelError, nil
default:
return slog.LevelInfo, fmt.Errorf("unknown log level: %q (valid: debug, info, warn, warning, error)", s)
}
}
// ParseFormat converts a format string to a Format value.
// Accepts json, text (case-insensitive). Empty defaults to JSON.
func ParseFormat(s string) (Format, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case formatJSON, "":
return FormatJSON, nil
case formatText:
return FormatText, nil
default:
return FormatJSON, fmt.Errorf("unknown log format: %q (valid: json, text)", s)
}
}
// ParseOutput converts an output string to an io.Writer.
// Accepts stdout, stderr (case-insensitive). Empty defaults to stdout.
func ParseOutput(s string) (io.Writer, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "", outputStdout:
return os.Stdout, nil
case outputStderr:
return os.Stderr, nil
default:
return os.Stdout, fmt.Errorf("unknown log output: %q (valid: stdout, stderr)", s)
}
}