110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"api-service/pkg/logger"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("Testing Dynamic Logging Functions...")
|
|
fmt.Println("====================================")
|
|
|
|
// Test fungsi penyimpanan log dinamis
|
|
testDynamicLogging()
|
|
|
|
// Tunggu sebentar untuk memastikan goroutine selesai
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
fmt.Println("\n====================================")
|
|
fmt.Println("Dynamic logging test completed!")
|
|
fmt.Println("Check the log files in pkg/logger/data/ directory")
|
|
}
|
|
|
|
func testDynamicLogging() {
|
|
// Buat logger instance
|
|
loggerInstance := logger.New("test-app", logger.DEBUG, false)
|
|
|
|
// Test 1: Log dengan penyimpanan otomatis
|
|
fmt.Println("\n1. Testing automatic log saving...")
|
|
loggerInstance.LogAndSave(logger.INFO, "Application started successfully", map[string]interface{}{
|
|
"version": "1.0.0",
|
|
"build_date": time.Now().Format("2006-01-02"),
|
|
"environment": "development",
|
|
})
|
|
|
|
// Test 2: Log dengan request context
|
|
fmt.Println("\n2. Testing log with request context...")
|
|
requestLogger := loggerInstance.WithRequestID("req-001").WithCorrelationID("corr-001")
|
|
requestLogger.LogAndSave(logger.INFO, "User login attempt", map[string]interface{}{
|
|
"username": "john_doe",
|
|
"ip": "192.168.1.100",
|
|
"success": true,
|
|
})
|
|
|
|
// Test 3: Error logging
|
|
fmt.Println("\n3. Testing error logging...")
|
|
loggerInstance.LogAndSave(logger.ERROR, "Database connection failed", map[string]interface{}{
|
|
"error": "connection timeout",
|
|
"retry_count": 3,
|
|
"host": "db.example.com:5432",
|
|
})
|
|
|
|
// Test 4: Manual log entry saving
|
|
fmt.Println("\n4. Testing manual log entry saving...")
|
|
manualEntry := logger.LogEntry{
|
|
Timestamp: time.Now().Format(time.RFC3339),
|
|
Level: "DEBUG",
|
|
Service: "manual-test",
|
|
Message: "Manual log entry created",
|
|
RequestID: "manual-req-001",
|
|
CorrelationID: "manual-corr-001",
|
|
File: "main.go",
|
|
Line: 42,
|
|
Fields: map[string]interface{}{
|
|
"custom_field": "test_value",
|
|
"number": 123,
|
|
"active": true,
|
|
},
|
|
}
|
|
|
|
// Simpan manual ke berbagai format
|
|
if err := logger.SaveLogText(manualEntry); err != nil {
|
|
log.Printf("Error saving text log: %v", err)
|
|
} else {
|
|
fmt.Println("✓ Text log saved successfully")
|
|
}
|
|
|
|
if err := logger.SaveLogJSON(manualEntry); err != nil {
|
|
log.Printf("Error saving JSON log: %v", err)
|
|
} else {
|
|
fmt.Println("✓ JSON log saved successfully")
|
|
}
|
|
|
|
if err := logger.SaveLogToDatabase(manualEntry); err != nil {
|
|
log.Printf("Error saving database log: %v", err)
|
|
} else {
|
|
fmt.Println("✓ Database log saved successfully")
|
|
}
|
|
|
|
// Test 5: Performance logging dengan durasi
|
|
fmt.Println("\n5. Testing performance logging...")
|
|
start := time.Now()
|
|
|
|
// Simulasi proses yang memakan waktu
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
duration := time.Since(start)
|
|
loggerInstance.LogAndSave(logger.INFO, "Data processing completed", map[string]interface{}{
|
|
"operation": "data_import",
|
|
"duration": duration.String(),
|
|
"duration_ms": duration.Milliseconds(),
|
|
"records": 1000,
|
|
"throughput": fmt.Sprintf("%.2f records/ms", 1000/float64(duration.Milliseconds())),
|
|
})
|
|
|
|
fmt.Println("\n✓ All logging tests completed successfully!")
|
|
}
|