Skip to content

Commit 172e5c3

Browse files
committed
Update configuration file templates
1 parent 3b93213 commit 172e5c3

File tree

9 files changed

+190
-0
lines changed

9 files changed

+190
-0
lines changed

examples/configurationfile/inputs/example.json

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
"time-format": "2006-01-02 15:04:05",
66
"error-level": "error",
77
"panic-level": "critical",
8+
"request-template": "Request: [{Method}] {URL}",
9+
"response-template": "Response: [{StatusCode}] {Status}",
10+
"request-mapping": {
11+
"method": "Method",
12+
"url": "URL"
13+
},
14+
"response-mapping": {
15+
"status-code": "StatusCode",
16+
"status": "Status"
17+
},
818
"message-queue-size": 100,
919
"handlers": [
1020
{

examples/configurationfile/inputs/example.xml

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
<time-format>2006-01-02 15:04:05</time-format>
77
<error-level>error</error-level>
88
<panic-level>critical</panic-level>
9+
<request-template>Request: [{Method}] {URL}</request-template>
10+
<response-template>Response: [{StatusCode}] {Status}</response-template>
11+
<request-mapping>
12+
<method>Method</method>
13+
<url>URL</url>
14+
</request-mapping>
15+
<response-mapping>
16+
<status-code>StatusCode</status-code>
17+
<status>Status</status>
18+
</response-mapping>
919
<message-queue-size>100</message-queue-size>
1020
<handlers>
1121
<handler>

examples/configurationfile/inputs/example.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ loggers:
33
time-format: '2006-01-02 15:04:05'
44
error-level: error
55
panic-level: critical
6+
request-template: "Request: [{Method}] {URL}"
7+
response-template: "Response: [{StatusCode}] {Status}"
8+
request-mapping:
9+
method: Method
10+
url: URL
11+
response-mapping:
12+
status-code: StatusCode
13+
status: Status
614
message-queue-size: 100
715
handlers:
816
- type: stdout

pkg/common/configuration/parser/parser.go

+8
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ type LoggerConfiguration struct {
152152
ErrorLevel string `json:"error-level" yaml:"error-level" xml:"error-level"`
153153
// PanicLevel is the panic level used by the logger for panic.
154154
PanicLevel string `json:"panic-level" yaml:"panic-level" xml:"panic-level"`
155+
// RequestTemplate is the template used by the logger for a request struct.
156+
RequestTemplate string `json:"request-template" yaml:"request-template" xml:"request-template"`
157+
// ResponseTemplate is the template used by the logger for a response struct.
158+
ResponseTemplate string `json:"response-template" yaml:"response-template" xml:"response-template"`
159+
// RequestMapping is the mapping used by the logger for a request struct.
160+
RequestMapping KeyValue `json:"request-mapping" yaml:"request-mapping" xml:"request-mapping"`
161+
// ResponseMapping is the mapping used by the logger for a response struct.
162+
ResponseMapping KeyValue `json:"response-mapping" yaml:"response-mapping" xml:"response-mapping"`
155163
// MessageQueueSize is the size of the message queue used by async logger.
156164
MessageQueueSize int `json:"message-queue-size" yaml:"message-queue-size" xml:"message-queue-size"`
157165
// Handlers is the list of handlers used by the logger.

pkg/common/utils/utils.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Package utils contains utility functions.
2+
package utils
3+
4+
import (
5+
"fmt"
6+
"net/http"
7+
"reflect"
8+
)
9+
10+
// StructToMap converts a struct public fields to a map.
11+
func StructToMap(object interface{}) map[string]interface{} {
12+
objectType := reflect.TypeOf(object)
13+
objectValue := reflect.ValueOf(object)
14+
15+
var data = make(map[string]interface{})
16+
for index := 0; index < objectType.NumField(); index++ {
17+
field := objectType.Field(index)
18+
if field.PkgPath == "" {
19+
value := objectValue.Field(index).Interface()
20+
switch convertedValue := value.(type) {
21+
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool, string:
22+
data[field.Name] = value
23+
case http.Header:
24+
for key, value := range convertedValue {
25+
fieldName := fmt.Sprintf("Header.%s", key)
26+
data[fieldName] = value
27+
}
28+
case map[string]interface{}, []interface{}:
29+
default:
30+
data[field.Name] = fmt.Sprintf("%v", value)
31+
}
32+
}
33+
}
34+
35+
return data
36+
}

pkg/common/utils/utils_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"github.com/dl1998/go-logging/internal/testutils"
6+
"net/http"
7+
"testing"
8+
)
9+
10+
var (
11+
testStruct = struct {
12+
private string
13+
Int int
14+
Int8 int8
15+
Int16 int16
16+
Int32 int32
17+
Int64 int64
18+
Uint uint
19+
Uint8 uint8
20+
Uint16 uint16
21+
Uint32 uint32
22+
Uint64 uint64
23+
Float32 float32
24+
Float64 float64
25+
Bool bool
26+
String string
27+
Header http.Header
28+
CustomStruct CustomStruct
29+
}{
30+
private: "private",
31+
Int: -1,
32+
Int8: -8,
33+
Int16: -16,
34+
Int32: -32,
35+
Int64: -64,
36+
Uint: 1,
37+
Uint8: 8,
38+
Uint16: 16,
39+
Uint32: 32,
40+
Uint64: 64,
41+
Float32: 32.00,
42+
Float64: 64.00,
43+
Bool: true,
44+
String: "string",
45+
Header: http.Header{"key": []string{"value"}},
46+
CustomStruct: CustomStruct{Field: "custom field"},
47+
}
48+
)
49+
50+
type CustomStruct struct {
51+
Field string
52+
}
53+
54+
func (customStruct *CustomStruct) String() string {
55+
return customStruct.Field
56+
}
57+
58+
// TestStructToMap tests that StructToMap converts a struct to a map.
59+
func TestStructToMap(t *testing.T) {
60+
expected := map[string]interface{}{
61+
"Int": testStruct.Int,
62+
"Int8": testStruct.Int8,
63+
"Int16": testStruct.Int16,
64+
"Int32": testStruct.Int32,
65+
"Int64": testStruct.Int64,
66+
"Uint": testStruct.Uint,
67+
"Uint8": testStruct.Uint8,
68+
"Uint16": testStruct.Uint16,
69+
"Uint32": testStruct.Uint32,
70+
"Uint64": testStruct.Uint64,
71+
"Float32": testStruct.Float32,
72+
"Float64": testStruct.Float64,
73+
"Bool": testStruct.Bool,
74+
"String": testStruct.String,
75+
"Header.key": []string{"value"},
76+
"CustomStruct": fmt.Sprintf("{%s}", testStruct.CustomStruct.Field),
77+
}
78+
79+
actual := StructToMap(testStruct)
80+
81+
testutils.AssertNotNil(t, actual)
82+
testutils.AssertEquals(t, expected, actual)
83+
}
84+
85+
// BenchmarkStructToMap benchmarks the StructToMap function.
86+
func BenchmarkStructToMap(b *testing.B) {
87+
for index := 0; index < b.N; index++ {
88+
_ = StructToMap(testStruct)
89+
}
90+
}

testdata/example.json

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
"time-format": "2006-01-02 15:04:05",
66
"error-level": "error",
77
"panic-level": "critical",
8+
"request-template": "Test Request: [{Method}] {URL}",
9+
"response-template": "Test Response: [{Status}] {StatusCode}",
10+
"request-mapping": {
11+
"test-method": "Method",
12+
"test-url": "URL"
13+
},
14+
"response-mapping": {
15+
"test-status-code": "StatusCode",
16+
"test-status": "Status"
17+
},
818
"message-queue-size": 100,
919
"handlers": [
1020
{

testdata/example.xml

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
<time-format>2006-01-02 15:04:05</time-format>
77
<error-level>error</error-level>
88
<panic-level>critical</panic-level>
9+
<request-template>Test Request: [{Method}] {URL}</request-template>
10+
<response-template>Test Response: [{Status}] {StatusCode}</response-template>
11+
<request-mapping>
12+
<test-method>Method</test-method>
13+
<test-url>URL</test-url>
14+
</request-mapping>
15+
<response-mapping>
16+
<test-status-code>StatusCode</test-status-code>
17+
<test-status>Status</test-status>
18+
</response-mapping>
919
<message-queue-size>100</message-queue-size>
1020
<handlers>
1121
<handler>

testdata/example.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ loggers:
33
time-format: '2006-01-02 15:04:05'
44
error-level: error
55
panic-level: critical
6+
request-template: "Test Request: [{Method}] {URL}"
7+
response-template: "Test Response: [{Status}] {StatusCode}"
8+
request-mapping:
9+
test-method: Method
10+
test-url: URL
11+
response-mapping:
12+
test-status-code: StatusCode
13+
test-status: Status
614
message-queue-size: 100
715
handlers:
816
- type: stdout

0 commit comments

Comments
 (0)