@rivascva/dt-idl 1.1.73 → 1.1.75
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 +44 -24
- package/go/log/utils.go +4 -1
- 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,46 +21,41 @@ 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 {
|
|
24
|
-
kvs
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
error *log.Logger
|
|
29
|
+
kvs map[string]any
|
|
30
|
+
info *log.Logger
|
|
31
|
+
warn *log.Logger
|
|
32
|
+
error *log.Logger
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
// NewLog creates a new log.
|
|
32
36
|
func NewLog() *Log {
|
|
33
|
-
return NewLogWithPrefix("")
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// NewLogWithPrefix creates a new log with a custom prefix.
|
|
37
|
-
func NewLogWithPrefix(prefix string) *Log {
|
|
38
37
|
return &Log{
|
|
39
|
-
kvs:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
error: log.New(os.Stdout, "[ERROR] ", log.LstdFlags),
|
|
38
|
+
kvs: make(map[string]any, 10),
|
|
39
|
+
info: log.New(os.Stdout, "", log.LstdFlags),
|
|
40
|
+
warn: log.New(os.Stdout, "", log.LstdFlags),
|
|
41
|
+
error: log.New(os.Stdout, "", log.LstdFlags),
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
|
|
47
45
|
func (l *Log) Info(m any) {
|
|
48
|
-
l.info.Println(
|
|
46
|
+
l.info.Println(toJson(m, l.kvs, "INFO"))
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
func (l *Log) Warn(m any) {
|
|
52
|
-
l.warn.Println(
|
|
50
|
+
l.warn.Println(toJson(m, l.kvs, "WARN"))
|
|
53
51
|
}
|
|
54
52
|
|
|
55
53
|
func (l *Log) Error(m any) {
|
|
56
|
-
l.error.Println(
|
|
54
|
+
l.error.Println(toJson(m, l.kvs, "ERROR"))
|
|
57
55
|
}
|
|
58
56
|
|
|
59
57
|
func (l *Log) Fatal(m any) {
|
|
60
|
-
l.error.Fatal(
|
|
58
|
+
l.error.Fatal(toJson(m, l.kvs, "ERROR"))
|
|
61
59
|
}
|
|
62
60
|
|
|
63
61
|
func (l *Log) With(kvs ...any) Logger {
|
|
@@ -76,11 +74,33 @@ func (l *Log) With(kvs ...any) Logger {
|
|
|
76
74
|
}
|
|
77
75
|
|
|
78
76
|
return &Log{
|
|
79
|
-
kvs:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
kvs: newkvs,
|
|
78
|
+
info: l.info,
|
|
79
|
+
warn: l.warn,
|
|
80
|
+
error: l.error,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
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,
|
|
84
104
|
}
|
|
85
105
|
}
|
|
86
106
|
|
package/go/log/utils.go
CHANGED
|
@@ -6,12 +6,15 @@ import (
|
|
|
6
6
|
)
|
|
7
7
|
|
|
8
8
|
// toJson converts the message and key-value pairs to a JSON string.
|
|
9
|
-
func toJson(m any, kvs map[string]any) string {
|
|
9
|
+
func toJson(m any, kvs map[string]any, level string) string {
|
|
10
10
|
// copy the key-value pairs into a new map and add the message
|
|
11
11
|
out := make(map[string]any, len(kvs)+1)
|
|
12
12
|
maps.Copy(out, kvs)
|
|
13
13
|
out["msg"] = m
|
|
14
14
|
|
|
15
|
+
// add the log level
|
|
16
|
+
out["level"] = level
|
|
17
|
+
|
|
15
18
|
// marshal the map to a JSON byte array
|
|
16
19
|
data, err := json.Marshal(out)
|
|
17
20
|
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
|
+
}
|