log directly to the terminal
This commit is contained in:
		
							parent
							
								
									0bb444cd50
								
							
						
					
					
						commit
						9af7b775a7
					
				@ -31,26 +31,10 @@ 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{}
 | 
			
		||||
@ -58,14 +42,14 @@ 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)
 | 
			
		||||
		log.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)
 | 
			
		||||
		log.Println("failed to read request body:", err)
 | 
			
		||||
		return "", fmt.Errorf("failed to read request body: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	// Reset the body for further reading
 | 
			
		||||
@ -75,13 +59,13 @@ func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
 | 
			
		||||
	bodyLog := map[string]string{"body": string(body)}
 | 
			
		||||
	logBytes, err := json.Marshal(bodyLog)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ErrorLogger.Println("failed to marshal request body:", err)
 | 
			
		||||
		log.Println("failed to marshal request body:", err)
 | 
			
		||||
	} else {
 | 
			
		||||
		InfoLogger.Println("Received request:", string(logBytes))
 | 
			
		||||
		log.Println("Received request:", string(logBytes))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := rqv.ParseForm(); err != nil {
 | 
			
		||||
		ErrorLogger.Println("failed to parse form data: %v", err)
 | 
			
		||||
		log.Println("failed to parse form data: %v", err)
 | 
			
		||||
		return "", fmt.Errorf("failed to parse form data: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,6 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/signal"
 | 
			
		||||
	"path"
 | 
			
		||||
@ -24,27 +23,12 @@ 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,7 +4,6 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/signal"
 | 
			
		||||
@ -27,26 +26,10 @@ 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,7 +4,6 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
 | 
			
		||||
@ -21,26 +20,10 @@ 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() {
 | 
			
		||||
 | 
			
		||||
@ -16,11 +16,6 @@ import (
 | 
			
		||||
	dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	InfoLogger  *log.Logger
 | 
			
		||||
	ErrorLogger *log.Logger
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type AccountServiceInterface interface {
 | 
			
		||||
	CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error)
 | 
			
		||||
	CreateAccount(ctx context.Context) (*models.AccountResult, error)
 | 
			
		||||
@ -98,7 +93,6 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes
 | 
			
		||||
	}
 | 
			
		||||
	_, 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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -179,12 +173,13 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
 | 
			
		||||
	req.Header.Set("Content-Type", "application/json")
 | 
			
		||||
	resp, err := http.DefaultClient.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("Failed to make %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error())
 | 
			
		||||
		errResponse.Description = err.Error()
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	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"))
 | 
			
		||||
	log.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
 | 
			
		||||
@ -231,7 +226,7 @@ func logRequestDetails(req *http.Request) {
 | 
			
		||||
	if req.Body != nil {
 | 
			
		||||
		bodyBytes, err := io.ReadAll(req.Body)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ErrorLogger.Printf("Error reading request body: %s", err)
 | 
			
		||||
			log.Printf("Error reading request body: %s", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
 | 
			
		||||
@ -239,5 +234,5 @@ func logRequestDetails(req *http.Request) {
 | 
			
		||||
		bodyBytes = []byte("-")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	InfoLogger.Printf("URL: %s  | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes))
 | 
			
		||||
	log.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