Skip to content

Commit 8110950

Browse files
committed
Add fundamental logger mechanism (opensergo#16)
Signed-off-by: Jiangnan Jia <[email protected]>
1 parent 1df7486 commit 8110950

File tree

13 files changed

+1057
-11
lines changed

13 files changed

+1057
-11
lines changed

Diff for: go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/opensergo/opensergo-control-plane
33
go 1.14
44

55
require (
6-
github.com/alibaba/sentinel-golang v1.0.3
76
github.com/envoyproxy/protoc-gen-validate v0.1.0
87
github.com/go-logr/logr v0.4.0
98
github.com/json-iterator/go v1.1.12 // indirect

Diff for: pkg/common/logging/README.md

Whitespace-only changes.

Diff for: pkg/common/logging/doc.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package logging.
16+
package logging

Diff for: pkg/common/logging/logger.go

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright 2022, OpenSergo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logging
16+
17+
import (
18+
"github.com/pkg/errors"
19+
"reflect"
20+
)
21+
22+
type Logger interface {
23+
24+
// Print logs message no format as what the msg presents.
25+
Print(msg string)
26+
27+
// DebugEnabled judge is the DebugLevel enabled
28+
DebugEnabled() bool
29+
// Debug logs a non-error message with the given key/value pairs as context.
30+
//
31+
// The msg argument should be used to add some constant description to
32+
// the log line. The key/value pairs can then be used to add additional
33+
// variable information. The key/value pairs should alternate string
34+
// keys and arbitrary values.
35+
Debug(msg string, keysAndValues ...interface{})
36+
37+
// InfoEnabled judge is the InfoLevel enabled
38+
InfoEnabled() bool
39+
// Info logs a non-error message with the given key/value pairs as context.
40+
//
41+
// The msg argument should be used to add some constant description to
42+
// the log line. The key/value pairs can then be used to add additional
43+
// variable information. The key/value pairs should alternate string
44+
// keys and arbitrary values.
45+
Info(msg string, keysAndValues ...interface{})
46+
47+
// WarnEnabled judge is the WarnLevel enabled
48+
WarnEnabled() bool
49+
// Warn logs a non-error message with the given key/value pairs as context.
50+
//
51+
// The msg argument should be used to add some constant description to
52+
// the log line. The key/value pairs can then be used to add additional
53+
// variable information. The key/value pairs should alternate string
54+
// keys and arbitrary values.
55+
Warn(msg string, keysAndValues ...interface{})
56+
57+
// ErrorEnabled judge is the ErrorLevel enabled
58+
ErrorEnabled() bool
59+
// Error logs an error message with error and the given key/value pairs as context.
60+
//
61+
// The msg argument should be used to add some constant description to
62+
// the log line. The key/value pairs can then be used to add additional
63+
// variable information. The key/value pairs should alternate string
64+
// keys and arbitrary values.
65+
Error(err error, msg string, keysAndValues ...interface{})
66+
}
67+
68+
var (
69+
loggerSlice = make([]Logger, 0)
70+
71+
consoleLogger Logger
72+
)
73+
74+
func Print(msg string) {
75+
doLog("Print", nil, msg)
76+
}
77+
78+
func Debug(msg string, keysAndValues ...interface{}) {
79+
doLog("Debug", nil, msg, keysAndValues...)
80+
}
81+
82+
func DebugWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) {
83+
if !logger.DebugEnabled() {
84+
return
85+
}
86+
logger.Print(AssembleMsg(logFormat, logCallerDepth, "DEBUG", msg, nil, false, keysAndValues...))
87+
}
88+
89+
func Info(msg string, keysAndValues ...interface{}) {
90+
doLog("Info", nil, msg, keysAndValues...)
91+
}
92+
93+
func InfoWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) {
94+
if !logger.InfoEnabled() {
95+
return
96+
}
97+
logger.Print(AssembleMsg(logFormat, logCallerDepth, "INFO", msg, nil, false, keysAndValues...))
98+
}
99+
100+
func Warn(msg string, keysAndValues ...interface{}) {
101+
doLog("Warn", nil, msg, keysAndValues...)
102+
}
103+
104+
func WarnWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, msg string, keysAndValues ...interface{}) {
105+
if !logger.WarnEnabled() {
106+
return
107+
}
108+
109+
logger.Print(AssembleMsg(logFormat, logCallerDepth, "WARN", msg, nil, false, keysAndValues...))
110+
}
111+
112+
func Error(err error, msg string, keysAndValues ...interface{}) {
113+
doLog("Error", err, msg, keysAndValues...)
114+
}
115+
116+
func ErrorWithCallerDepth(logger Logger, logFormat LogFormat, logCallerDepth int, err error, errorWithStack bool, msg string, keysAndValues ...interface{}) {
117+
if !logger.ErrorEnabled() {
118+
return
119+
}
120+
logger.Print(AssembleMsg(logFormat, logCallerDepth, "ERROR", msg, err, errorWithStack, keysAndValues...))
121+
}
122+
123+
// AppendLoggerSlice add the Logger into loggerSlice
124+
func AppendLoggerSlice(loggerAppend Logger) {
125+
loggerSlice = append(loggerSlice, loggerAppend)
126+
}
127+
128+
// ClearLoggerSlice clear the Logger into loggerSlice
129+
func ClearLoggerSlice() {
130+
loggerSlice = make([]Logger, 0)
131+
}
132+
133+
// SetConsoleLogger set the consoleLogger to print int stdout
134+
func SetConsoleLogger(logger Logger) {
135+
consoleLogger = logger
136+
}
137+
138+
// doLog do log
139+
// funcNameFromInterface funcName in Logger
140+
// err
141+
// msg
142+
// keysAndValues
143+
func doLog(funcNameFromInterface string, err error, msg string, keysAndValues ...interface{}) {
144+
if consoleLogger == nil && len(loggerSlice) == 0 {
145+
NewDefaultConsoleLogger(InfoLevel)
146+
}
147+
148+
if consoleLogger != nil {
149+
invokeLogger(consoleLogger, funcNameFromInterface, err, msg, keysAndValues...)
150+
}
151+
152+
if len(loggerSlice) > 0 {
153+
for _, logger := range loggerSlice {
154+
invokeLogger(logger, funcNameFromInterface, err, msg, keysAndValues...)
155+
}
156+
}
157+
}
158+
159+
// invokeLogger do log actually by invoke function of Logger
160+
// logger Logger to print
161+
// funcNameFromInterface funcName in Logger
162+
// err
163+
// msg
164+
// keysAndValues
165+
func invokeLogger(logger Logger, funcNameFromInterface string, err error, msg string, keysAndValues ...interface{}) {
166+
method, ok := reflect.TypeOf(logger).MethodByName(funcNameFromInterface)
167+
if !ok {
168+
assembleMsg := AssembleMsg(SeparateFormat, 4, "WARN", "no function named '"+funcNameFromInterface+"' was found in interface 'opensergo-go/pkg/logging/Logger'", nil, false)
169+
logger.Print(assembleMsg)
170+
return
171+
}
172+
173+
keysAndValuesLen := len(keysAndValues)
174+
params := make([]reflect.Value, 0)
175+
params = append(params, reflect.ValueOf(logger))
176+
if "Error" == funcNameFromInterface {
177+
if err == nil {
178+
err = errors.New("")
179+
}
180+
params = append(params, reflect.ValueOf(err))
181+
}
182+
params = append(params, reflect.ValueOf(msg))
183+
184+
if keysAndValuesLen != 0 {
185+
if keysAndValuesLen == 1 && keysAndValues[0] == nil {
186+
187+
} else {
188+
for _, keyOrValue := range keysAndValues {
189+
params = append(params, reflect.ValueOf(keyOrValue))
190+
}
191+
}
192+
}
193+
method.Func.Call(params)
194+
}

0 commit comments

Comments
 (0)