-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambient_logger.go
64 lines (57 loc) · 1.81 KB
/
ambient_logger.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
package ambient
import (
"os"
"strings"
)
// AppLogger represents the log service for the app.
type AppLogger interface {
Logger
// Fatal is reserved for the app level only.
Fatal(format string, v ...interface{})
SetLogLevel(level LogLevel)
Named(name string) AppLogger
Name() string
}
// Logger represents the log service for the plugins.
type Logger interface {
Log(level LogLevel, format string, v ...interface{})
Debug(format string, v ...interface{})
Info(format string, v ...interface{})
Warn(format string, v ...interface{})
Error(format string, v ...interface{})
}
// LogLevel is a log level.
type LogLevel int
const (
// LogLevelDebug is for debugging output. It's very verbose.
LogLevelDebug LogLevel = iota
// LogLevelInfo is for informational messages. It shows messages on services
// starting, stopping, and users logging in.
LogLevelInfo
// LogLevelWarn is for behavior that may need to be fixed. It shows
// permission warnings for plugins.
LogLevelWarn
// LogLevelError is for messages when something is wrong with the
// app and it needs to be corrected.
LogLevelError
// LogLevelFatal is for messages when the app cannot continue and
// will halt.
LogLevelFatal
)
// EnvLogLevel returns the log level from the AMB_LOGLEVEL environment variable.
func EnvLogLevel() LogLevel {
ll := os.Getenv("AMB_LOGLEVEL")
switch true {
case strings.EqualFold(ll, "FATAL") || strings.EqualFold(ll, "4"):
return LogLevelFatal
case strings.EqualFold(ll, "ERROR") || strings.EqualFold(ll, "3"):
return LogLevelError
case strings.EqualFold(ll, "WARN") || strings.EqualFold(ll, "2"):
return LogLevelWarn
case strings.EqualFold(ll, "INFO") || strings.EqualFold(ll, "1"):
return LogLevelInfo
case strings.EqualFold(ll, "DEBUG") || strings.EqualFold(ll, "0"):
return LogLevelDebug
}
return LogLevelInfo
}