@rivascva/dt-idl 1.1.69 → 1.1.71
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 +4 -4
- package/go/log/utils.go +2 -2
- package/go/utils/context.go +35 -0
- package/package.json +1 -1
package/go/log/log.go
CHANGED
|
@@ -45,19 +45,19 @@ func NewLogWithPrefix(prefix string) *Log {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
func (l *Log) Info(m any) {
|
|
48
|
-
l.info.Println(l.prefix,
|
|
48
|
+
l.info.Println(l.prefix, toJson(m, l.kvs))
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
func (l *Log) Warn(m any) {
|
|
52
|
-
l.warn.Println(l.prefix,
|
|
52
|
+
l.warn.Println(l.prefix, toJson(m, l.kvs))
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
func (l *Log) Error(m any) {
|
|
56
|
-
l.error.Println(l.prefix,
|
|
56
|
+
l.error.Println(l.prefix, toJson(m, l.kvs))
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
func (l *Log) Fatal(m any) {
|
|
60
|
-
l.error.Fatal(l.prefix,
|
|
60
|
+
l.error.Fatal(l.prefix, toJson(m, l.kvs))
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
func (l *Log) With(kvs ...any) Logger {
|
package/go/log/utils.go
CHANGED
|
@@ -5,8 +5,8 @@ import (
|
|
|
5
5
|
"maps"
|
|
6
6
|
)
|
|
7
7
|
|
|
8
|
-
//
|
|
9
|
-
func
|
|
8
|
+
// toJson converts the message and key-value pairs to a JSON string.
|
|
9
|
+
func toJson(m any, kvs map[string]any) 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)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package utils
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"errors"
|
|
6
|
+
"fmt"
|
|
7
|
+
|
|
8
|
+
"github.com/RivasCVA/dt-idl/go/models"
|
|
9
|
+
"github.com/golang-jwt/jwt/v5"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
// GetTokenFromContext retrieves the JWT token from the given context.
|
|
13
|
+
func GetTokenFromContext(ctx context.Context) *jwt.Token {
|
|
14
|
+
token, ok := ctx.Value(models.TokenKey).(*jwt.Token)
|
|
15
|
+
if !ok {
|
|
16
|
+
return nil
|
|
17
|
+
}
|
|
18
|
+
return token
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func GetUserIdFromContext(ctx context.Context) (string, error) {
|
|
22
|
+
// get the token from the context
|
|
23
|
+
token := GetTokenFromContext(ctx)
|
|
24
|
+
if token == nil {
|
|
25
|
+
return "", errors.New("token not found in the context")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// get the subject (userId) from the token
|
|
29
|
+
userId, err := token.Claims.GetSubject()
|
|
30
|
+
if err != nil {
|
|
31
|
+
return "", fmt.Errorf("failed to get the subject from the token: %w", err)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return userId, nil
|
|
35
|
+
}
|