@rivascva/dt-idl 1.1.74 → 1.1.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/log/log.go +35 -7
- package/go/log/utils.go +4 -0
- package/go/middlewares/middlewares.go +11 -0
- package/go/models/constants.go +2 -0
- package/go/utils/context.go +9 -0
- package/package.json +1 -1
package/go/log/log.go
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
package log
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"context"
|
|
4
5
|
"fmt"
|
|
5
6
|
"log"
|
|
6
7
|
"maps"
|
|
7
8
|
"os"
|
|
9
|
+
|
|
10
|
+
"github.com/RivasCVA/dt-idl/go/utils"
|
|
8
11
|
)
|
|
9
12
|
|
|
10
13
|
type Logger interface {
|
|
@@ -18,6 +21,8 @@ type Logger interface {
|
|
|
18
21
|
Fatal(m any)
|
|
19
22
|
// With returns a new log with the given key-value pairs.
|
|
20
23
|
With(kvs ...any) Logger
|
|
24
|
+
// WithContext returns a new log with the given context.
|
|
25
|
+
WithContext(ctx context.Context) Logger
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
type Log struct {
|
|
@@ -31,26 +36,26 @@ type Log struct {
|
|
|
31
36
|
func NewLog() *Log {
|
|
32
37
|
return &Log{
|
|
33
38
|
kvs: make(map[string]any, 10),
|
|
34
|
-
info: log.New(os.Stdout, "",
|
|
35
|
-
warn: log.New(os.Stdout, "",
|
|
36
|
-
error: log.New(os.Stdout, "",
|
|
39
|
+
info: log.New(os.Stdout, "", 0),
|
|
40
|
+
warn: log.New(os.Stdout, "", 0),
|
|
41
|
+
error: log.New(os.Stdout, "", 0),
|
|
37
42
|
}
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
func (l *Log) Info(m any) {
|
|
41
|
-
l.info.Println(toJson(m, l.kvs, "
|
|
46
|
+
l.info.Println(toJson(m, l.kvs, "Info"))
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
func (l *Log) Warn(m any) {
|
|
45
|
-
l.warn.Println(toJson(m, l.kvs, "
|
|
50
|
+
l.warn.Println(toJson(m, l.kvs, "Warn"))
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
func (l *Log) Error(m any) {
|
|
49
|
-
l.error.Println(toJson(m, l.kvs, "
|
|
54
|
+
l.error.Println(toJson(m, l.kvs, "Error"))
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
func (l *Log) Fatal(m any) {
|
|
53
|
-
l.error.Fatal(toJson(m, l.kvs, "
|
|
58
|
+
l.error.Fatal(toJson(m, l.kvs, "Error"))
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
func (l *Log) With(kvs ...any) Logger {
|
|
@@ -76,5 +81,28 @@ func (l *Log) With(kvs ...any) Logger {
|
|
|
76
81
|
}
|
|
77
82
|
}
|
|
78
83
|
|
|
84
|
+
func (l *Log) WithContext(ctx context.Context) Logger {
|
|
85
|
+
// copy the existing key-value pairs into a new map
|
|
86
|
+
newkvs := make(map[string]any, len(l.kvs)+10)
|
|
87
|
+
maps.Copy(newkvs, l.kvs)
|
|
88
|
+
|
|
89
|
+
// add the user id to the key-value pairs
|
|
90
|
+
if userId, err := utils.GetUserIdFromContext(ctx); err == nil {
|
|
91
|
+
newkvs["userId"] = userId
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// add the request path to the key-value pairs
|
|
95
|
+
if path, err := utils.GetPathFromContext(ctx); err == nil {
|
|
96
|
+
newkvs["path"] = path
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return &Log{
|
|
100
|
+
kvs: newkvs,
|
|
101
|
+
info: l.info,
|
|
102
|
+
warn: l.warn,
|
|
103
|
+
error: l.error,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
79
107
|
//exhaustruct:ignore - check for interface implementation
|
|
80
108
|
var _ Logger = &Log{}
|
package/go/log/utils.go
CHANGED
|
@@ -3,6 +3,7 @@ package log
|
|
|
3
3
|
import (
|
|
4
4
|
"encoding/json"
|
|
5
5
|
"maps"
|
|
6
|
+
"time"
|
|
6
7
|
)
|
|
7
8
|
|
|
8
9
|
// toJson converts the message and key-value pairs to a JSON string.
|
|
@@ -15,6 +16,9 @@ func toJson(m any, kvs map[string]any, level string) string {
|
|
|
15
16
|
// add the log level
|
|
16
17
|
out["level"] = level
|
|
17
18
|
|
|
19
|
+
// add the timestamp
|
|
20
|
+
out["timestamp"] = time.Now().UTC().Format(time.RFC3339)
|
|
21
|
+
|
|
18
22
|
// marshal the map to a JSON byte array
|
|
19
23
|
data, err := json.Marshal(out)
|
|
20
24
|
if err != nil {
|
|
@@ -50,6 +50,17 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
// ContextMiddleware attaches helpful information to the request context.
|
|
54
|
+
func ContextMiddleware(next http.Handler) http.Handler {
|
|
55
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
56
|
+
// set the path in the request context
|
|
57
|
+
ctx := context.WithValue(r.Context(), models.PathKey, r.URL.Path)
|
|
58
|
+
r = r.WithContext(ctx)
|
|
59
|
+
|
|
60
|
+
next.ServeHTTP(w, r)
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
// CommonHeaders adds shared headers to all responses.
|
|
54
65
|
func CommonHeaders(next http.Handler) http.Handler {
|
|
55
66
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
package/go/models/constants.go
CHANGED
package/go/utils/context.go
CHANGED
|
@@ -37,3 +37,12 @@ func GetUserIdFromContext(ctx context.Context) (string, error) {
|
|
|
37
37
|
|
|
38
38
|
return userId, nil
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
// GetPathFromContext retrieves the request path from the given context.
|
|
42
|
+
func GetPathFromContext(ctx context.Context) (string, error) {
|
|
43
|
+
path, ok := ctx.Value(models.PathKey).(string)
|
|
44
|
+
if !ok {
|
|
45
|
+
return "", errors.New("path not found in the context")
|
|
46
|
+
}
|
|
47
|
+
return path, nil
|
|
48
|
+
}
|