From 782a358af6a7ccdd3056ccb305258f2d7cb5b015 Mon Sep 17 00:00:00 2001 From: golnar-boosty Date: Tue, 19 Nov 2024 18:55:54 +0000 Subject: [PATCH] feat(logs): add log generation functionality in the log folder --- logs/logger.go | 31 ++++++++++++++++++++++++++++ logs/monitor.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ logs/recover.go | 14 +++++++++++++ 3 files changed, 99 insertions(+) create mode 100755 logs/logger.go create mode 100755 logs/monitor.go create mode 100755 logs/recover.go diff --git a/logs/logger.go b/logs/logger.go new file mode 100755 index 0000000..a78beb9 --- /dev/null +++ b/logs/logger.go @@ -0,0 +1,31 @@ +package log + +import ( + "fmt" + "os" + "strconv" + "time" + + cosmosLog "cosmossdk.io/log" +) + +func CustomLogger() cosmosLog.Logger { + + printLogs, err := strconv.ParseBool(os.Getenv("PrintLogs")) + if err != nil { + fmt.Println("[CustomLogger] Error parsing PrintLogs environment variable:", err) + } + + if !printLogs { + return cosmosLog.NewNopLogger() + } + + // Initialize a new `cosmosLog` logger instance + logger := cosmosLog.NewLogger(os.Stderr) + + logger = logger.With( + "timestamp", time.Now().UTC().Format(time.RFC3339), + ) + + return logger +} diff --git a/logs/monitor.go b/logs/monitor.go new file mode 100755 index 0000000..68288d0 --- /dev/null +++ b/logs/monitor.go @@ -0,0 +1,54 @@ +package log + +import ( + "fmt" + "os" + "runtime" + "strconv" + "time" + + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/load" + "github.com/shirou/gopsutil/mem" + "github.com/sirupsen/logrus" +) + +var log = logrus.New() + +// Monitor will continuously collect system information +func Monitor(interval time.Duration) { + + printLogs, err := strconv.ParseBool(os.Getenv("PrintLogs")) + + if err != nil { + fmt.Println("[CustomLogger] Error parsing PrintLogs environment variable:", err) + } + + if printLogs { + for { + var memStats runtime.MemStats + runtime.ReadMemStats(&memStats) + + v, _ := mem.VirtualMemory() + cpuPercent, _ := cpu.Percent(0, true) + loadAvg, _ := load.Avg() + + log.Printf("Timestamp: %v", time.Now().Format(time.RFC3339)) + + log.Printf("Memory Total: %v, Free: %v, Used Percent: %.2f%%, Active: %v", + v.Total, v.Free, v.UsedPercent, v.Active) + + log.Printf("CPU Usage Percentage: %v%%, CPU Usage: %v", cpuPercent, runtime.NumCPU()) + + // It logs the system load average for 1, 5, and 15 minutes. + log.Printf("Load Average: 1m: %.2f, 5m: %.2f, 15m: %.2f", + loadAvg.Load1, loadAvg.Load5, loadAvg.Load15) + + log.Printf("Memory Alloc: %v, Total Memory Alloc: %v, System Memory: %v", + memStats.Alloc, memStats.TotalAlloc, memStats.Sys) + + time.Sleep(interval) + } + } + +} diff --git a/logs/recover.go b/logs/recover.go new file mode 100755 index 0000000..d87cd61 --- /dev/null +++ b/logs/recover.go @@ -0,0 +1,14 @@ +package log + +import ( + "github.com/sirupsen/logrus" +) + +var logger = logrus.New() + +// Recovery function to handle panics +func RecoverFromPanic() { + if r := recover(); r != nil { + logger.Errorf("Application crashed: %v", r) + } +}