-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
81 lines (66 loc) · 1.67 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package engine
import (
"fmt"
"github.com/cadmean-ru/amphion/common"
"github.com/cadmean-ru/amphion/common/a"
)
type LogLevel uint
const (
Info LogLevel = 0
Warning LogLevel = 1
Error LogLevel = 2
)
type ILogger interface {
Log(level LogLevel, subsystem string, message string)
}
type Logger struct {
loggerImpl ILogger
}
func (logger *Logger) Info(subsystem interface{}, message string) {
logger.loggerImpl.Log(Info, logger.subsystemNameOrEmptyString(subsystem), message)
}
func (logger *Logger) Warning(subsystem interface{}, message string) {
logger.loggerImpl.Log(Warning, logger.subsystemNameOrEmptyString(subsystem), message)
}
func (logger *Logger) Error(subsystem interface{}, message string) {
logger.loggerImpl.Log(Error, logger.subsystemNameOrEmptyString(subsystem), message)
}
func (logger *Logger) subsystemNameOrEmptyString(subsystem interface{}) string {
if subsystem == nil {
return ""
}
if comp, ok := subsystem.(Component); ok {
return NameOfComponent(comp)
} else if named, ok := subsystem.(a.NamedObject); ok {
return named.GetName()
} else {
return fmt.Sprintf("%v", subsystem)
}
}
func getLoggerImpl(p common.Platform) ILogger {
switch p.GetName() {
default:
return &ConsoleLogger{}
}
}
func GetLoggerForPlatform(p common.Platform) *Logger {
return &Logger{
loggerImpl: getLoggerImpl(p),
}
}
type ConsoleLogger struct {}
func (logger *ConsoleLogger) Log(level LogLevel, subsystem string, message string) {
var lvlStr string
switch level {
case Info:
lvlStr = "Info"
break
case Warning:
lvlStr = "Warning"
break
case Error:
lvlStr = "Error"
break
}
fmt.Println(fmt.Sprintf("%s: %s: %s", lvlStr, subsystem, message))
}