Skip to content

Commit

Permalink
Add WithAppJsonData
Browse files Browse the repository at this point in the history
  • Loading branch information
NV4RE committed Nov 23, 2023
1 parent e51f127 commit 996befe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 9 additions & 8 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
)

type Log interface {
Write() // Logs the current entry to the output.
SetMessage(msg string) Log // Sets or overrides the log message.
SetLevel(level level.Level) Log // Sets or overrides the log level (e.g., info, warning, error).
SetAlert(bool bool) Log // Sets or overrides the alert flag.
WithAppData(key string, value any) Log // Adds application-specific data.
WithError(err error) Log // Adds error information.
WithTracing(t trace.SpanContext) Log // Adds tracing information.
WithStackTrace() Log // Captures and adds a stack trace.
Write() // Logs the current entry to the output.
SetMessage(msg string) Log // Sets or overrides the log message.
SetLevel(level level.Level) Log // Sets or overrides the log level (e.g., info, warning, error).
SetAlert(bool bool) Log // Sets or overrides the alert flag.
WithAppData(key string, value any) Log // Adds application-specific data.
WithError(err error) Log // Adds error information.
WithTracing(t trace.SpanContext) Log // Adds tracing information.
WithStackTrace() Log // Captures and adds a stack trace.
WithAppJsonData(key string, value any) Log // Set arbitrary json data
}

type ZapLogger interface {
Expand Down
13 changes: 13 additions & 0 deletions zap_logger/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zap_logger

import (
"encoding/json"
"fmt"
"github.com/Sellsuki/sellsuki-go-logger/v2/config"
"github.com/Sellsuki/sellsuki-go-logger/v2/level"
"github.com/Sellsuki/sellsuki-go-logger/v2/log"
Expand Down Expand Up @@ -67,6 +69,17 @@ func (l Logger) WithAppData(key string, value any) log.Log {
return &l
}

func (l Logger) WithAppJsonData(key string, value any) log.Log {
b, err := json.Marshal(value)
if err != nil {
l.AppFields[fmt.Sprintf(`%s_json_error`, key)] = err.Error()
}

l.AppFields[fmt.Sprintf(`%s_json`, key)] = string(b)

return &l
}

func (l Logger) WithError(err error) log.Log {
if err == nil {
return &l
Expand Down

0 comments on commit 996befe

Please sign in to comment.