forked from yuval-k/harbor
-
Notifications
You must be signed in to change notification settings - Fork 4
/
known_loggers.go
110 lines (92 loc) · 2.43 KB
/
known_loggers.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
package logger
import (
"github.com/goharbor/harbor/src/jobservice/logger/backend"
"reflect"
"strings"
)
const (
// NameFile is unique name of the file logger.
NameFile = "FILE"
// NameStdOutput is the unique name of the std logger.
NameStdOutput = "STD_OUTPUT"
// NameDB is the unique name of the DB logger.
NameDB = "DB"
)
// Declaration is used to declare a supported logger.
// Use this declaration to indicate what logger and sweeper will be provided.
type Declaration struct {
Logger Factory
Sweeper SweeperFactory
Getter GetterFactory
// Indicate if the logger is a singleton logger
Singleton bool
}
// knownLoggers is a static logger registry.
// All the implemented loggers (w/ sweeper) should be registered
// with an unique name in this registry. Then they can be used to
// log info.
var knownLoggers = map[string]*Declaration{
// File logger
NameFile: {FileFactory, FileSweeperFactory, FileGetterFactory, false},
// STD output(both stdout and stderr) logger
NameStdOutput: {StdFactory, nil, nil, true},
// DB logger
NameDB: {DBFactory, DBSweeperFactory, DBGetterFactory, false},
}
// IsKnownLogger checks if the logger is supported with name.
func IsKnownLogger(name string) bool {
_, ok := knownLoggers[name]
return ok
}
// HasSweeper checks if the logger with the name provides a sweeper.
func HasSweeper(name string) bool {
d, ok := knownLoggers[name]
return ok && d.Sweeper != nil
}
// HasGetter checks if the logger with the name provides a log data getter.
func HasGetter(name string) bool {
d, ok := knownLoggers[name]
return ok && d.Getter != nil
}
// KnownLoggers return the declaration by the name
func KnownLoggers(name string) *Declaration {
return knownLoggers[name]
}
// All known levels which are supported.
var debugLevels = []string{
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"FATAL",
}
// IsKnownLevel is used to check if the logger level is supported.
func IsKnownLevel(level string) bool {
if len(level) == 0 {
return false
}
for _, lvl := range debugLevels {
if lvl == strings.ToUpper(level) {
return true
}
}
return false
}
// GetLoggerName return a logger name by Interface
func GetLoggerName(l Interface) string {
var name string
if l == nil {
return name
}
switch l.(type) {
case *backend.DBLogger:
name = NameDB
case *backend.StdOutputLogger:
name = NameStdOutput
case *backend.FileLogger:
name = NameFile
default:
name = reflect.TypeOf(l).String()
}
return name
}