From 6ac9ac29d835dfe32c3f65e423798cbd7df5195b Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 7 Nov 2024 16:41:08 +0300 Subject: [PATCH 1/6] add simple http logging --- remote/accountservice.go | 44 +++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/remote/accountservice.go b/remote/accountservice.go index 2e19de1..92fd9df 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -1,20 +1,26 @@ package remote import ( + "bytes" "context" "encoding/json" "errors" "io" + "log" "net/http" "net/url" + "os" - dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" - "github.com/grassrootseconomics/eth-custodial/pkg/api" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/models" + "github.com/grassrootseconomics/eth-custodial/pkg/api" + dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) var ( + DebugLogger = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile) + InfoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime) + ErrorLogger = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) ) type AccountServiceInterface interface { @@ -51,7 +57,7 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri return nil, err } - _, err = doCustodialRequest(ctx, req, &r) + _, err = doCustodialRequest(ctx, req, &r) if err != nil { return nil, err } @@ -79,7 +85,6 @@ func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (* return &balanceResult, err } - // CreateAccount creates a new account in the custodial system. // Returns: // - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account. @@ -94,7 +99,7 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes return nil, err } - _, err = doCustodialRequest(ctx, req, &r) + _, err = doCustodialRequest(ctx, req, &r) if err != nil { return nil, err } @@ -118,7 +123,7 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ( return nil, err } - _, err = doDataRequest(ctx, req, r) + _, err = doDataRequest(ctx, req, r) if err != nil { return nil, err } @@ -126,7 +131,6 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ( return r, nil } - // FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint // Parameters: // - publicKey: The public key associated with the account. @@ -143,7 +147,7 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin return nil, err } - _, err = doDataRequest(ctx, req, r) + _, err = doDataRequest(ctx, req, r) if err != nil { return nil, err } @@ -151,7 +155,6 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin return r, nil } - // VoucherData retrieves voucher metadata from the data indexer API endpoint. // Parameters: // - address: The voucher address. @@ -173,7 +176,7 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod } func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { - var okResponse api.OKResponse + var okResponse api.OKResponse var errResponse api.ErrResponse req.Header.Set("Content-Type", "application/json") @@ -184,6 +187,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons } defer resp.Body.Close() + InfoLogger.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type")) body, err := io.ReadAll(resp.Body) if err != nil { return nil, err @@ -214,10 +218,30 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { req.Header.Set("X-GE-KEY", config.CustodialAPIKey) + logRequestDetails(req, config.CustodialAPIKey) return doRequest(ctx, req, rcpt) } func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { req.Header.Set("X-GE-KEY", config.DataAPIKey) + logRequestDetails(req, config.CustodialAPIKey) return doRequest(ctx, req, rcpt) } + +func logRequestDetails(req *http.Request, apiKey string) { + var bodyBytes []byte + contentType := req.Header.Get("Content-Type") + if req.Body != nil { + bodyBytes, err := io.ReadAll(req.Body) + if err != nil { + ErrorLogger.Printf("Error reading request body: %s", err) + return + } + req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) + } else { + bodyBytes = []byte("-") + } + + InfoLogger.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) + +} From a11776e1b38c20092e4e8d9307993b51acf82cc4 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 7 Nov 2024 16:41:38 +0300 Subject: [PATCH 2/6] ignore .log files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ddccccf..b523c77 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ go.work* cmd/.state/ id_* *.gdbm +*.log From 9c972ffa6bb0faf851c4c6e607178e905e3937a9 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 7 Nov 2024 16:46:12 +0300 Subject: [PATCH 3/6] add specific log files per binary --- cmd/africastalking/main.go | 42 ++++++++++++++++++++++++++++++++++++++ cmd/async/main.go | 19 ++++++++++++++++- cmd/http/main.go | 18 ++++++++++++++++ cmd/main.go | 18 ++++++++++++++++ remote/accountservice.go | 22 +++++++++++--------- 5 files changed, 108 insertions(+), 11 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index db66a2e..15a13ee 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -1,9 +1,13 @@ package main import ( + "bytes" "context" + "encoding/json" "flag" "fmt" + "io" + "log" "net/http" "os" "os/signal" @@ -27,10 +31,27 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") + WarningLogger *log.Logger + InfoLogger *log.Logger + ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() + + logFile := "urdt-ussd-africastalking.log" + + file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + + // Inject into remote package + remote.InfoLogger = InfoLogger + remote.ErrorLogger = ErrorLogger } type atRequestParser struct{} @@ -38,9 +59,30 @@ type atRequestParser struct{} func (arp *atRequestParser) GetSessionId(rq any) (string, error) { rqv, ok := rq.(*http.Request) if !ok { + ErrorLogger.Println("got an invalid request:", rq) return "", handlers.ErrInvalidRequest } + + // Capture body (if any) for logging + body, err := io.ReadAll(rqv.Body) + if err != nil { + ErrorLogger.Println("failed to read request body:", err) + return "", fmt.Errorf("failed to read request body: %v", err) + } + // Reset the body for further reading + rqv.Body = io.NopCloser(bytes.NewReader(body)) + + // Log the body as JSON + bodyLog := map[string]string{"body": string(body)} + logBytes, err := json.Marshal(bodyLog) + if err != nil { + ErrorLogger.Println("failed to marshal request body:", err) + } else { + InfoLogger.Println("Received request:", string(logBytes)) + } + if err := rqv.ParseForm(); err != nil { + ErrorLogger.Println("failed to parse form data: %v", err) return "", fmt.Errorf("failed to parse form data: %v", err) } diff --git a/cmd/async/main.go b/cmd/async/main.go index e4c94b0..0dd7c2c 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "log" "os" "os/signal" "path" @@ -23,12 +24,28 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") + WarningLogger *log.Logger + InfoLogger *log.Logger + ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() -} + logFile := "urdt-ussd-async.log" + + file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + + // Inject into remote package + remote.InfoLogger = InfoLogger + remote.ErrorLogger = ErrorLogger +} type asyncRequestParser struct { sessionId string input []byte diff --git a/cmd/http/main.go b/cmd/http/main.go index 96e2688..ffb4109 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "log" "net/http" "os" "os/signal" @@ -26,10 +27,27 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") + WarningLogger *log.Logger + InfoLogger *log.Logger + ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() + + logFile := "urdt-ussd-http.log" + + file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + + // Inject into remote package + remote.InfoLogger = InfoLogger + remote.ErrorLogger = ErrorLogger } func main() { diff --git a/cmd/main.go b/cmd/main.go index 9599eb7..a623cbe 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "log" "os" "path" @@ -20,10 +21,27 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") + WarningLogger *log.Logger + InfoLogger *log.Logger + ErrorLogger *log.Logger ) func init() { initializers.LoadEnvVariables() + + logFile := "urdt-ussd-cli.log" + + file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + + // Inject into remote package + remote.InfoLogger = InfoLogger + remote.ErrorLogger = ErrorLogger } func main() { diff --git a/remote/accountservice.go b/remote/accountservice.go index 2e19de1..6e222e7 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -5,16 +5,19 @@ import ( "encoding/json" "errors" "io" + "log" "net/http" "net/url" - dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" - "github.com/grassrootseconomics/eth-custodial/pkg/api" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/models" + "github.com/grassrootseconomics/eth-custodial/pkg/api" + dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) var ( + InfoLogger *log.Logger + ErrorLogger *log.Logger ) type AccountServiceInterface interface { @@ -51,7 +54,7 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri return nil, err } - _, err = doCustodialRequest(ctx, req, &r) + _, err = doCustodialRequest(ctx, req, &r) if err != nil { return nil, err } @@ -79,7 +82,6 @@ func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (* return &balanceResult, err } - // CreateAccount creates a new account in the custodial system. // Returns: // - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account. @@ -94,7 +96,7 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes return nil, err } - _, err = doCustodialRequest(ctx, req, &r) + _, err = doCustodialRequest(ctx, req, &r) if err != nil { return nil, err } @@ -118,7 +120,7 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ( return nil, err } - _, err = doDataRequest(ctx, req, r) + _, err = doDataRequest(ctx, req, r) if err != nil { return nil, err } @@ -126,7 +128,6 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ( return r, nil } - // FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint // Parameters: // - publicKey: The public key associated with the account. @@ -143,7 +144,7 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin return nil, err } - _, err = doDataRequest(ctx, req, r) + _, err = doDataRequest(ctx, req, r) if err != nil { return nil, err } @@ -151,7 +152,6 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin return r, nil } - // VoucherData retrieves voucher metadata from the data indexer API endpoint. // Parameters: // - address: The voucher address. @@ -173,9 +173,11 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod } func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { - var okResponse api.OKResponse + var okResponse api.OKResponse var errResponse api.ErrResponse + InfoLogger.Printf("Outgoing request:", req.URL, req.Body) + req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { From 1bcbb2079e81243e00198002cf8ae4f08303d570 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 8 Nov 2024 00:17:02 +0300 Subject: [PATCH 4/6] Removed unused variable --- remote/accountservice.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/remote/accountservice.go b/remote/accountservice.go index ef0d4ae..ae1fab7 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -9,7 +9,6 @@ import ( "log" "net/http" "net/url" - "os" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/models" @@ -178,8 +177,6 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons var okResponse api.OKResponse var errResponse api.ErrResponse - InfoLogger.Printf("Outgoing request:", req.URL, req.Body) - req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { @@ -219,17 +216,17 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { req.Header.Set("X-GE-KEY", config.CustodialAPIKey) - logRequestDetails(req, config.CustodialAPIKey) + logRequestDetails(req) return doRequest(ctx, req, rcpt) } func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { req.Header.Set("X-GE-KEY", config.DataAPIKey) - logRequestDetails(req, config.CustodialAPIKey) + logRequestDetails(req) return doRequest(ctx, req, rcpt) } -func logRequestDetails(req *http.Request, apiKey string) { +func logRequestDetails(req *http.Request) { var bodyBytes []byte contentType := req.Header.Get("Content-Type") if req.Body != nil { @@ -244,5 +241,4 @@ func logRequestDetails(req *http.Request, apiKey string) { } InfoLogger.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) - } From 64a7b492186384b4b6e6a71e99de76069f7bf880 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 8 Nov 2024 00:21:11 +0300 Subject: [PATCH 5/6] Remove unused Warning logger --- cmd/africastalking/main.go | 1 - cmd/async/main.go | 1 - cmd/http/main.go | 1 - cmd/main.go | 1 - 4 files changed, 4 deletions(-) diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 15a13ee..7616473 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -31,7 +31,6 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - WarningLogger *log.Logger InfoLogger *log.Logger ErrorLogger *log.Logger ) diff --git a/cmd/async/main.go b/cmd/async/main.go index 0dd7c2c..afc0bca 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -24,7 +24,6 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - WarningLogger *log.Logger InfoLogger *log.Logger ErrorLogger *log.Logger ) diff --git a/cmd/http/main.go b/cmd/http/main.go index ffb4109..0565c45 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -27,7 +27,6 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - WarningLogger *log.Logger InfoLogger *log.Logger ErrorLogger *log.Logger ) diff --git a/cmd/main.go b/cmd/main.go index a623cbe..857ccb9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,7 +21,6 @@ import ( var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") - WarningLogger *log.Logger InfoLogger *log.Logger ErrorLogger *log.Logger ) From dcd8fce59a81bca56961e94fb1625ce601e06e95 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 8 Nov 2024 10:07:06 +0300 Subject: [PATCH 6/6] add log on create account --- remote/accountservice.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/remote/accountservice.go b/remote/accountservice.go index ae1fab7..e6a5556 100644 --- a/remote/accountservice.go +++ b/remote/accountservice.go @@ -96,9 +96,9 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes if err != nil { return nil, err } - _, err = doCustodialRequest(ctx, req, &r) if err != nil { + log.Printf("Failed to make custodial %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error()) return nil, err } @@ -176,7 +176,6 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { var okResponse api.OKResponse var errResponse api.ErrResponse - req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil {