-
Notifications
You must be signed in to change notification settings - Fork 20
Feat: Request Console Logging #30
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
Open
erancihan
wants to merge
10
commits into
doganarif:main
Choose a base branch
from
erancihan:feat/with-console-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d18372d
feat: ignore vendor directory
erancihan 2328d27
feat: add console logging utility
erancihan 6241f39
fix: typo
erancihan 0ce2ee3
fix: update wrap usage
erancihan 3221ae4
feat: add is tty check
erancihan bed9336
fix: update prompt prefix to match default url
erancihan c4aa097
feat: move config to own file
erancihan 7de5c81
feat: pass config referance to Wrap instead of expanding
erancihan 3b5ce66
feat: update logger initialization
erancihan 913c570
feat: clean up and document
erancihan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/doganarif/govisual/internal/model" | ||
| ) | ||
|
|
||
| const ( | ||
| green = "\033[32m" | ||
| white = "\033[37m" | ||
| red = "\033[31m" | ||
| blue = "\033[34m" | ||
| yellow = "\033[33m" | ||
| gray = "\033[90m" | ||
| black = "\033[30m" | ||
| magenta = "\033[35m" | ||
| cyan = "\033[36m" | ||
| reset = "\033[0m" | ||
| ) | ||
|
|
||
| func colorizeMethod(method string) string { | ||
| if method == "" { | ||
| return "" | ||
| } | ||
|
|
||
| var color string | ||
| switch method { | ||
| case http.MethodGet: | ||
| color = blue | ||
| case http.MethodPost: | ||
| color = green | ||
| case http.MethodPut: | ||
| color = yellow | ||
| case http.MethodDelete: | ||
| color = red | ||
| case http.MethodPatch: | ||
| color = magenta | ||
| case http.MethodHead: | ||
| color = gray | ||
| case http.MethodOptions: | ||
| color = cyan | ||
| case http.MethodTrace: | ||
| color = white | ||
| default: | ||
| color = black | ||
| } | ||
|
|
||
| return fmt.Sprintf("[%s%-7s%s]", color, method, reset) | ||
| } | ||
|
|
||
| func colorizeStatus(status int) string { | ||
| if status < 100 || status > 599 { | ||
| return fmt.Sprintf("[%s%3d%s]", red, status, reset) | ||
| } | ||
|
|
||
| var color string | ||
| switch { | ||
| case status >= http.StatusContinue && status < http.StatusOK: | ||
| color = gray | ||
| case status >= http.StatusOK && status < http.StatusMultipleChoices: | ||
| color = green | ||
| case status >= http.StatusMultipleChoices && status < http.StatusBadRequest: | ||
| color = white | ||
| case status >= http.StatusBadRequest && status < http.StatusInternalServerError: | ||
| color = yellow | ||
| default: | ||
| color = red | ||
| } | ||
|
|
||
| return fmt.Sprintf("[%s%3d%s]", color, status, reset) | ||
| } | ||
|
|
||
| func colorizeDuration(duration time.Duration) string { | ||
| if duration < 0 { | ||
| return fmt.Sprintf("%s%13v%s", red, duration, reset) | ||
| } | ||
|
|
||
| var color string | ||
| switch { | ||
| case duration < 500*time.Millisecond: | ||
| color = green | ||
| case duration < 1*time.Second: | ||
| color = yellow | ||
| default: | ||
| color = red | ||
| } | ||
|
|
||
| return fmt.Sprintf("%s%13v%s", color, duration, reset) | ||
| } | ||
|
|
||
| func LogRequest(reqLog *model.RequestLog) { | ||
| // This function logs the request details based on the configuration | ||
| if reqLog == nil { | ||
| fmt.Println("Warning: Attempted to log nil request log, ignoring") | ||
| return | ||
| } | ||
|
|
||
| fmt.Printf( | ||
| "[VIS] %v %s%s %s %#v\n", | ||
| reqLog.Timestamp.Format("2006-01-02 15:04:05"), | ||
| colorizeMethod(reqLog.Method), | ||
| colorizeStatus(reqLog.StatusCode), | ||
| colorizeDuration(time.Since(reqLog.Timestamp)), | ||
| reqLog.Path, | ||
| ) | ||
| } | ||
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,75 @@ | ||
| package main | ||
|
erancihan marked this conversation as resolved.
Outdated
|
||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/doganarif/govisual" | ||
| ) | ||
|
|
||
| func main() { | ||
| mux := http.NewServeMux() | ||
|
|
||
| // Add your routes | ||
| mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| // Example handler logic | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(`{"message": "Hello, user!"}`)) | ||
| }) | ||
|
|
||
| // add delay simulation route | ||
| mux.HandleFunc("/dalay/{duration}", func(w http.ResponseWriter, r *http.Request) { | ||
| durationStr := r.PathValue("duration") | ||
| duration, err := strconv.Atoi(durationStr) | ||
| if err != nil || duration < 0 { | ||
| http.Error(w, "Invalid duration", http.StatusBadRequest) | ||
| return | ||
| } | ||
| time.Sleep(time.Duration(duration) * time.Millisecond) | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(`{"message": "Delay completed"}`)) | ||
| }) | ||
|
|
||
| // add status code simulation route | ||
| mux.HandleFunc("/status/{code}", func(w http.ResponseWriter, r *http.Request) { | ||
| codeStr := r.PathValue("code") | ||
| code, err := strconv.Atoi(codeStr) | ||
| if err != nil || code < 100 || code > 599 { | ||
| http.Error(w, "Invalid status code", http.StatusBadRequest) | ||
| return | ||
| } | ||
| w.WriteHeader(code) | ||
| w.Write([]byte(`{"message": "Status code set"}`)) | ||
| }) | ||
|
|
||
| // add status code simulation route with delay | ||
| mux.HandleFunc("/status/{code}/delay/{duration}", func(w http.ResponseWriter, r *http.Request) { | ||
| codeStr := r.PathValue("code") | ||
| code, err := strconv.Atoi(codeStr) | ||
| if err != nil || code < 100 || code > 599 { | ||
| http.Error(w, "Invalid status code", http.StatusBadRequest) | ||
| return | ||
| } | ||
| durationStr := r.PathValue("duration") | ||
| duration, err := strconv.Atoi(durationStr) | ||
| if err != nil || duration < 0 { | ||
| http.Error(w, "Invalid duration", http.StatusBadRequest) | ||
| return | ||
| } | ||
| time.Sleep(time.Duration(duration) * time.Millisecond) | ||
| w.WriteHeader(code) | ||
| w.Write([]byte(`{"message": "Status code set with delay"}`)) | ||
| }) | ||
|
|
||
| // Wrap with GoVisual | ||
| handler := govisual.Wrap( | ||
| mux, | ||
| govisual.WithRequestBodyLogging(true), | ||
| govisual.WithResponseBodyLogging(true), | ||
| govisual.WithConsoleLogging(true), | ||
| ) | ||
|
|
||
| http.ListenAndServe(":8080", handler) | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.