106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// ExampleDynamicLogging menunjukkan cara menggunakan fungsi penyimpanan log dinamis
|
|
func ExampleDynamicLogging() {
|
|
// Buat logger instance
|
|
logger := New("test-service", DEBUG, false)
|
|
|
|
// Contoh 1: Log biasa dengan penyimpanan otomatis
|
|
fmt.Println("=== Contoh 1: Log biasa dengan penyimpanan otomatis ===")
|
|
logger.LogAndSave(INFO, "Aplikasi dimulai", map[string]interface{}{
|
|
"version": "1.0.0",
|
|
"mode": "development",
|
|
})
|
|
|
|
// Contoh 2: Log dengan request ID
|
|
fmt.Println("\n=== Contoh 2: Log dengan request ID ===")
|
|
reqLogger := logger.WithRequestID("req-123456")
|
|
reqLogger.LogAndSave(INFO, "Request diproses", map[string]interface{}{
|
|
"endpoint": "/api/v1/users",
|
|
"method": "GET",
|
|
"user_id": 1001,
|
|
})
|
|
|
|
// Contoh 3: Log error
|
|
fmt.Println("\n=== Contoh 3: Log error ===")
|
|
logger.LogAndSave(ERROR, "Database connection failed", map[string]interface{}{
|
|
"error": "connection timeout",
|
|
"timeout": "30s",
|
|
"host": "localhost:5432",
|
|
})
|
|
|
|
// Contoh 4: Manual save ke berbagai format
|
|
fmt.Println("\n=== Contoh 4: Manual save ke berbagai format ===")
|
|
manualEntry := LogEntry{
|
|
Timestamp: time.Now().Format(time.RFC3339),
|
|
Level: "INFO",
|
|
Service: "manual-service",
|
|
Message: "Manual log entry",
|
|
RequestID: "manual-req-001",
|
|
File: "example.go",
|
|
Line: 42,
|
|
Fields: map[string]interface{}{
|
|
"custom_field": "custom_value",
|
|
"number": 42,
|
|
},
|
|
}
|
|
|
|
// Simpan manual ke berbagai format
|
|
if err := SaveLogText(manualEntry); err != nil {
|
|
fmt.Printf("Error saving text log: %v\n", err)
|
|
}
|
|
|
|
if err := SaveLogJSON(manualEntry); err != nil {
|
|
fmt.Printf("Error saving JSON log: %v\n", err)
|
|
}
|
|
|
|
if err := SaveLogToDatabase(manualEntry); err != nil {
|
|
fmt.Printf("Error saving to database log: %v\n", err)
|
|
}
|
|
|
|
// Contoh 5: Log dengan durasi
|
|
fmt.Println("\n=== Contoh 5: Log dengan durasi ===")
|
|
start := time.Now()
|
|
time.Sleep(100 * time.Millisecond) // Simulasi proses
|
|
duration := time.Since(start)
|
|
|
|
logger.LogAndSave(INFO, "Process completed", map[string]interface{}{
|
|
"operation": "data_processing",
|
|
"duration": duration.String(),
|
|
"items": 150,
|
|
})
|
|
|
|
fmt.Println("\n=== Semua log telah disimpan dalam berbagai format ===")
|
|
fmt.Println("1. Format teks dengan pemisah |: pkg/logger/data/logs.txt")
|
|
fmt.Println("2. Format JSON: pkg/logger/data/logs.json")
|
|
fmt.Println("3. Format database (placeholder): pkg/logger/data/database_logs.txt")
|
|
}
|
|
|
|
// ExampleMiddlewareLogging menunjukkan penggunaan dalam middleware
|
|
func ExampleMiddlewareLogging() {
|
|
fmt.Println("\n=== Contoh Penggunaan dalam Middleware ===")
|
|
|
|
middlewareLogger := New("middleware-service", INFO, false)
|
|
|
|
// Simulasi request processing
|
|
middlewareLogger.LogAndSave(INFO, "Request received", map[string]interface{}{
|
|
"method": "POST",
|
|
"path": "/api/v1/auth/login",
|
|
"client_ip": "192.168.1.100",
|
|
"user_agent": "Mozilla/5.0",
|
|
"content_type": "application/json",
|
|
})
|
|
|
|
// Simulasi response
|
|
middlewareLogger.LogAndSave(INFO, "Response sent", map[string]interface{}{
|
|
"status_code": 200,
|
|
"duration": "150ms",
|
|
"response_size": "2.5KB",
|
|
})
|
|
}
|