-
Notifications
You must be signed in to change notification settings - Fork 19
feat(client): add debug struct logging #304
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4791765
feat: add tuf client tracing
iapershin dfd44e3
refactor: add version to usage
alexey-igrychev c851fe4
misly
alexey-igrychev 9f243b5
feat(client): add debug struct logging
iapershin 04292c4
fix lint
iapershin d8fd9a5
fix
iapershin 2c89afb
to pkg
iapershin bfc71e6
+
alexey-igrychev fa80180
chore: doc:regent
alexey-igrychev 8bcbeb5
+
alexey-igrychev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package logger | ||
|
|
||
| import ( | ||
| "log/slog" | ||
| "os" | ||
| ) | ||
|
|
||
| var GlobalLogger *Logger | ||
|
|
||
| type LoggerInterface interface { | ||
| Debug(msg string, args ...any) | ||
| Error(msg string, args ...any) | ||
| Info(msg string, args ...any) | ||
| Warn(msg string, args ...any) | ||
| With(args ...any) LoggerInterface | ||
| } | ||
|
|
||
| type Logger struct { | ||
| LoggerInterface | ||
| } | ||
|
|
||
| type LoggerOptions struct { | ||
| Level string | ||
| LogFormat string | ||
| } | ||
|
|
||
| func SetupGlobalLogger(opts LoggerOptions) *Logger { | ||
| return NewLogger(&SlogWrapper{ | ||
| logger: NewSlogLogger(SlogOptions{ | ||
| Handler: slogHandler(opts.LogFormat, slogLevel(opts.Level)), | ||
| }), | ||
| }) | ||
| } | ||
|
|
||
| func NewLogger(l LoggerInterface) *Logger { | ||
| return &Logger{LoggerInterface: l} | ||
| } | ||
|
|
||
| type SlogOptions struct { | ||
| Handler slog.Handler | ||
| } | ||
|
|
||
| func NewSlogLogger(opts SlogOptions) *slog.Logger { | ||
| return slog.New(opts.Handler) | ||
| } | ||
|
|
||
| type SlogWrapper struct { | ||
| logger *slog.Logger | ||
| } | ||
|
|
||
| func (s *SlogWrapper) Debug(msg string, args ...any) { | ||
| s.logger.Debug(msg, args...) | ||
| } | ||
|
|
||
| func (s *SlogWrapper) Error(msg string, args ...any) { | ||
| s.logger.Error(msg, args...) | ||
| } | ||
|
|
||
| func (s *SlogWrapper) Info(msg string, args ...any) { | ||
| s.logger.Info(msg, args...) | ||
| } | ||
|
|
||
| func (s *SlogWrapper) Warn(msg string, args ...any) { | ||
| s.logger.Warn(msg, args...) | ||
| } | ||
|
|
||
| func (s *SlogWrapper) With(args ...any) LoggerInterface { | ||
| return &SlogWrapper{ | ||
| logger: s.logger.With(args...), | ||
| } | ||
| } | ||
|
|
||
| func (l *Logger) With(args ...any) *Logger { | ||
| return &Logger{LoggerInterface: l.LoggerInterface.With(args...)} | ||
| } | ||
|
|
||
| func slogLevel(l string) slog.Level { | ||
| switch l { | ||
| case "debug": | ||
| return slog.LevelDebug | ||
| case "info": | ||
| return slog.LevelInfo | ||
| case "warn": | ||
| return slog.LevelWarn | ||
| case "error": | ||
| return slog.LevelError | ||
| default: | ||
| return slog.LevelInfo | ||
| } | ||
| } | ||
|
|
||
| func slogHandler(format string, level slog.Level) slog.Handler { | ||
| options := &slog.HandlerOptions{ | ||
| Level: level, | ||
| } | ||
| switch format { | ||
| case "json": | ||
| return slog.NewJSONHandler(os.Stdout, options) | ||
| default: | ||
| return slog.NewTextHandler(os.Stdout, options) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package tuf | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptrace" | ||
| "time" | ||
|
|
||
| "github.com/werf/trdl/client/pkg/logger" | ||
| ) | ||
|
|
||
| type TracingTransport struct { | ||
| Transport http.RoundTripper | ||
| Logger logger.Logger | ||
| } | ||
|
|
||
| func (t *TracingTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| log := t.Logger.With("sorce", "tuf-client") | ||
| startTime := time.Now() | ||
|
|
||
| log.Debug("Request started", | ||
| "method", req.Method, | ||
| "url", req.URL.String(), | ||
| ) | ||
|
|
||
| trace := &httptrace.ClientTrace{ | ||
| DNSDone: func(info httptrace.DNSDoneInfo) { | ||
| log.Debug("DNS lookup done", "host", info.Addrs) | ||
| }, | ||
| ConnectDone: func(network, addr string, err error) { | ||
| if err != nil { | ||
| log.Debug("Failed to connect", | ||
| "host", addr, | ||
| "error", err, | ||
| ) | ||
| return | ||
| } | ||
| log.Debug("Connected", "address", addr) | ||
| }, | ||
| } | ||
|
|
||
| req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) | ||
|
|
||
| resp, err := t.Transport.RoundTrip(req) | ||
| if err != nil { | ||
| log.Debug("Failed to send request", | ||
| "url", req.URL.String(), | ||
| "error", err, | ||
| ) | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Debug("Request completed", | ||
| "status", resp.Status, | ||
| "duration", time.Since(startTime).Seconds(), | ||
| ) | ||
|
|
||
| return resp, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
source