-
Notifications
You must be signed in to change notification settings - Fork 11
/
extra_logging.go
135 lines (114 loc) · 4.24 KB
/
extra_logging.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package template
import (
"os"
"strconv"
"strings"
"sync"
"github.com/coveo/gotemplate/utils"
"github.com/fatih/color"
logging "github.com/op/go-logging"
)
const (
logger = "gotemplate"
loggerInternal = "gotemplate-int"
loggingBase = "Logging"
)
var loggingFuncs = dictionary{
"critical": func(args ...interface{}) string { return logBase(Log.Critical, args...) },
"debug": func(args ...interface{}) string { return logBase(Log.Debug, args...) },
"error": func(args ...interface{}) string { return logBase(Log.Error, args...) },
"fatal": func(args ...interface{}) string { return logBase(Log.Fatal, args...) },
"info": func(args ...interface{}) string { return logBase(Log.Info, args...) },
"notice": func(args ...interface{}) string { return logBase(Log.Notice, args...) },
"panic": func(args ...interface{}) string { return logBase(Log.Panic, args...) },
"warning": func(args ...interface{}) string { return logBase(Log.Warning, args...) },
}
var loggingFuncsAliases = aliases{
"critical": {"criticalf"},
"debug": {"debugf"},
"error": {"errorf"},
"fatal": {"fatalf"},
"info": {"infof"},
"notice": {"noticef"},
"panic": {"panicf"},
"warning": {"warn", "warnf", "warningf"},
}
var loggingFuncsHelp = descriptions{
"critical": "Logs a message using CRITICAL as log level (0).",
"debug": "Logs a message using DEBUG as log level (5).",
"error": "Logs a message using ERROR as log level (1).",
"fatal": "Equivalents to critical followed by a call to os.Exit(1).",
"info": "Logs a message using INFO as log level (4).",
"notice": "Logs a message using NOTICE as log level (3).",
"panic": "Equivalents to critical followed by a call to panic.",
"warning": "Logs a message using WARNING as log level (2).",
}
func (t *Template) addLoggingFuncs() {
t.AddFunctions(loggingFuncs, loggingBase, FuncOptions{
FuncHelp: loggingFuncsHelp,
FuncAliases: loggingFuncsAliases,
})
}
func logBase(f func(...interface{}), args ...interface{}) string {
f(utils.FormatMessage(args...))
return ""
}
// Log is the logger used to log message during template processing
var Log = logging.MustGetLogger(logger)
// log is application logger used to follow the behaviour of the application
var log = logging.MustGetLogger(loggerInternal)
var loggingMutex sync.Mutex
func getLogLevelInternal() logging.Level {
loggingMutex.Lock()
defer loggingMutex.Unlock()
return logging.GetLevel(loggerInternal)
}
// GetLogLevel returns the current logging level for gotemplate
func GetLogLevel() logging.Level {
loggingMutex.Lock()
defer loggingMutex.Unlock()
return logging.GetLevel(logger)
}
// SetLogLevel set the logging level for gotemplate
func SetLogLevel(level logging.Level) {
loggingMutex.Lock()
defer loggingMutex.Unlock()
logging.SetLevel(level, logger)
}
// ConfigureLogging allows configuration of the default logging level
func ConfigureLogging(level, internalLevel logging.Level, simple bool) {
format := `[%{module}] %{time:2006/01/02 15:04:05.000} %{color}%{level:-8s} %{message}%{color:reset}`
if simple {
format = `[%{level}] %{message}`
}
logging.SetBackend(logging.NewBackendFormatter(logging.NewLogBackend(color.Error, "", 0), logging.MustStringFormatter(format)))
SetLogLevel(level)
logging.SetLevel(internalLevel, loggerInternal)
}
// InitLogging allows configuration of the default logging level
func InitLogging() int {
if level, err := strconv.Atoi(utils.GetEnv(EnvDebug, "2")); err != nil {
log.Warningf("Unable to convert %s into integer: %s", EnvDebug, os.Getenv(EnvDebug))
} else {
logging.SetLevel(logging.Level(level), loggerInternal)
}
return 0
}
// Default package init
var _ = InitLogging()
// TryGetLoggingLevelFromString converts a string into a logging level
func TryGetLoggingLevelFromString(level string, defaultLevel logging.Level) (logging.Level, error) {
level = strings.TrimSpace(level)
if level == "" {
return defaultLevel, nil
}
levelNum, err := strconv.Atoi(level)
if err == nil {
return logging.Level(levelNum), nil
}
return logging.LogLevel(level)
}
// GetLoggingLevelFromString converts a string into a logging level
func GetLoggingLevelFromString(level string) logging.Level {
return must(TryGetLoggingLevelFromString(level, logging.INFO)).(logging.Level)
}