Feat: Improve Logging for Go Server (#68)
This MR adds an optional `debug` object to the configuration table to help users debug any connectivity issues with Gitlab. The table lets you log the requests and responses from the Go server.
This commit is contained in:
committed by
GitHub
parent
c8a0267ba6
commit
f853c2f940
@@ -86,6 +86,7 @@ Here is the default setup function. All of these values are optional, and if you
|
|||||||
require("gitlab").setup({
|
require("gitlab").setup({
|
||||||
port = nil, -- The port of the Go server, which runs in the background, if omitted or `nil` the port will be chosen automatically
|
port = nil, -- The port of the Go server, which runs in the background, if omitted or `nil` the port will be chosen automatically
|
||||||
log_path = vim.fn.stdpath("cache") .. "/gitlab.nvim.log", -- Log path for the Go server
|
log_path = vim.fn.stdpath("cache") .. "/gitlab.nvim.log", -- Log path for the Go server
|
||||||
|
debug = { go_request = false, go_response = false }, -- Which values to log
|
||||||
reviewer = "delta", -- The reviewer type ("delta" or "diffview")
|
reviewer = "delta", -- The reviewer type ("delta" or "diffview")
|
||||||
attachment_dir = nil, -- The local directory for files (see the "summary" section)
|
attachment_dir = nil, -- The local directory for files (see the "summary" section)
|
||||||
popup = { -- The popup for comment creation, editing, and replying
|
popup = { -- The popup for comment creation, editing, and replying
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-retryablehttp"
|
||||||
"github.com/xanzy/go-gitlab"
|
"github.com/xanzy/go-gitlab"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,15 +19,17 @@ type Client struct {
|
|||||||
mergeId int
|
mergeId int
|
||||||
gitlabInstance string
|
gitlabInstance string
|
||||||
authToken string
|
authToken string
|
||||||
|
logPath string
|
||||||
|
debug bool
|
||||||
git *gitlab.Client
|
git *gitlab.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type Logger struct {
|
type DebugSettings struct {
|
||||||
Active bool
|
GoRequest bool `json:"go_request"`
|
||||||
|
GoResponse bool `json:"go_response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Logger) Printf(s string, args ...interface{}) {
|
var requestLogger retryablehttp.RequestLogHook = func(l retryablehttp.Logger, r *http.Request, i int) {
|
||||||
logString := fmt.Sprintf(s+"\n", args...)
|
|
||||||
logPath := os.Args[len(os.Args)-1]
|
logPath := os.Args[len(os.Args)-1]
|
||||||
|
|
||||||
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
@@ -33,7 +37,31 @@ func (l Logger) Printf(s string, args ...interface{}) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
_, err = file.Write([]byte(logString))
|
|
||||||
|
token := r.Header.Get("Private-Token")
|
||||||
|
r.Header.Set("Private-Token", "REDACTED")
|
||||||
|
res, err := httputil.DumpRequest(r, true)
|
||||||
|
r.Header.Set("Private-Token", token)
|
||||||
|
|
||||||
|
_, err = file.Write([]byte("\n-- REQUEST --\n"))
|
||||||
|
_, err = file.Write(res)
|
||||||
|
_, err = file.Write([]byte("\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var responseLogger retryablehttp.ResponseLogHook = func(l retryablehttp.Logger, response *http.Response) {
|
||||||
|
logPath := os.Args[len(os.Args)-1]
|
||||||
|
|
||||||
|
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
res, err := httputil.DumpResponse(response, true)
|
||||||
|
|
||||||
|
_, err = file.Write([]byte("\n-- RESPONSE --\n"))
|
||||||
|
_, err = file.Write(res)
|
||||||
|
_, err = file.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This will initialize the client with the token and check for the basic project ID and command arguments */
|
/* This will initialize the client with the token and check for the basic project ID and command arguments */
|
||||||
@@ -46,6 +74,15 @@ func (c *Client) init(branchName string) error {
|
|||||||
projectId := os.Args[1]
|
projectId := os.Args[1]
|
||||||
gitlabInstance := os.Args[2]
|
gitlabInstance := os.Args[2]
|
||||||
authToken := os.Args[4]
|
authToken := os.Args[4]
|
||||||
|
debugSettings := os.Args[5]
|
||||||
|
|
||||||
|
var debugObject DebugSettings
|
||||||
|
err := json.Unmarshal([]byte(debugSettings), &debugObject)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Could not parse debug settings: %w, %s", err, debugSettings)
|
||||||
|
}
|
||||||
|
|
||||||
|
logPath := os.Args[len(os.Args)-1]
|
||||||
|
|
||||||
if projectId == "" {
|
if projectId == "" {
|
||||||
return errors.New("Project ID cannot be empty")
|
return errors.New("Project ID cannot be empty")
|
||||||
@@ -62,11 +99,23 @@ func (c *Client) init(branchName string) error {
|
|||||||
c.gitlabInstance = gitlabInstance
|
c.gitlabInstance = gitlabInstance
|
||||||
c.projectId = projectId
|
c.projectId = projectId
|
||||||
c.authToken = authToken
|
c.authToken = authToken
|
||||||
|
c.logPath = logPath
|
||||||
|
|
||||||
var l Logger
|
|
||||||
var apiCustUrl = fmt.Sprintf(c.gitlabInstance + "/api/v4")
|
var apiCustUrl = fmt.Sprintf(c.gitlabInstance + "/api/v4")
|
||||||
|
|
||||||
git, err := gitlab.NewClient(authToken, gitlab.WithBaseURL(apiCustUrl), gitlab.WithCustomLogger(l))
|
gitlabOptions := []gitlab.ClientOptionFunc{
|
||||||
|
gitlab.WithBaseURL(apiCustUrl),
|
||||||
|
}
|
||||||
|
|
||||||
|
if debugObject.GoRequest {
|
||||||
|
gitlabOptions = append(gitlabOptions, gitlab.WithRequestLogHook(requestLogger))
|
||||||
|
}
|
||||||
|
|
||||||
|
if debugObject.GoResponse {
|
||||||
|
gitlabOptions = append(gitlabOptions, gitlab.WithResponseLogHook(responseLogger))
|
||||||
|
}
|
||||||
|
|
||||||
|
git, err := gitlab.NewClient(authToken, gitlabOptions...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to create client: %v", err)
|
return fmt.Errorf("Failed to create client: %v", err)
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ M.start = function(callback)
|
|||||||
.. " "
|
.. " "
|
||||||
.. state.settings.auth_token
|
.. state.settings.auth_token
|
||||||
.. " "
|
.. " "
|
||||||
|
.. "'" .. vim.json.encode(state.settings.debug) .. "'"
|
||||||
|
.. " "
|
||||||
.. state.settings.log_path
|
.. state.settings.log_path
|
||||||
|
|
||||||
local job_id = vim.fn.jobstart(command, {
|
local job_id = vim.fn.jobstart(command, {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ local M = {}
|
|||||||
-- These are the default settings for the plugin
|
-- These are the default settings for the plugin
|
||||||
M.settings = {
|
M.settings = {
|
||||||
port = nil, -- choose random port
|
port = nil, -- choose random port
|
||||||
|
debug = { go_request = false, go_response = false },
|
||||||
log_path = (vim.fn.stdpath("cache") .. "/gitlab.nvim.log"),
|
log_path = (vim.fn.stdpath("cache") .. "/gitlab.nvim.log"),
|
||||||
reviewer = "delta",
|
reviewer = "delta",
|
||||||
attachment_dir = "",
|
attachment_dir = "",
|
||||||
|
|||||||
Reference in New Issue
Block a user