Merge pull request 'log-file' (#166) from log-file into http-logs
Reviewed-on: #166
This commit is contained in:
commit
7a02ffcf0c
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ go.work*
|
||||
cmd/.state/
|
||||
id_*
|
||||
*.gdbm
|
||||
*.log
|
||||
|
@ -1,9 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -27,10 +31,26 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
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 +58,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)
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@ -23,12 +24,27 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
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
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -26,10 +27,26 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
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() {
|
||||
|
17
cmd/main.go
17
cmd/main.go
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
@ -20,10 +21,26 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
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() {
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/models"
|
||||
@ -18,9 +17,8 @@ import (
|
||||
)
|
||||
|
||||
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)
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
type AccountServiceInterface interface {
|
||||
@ -98,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
|
||||
}
|
||||
|
||||
@ -178,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 {
|
||||
@ -218,17 +215,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 {
|
||||
@ -243,5 +240,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))
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user