-
Notifications
You must be signed in to change notification settings - Fork 0
Deploy #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deploy #14
Changes from all commits
48624f0
5041d0e
dca74b2
af78d4a
6912264
0741d90
c96ea8f
86c2120
2045bb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||
| package mchlogcommon | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "encoding/json" | ||||||||||
| "errors" | ||||||||||
| "fmt" | ||||||||||
| "reflect" | ||||||||||
|
|
||||||||||
| "github.com/rs/zerolog" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // GetJSONLogger retorna o evento de log correspondente | ||||||||||
| // pronto para ser persistido no arquivo. | ||||||||||
| func GetJSONLogger(logger *zerolog.Logger, content any) (*zerolog.Event, error) { | ||||||||||
| var err error | ||||||||||
| var event *zerolog.Event | ||||||||||
| var ok bool | ||||||||||
|
|
||||||||||
| v := reflect.ValueOf(content) | ||||||||||
| switch v.Kind() { | ||||||||||
| case reflect.Map: | ||||||||||
| var m map[string]any | ||||||||||
| if m, ok = content.(map[string]any); !ok { | ||||||||||
| m = make(map[string]any) | ||||||||||
| keys := v.MapKeys() | ||||||||||
| for _, k := range keys { | ||||||||||
| va := v.MapIndex(k) | ||||||||||
| switch va.Kind() { | ||||||||||
| case reflect.String: | ||||||||||
| m[k.String()] = va.String() | ||||||||||
| case reflect.Int, reflect.Int64, reflect.Int32: | ||||||||||
| m[k.String()] = va.Int() | ||||||||||
| case reflect.Float64, reflect.Float32: | ||||||||||
| m[k.String()] = va.Float() | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| event = logger.Log().Fields(m) | ||||||||||
|
|
||||||||||
| case reflect.Slice, reflect.Array: | ||||||||||
| var arrb []byte | ||||||||||
| if arrb, ok = content.([]byte); ok { | ||||||||||
| var m map[string]any | ||||||||||
| if err = json.Unmarshal(arrb, &m); err == nil { | ||||||||||
| event = logger.Log().Fields(m) | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| var arr []any | ||||||||||
| if arr, ok = content.([]any); !ok { | ||||||||||
| tam := v.Len() | ||||||||||
| for i := 0; i < tam; i++ { | ||||||||||
| va := v.Index(i) | ||||||||||
| switch va.Kind() { | ||||||||||
| case reflect.String: | ||||||||||
| arr = append(arr, va.String()) | ||||||||||
| case reflect.Int, reflect.Int64, reflect.Int32: | ||||||||||
| arr = append(arr, va.Int()) | ||||||||||
| case reflect.Float64, reflect.Float32: | ||||||||||
| arr = append(arr, va.Float()) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| event = logger.Log().Fields(arr) | ||||||||||
| } | ||||||||||
| case reflect.String: | ||||||||||
| s := content.(string) | ||||||||||
| var m map[string]any | ||||||||||
| if err = json.Unmarshal([]byte(s), &m); err == nil { | ||||||||||
| event = logger.Log().Fields(m) | ||||||||||
| } | ||||||||||
| default: | ||||||||||
| err = errors.New("") | ||||||||||
|
Comment on lines
+71
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default error message Please provide a more specific error message that indicates the type of content that was not handled.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| if err != nil { | ||||||||||
| err = fmt.Errorf("tipo inválido do conteúdo do log: %v.\nEsperados map, json em formato de string ou []byte", v.Type()) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return event, err | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for handling
reflect.Sliceorreflect.Arraytypes (when not[]byteor[]anyinterpreted as key-value pairs) can lead to incorrect JSON output. Ifcontentis a strongly typed slice (e.g.,[]int{1, 2, 3}), it is converted to[]any{1, 2, 3}. Passing this tologger.Log().Fields(arr)will causezerologto misinterpret the array elements as alternating keys and values, which is likely not the intended behavior and can result in malformed log entries.To ensure correct JSON formatting, consider marshaling such slices/arrays to JSON and then unmarshaling them into a
map[string]anyif they represent a JSON object, or logging them as a single array field if they represent a JSON array of primitives. If the content cannot be converted to a valid JSON object, it should be explicitly treated as an invalid type.