feat(ws server): added in a websocket on port system to help with better logging
This commit is contained in:
67
backend/utils/db/config.go
Normal file
67
backend/utils/db/config.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
gorm.Model
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
Name string `gorm:"uniqueIndex;not null"`
|
||||
Description string `gorm:"type:text"`
|
||||
Value string `gorm:"not null"`
|
||||
Enabled bool `gorm:"default:true"`
|
||||
AppService string `gorm:"default:system"`
|
||||
CreatedAt time.Time `gorm:"index"`
|
||||
UpdatedAt time.Time `gorm:"index"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
var seedConfigData = []Config{
|
||||
{Name: "serverPort", Description: "The port the server will listen on if not running in docker", Value: "4000", Enabled: true},
|
||||
{Name: "server", Description: "The server we will use when connecting to the alplaprod sql", Value: "usmcd1vms006", Enabled: true},
|
||||
}
|
||||
|
||||
func SeedConfigs(db *gorm.DB) error {
|
||||
|
||||
for _, cfg := range seedConfigData {
|
||||
var existing Config
|
||||
// Try to find config by unique name
|
||||
result := db.Where("name =?", cfg.Name).First(&existing)
|
||||
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
// not here lets add it
|
||||
if err := db.Create(&cfg).Error; err != nil {
|
||||
log.Printf("Failed to seed config %s: %v", cfg.Name, err)
|
||||
}
|
||||
log.Printf("Seeded new config: %s", cfg.Name)
|
||||
} else {
|
||||
// Some other error
|
||||
return result.Error
|
||||
}
|
||||
} else {
|
||||
// only update the fields we want to update.
|
||||
existing.Description = cfg.Description
|
||||
if err := db.Save(&existing).Error; err != nil {
|
||||
log.Printf("Failed to update config %s: %v", cfg.Name, err)
|
||||
return err
|
||||
}
|
||||
log.Printf("Updated existing config: %s", cfg.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllConfigs(db *gorm.DB) ([]Config, error) {
|
||||
var configs []Config
|
||||
|
||||
result := db.Find(&configs)
|
||||
|
||||
return configs, result.Error
|
||||
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
type JSONB map[string]interface{}
|
||||
|
||||
func InitDB() error {
|
||||
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s",
|
||||
os.Getenv("DB_HOST"),
|
||||
@@ -25,8 +27,17 @@ func InitDB() error {
|
||||
return fmt.Errorf("failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// Auto-migrate all models
|
||||
DB.AutoMigrate(&Log{}) // Add other models here
|
||||
fmt.Println("✅ Connected to database")
|
||||
|
||||
// ensures we have the uuid stuff setup properly
|
||||
DB.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`)
|
||||
|
||||
err = DB.AutoMigrate(&Log{}, &Config{}, &ClientRecord{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to auto-migrate models: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("✅ Database migration completed successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
package db
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Log struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
LogID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
|
||||
Level string `gorm:"size:10;not null"` // "info", "error", etc.
|
||||
Message string `gorm:"not null"`
|
||||
Service string `gorm:"size:50"` // Optional: service name
|
||||
Metadata JSONB `gorm:"type:jsonb"` // Structured fields (e.g., {"user_id": 123})
|
||||
CreatedAt time.Time `gorm:"index"` // Auto-set by GORM
|
||||
Service string `gorm:"size:50"`
|
||||
Metadata JSONB `gorm:"type:jsonb"` // fields (e.g., {"user_id": 123})
|
||||
CreatedAt time.Time `gorm:"index"`
|
||||
Checked bool `gorm:"type:boolean;default:false"`
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
// JSONB is a helper type for PostgreSQL JSONB fields.
|
||||
type JSONB map[string]interface{}
|
||||
//type JSONB map[string]interface{}
|
||||
|
||||
// --- CRUD Operations ---
|
||||
|
||||
@@ -29,13 +36,13 @@ func CreateLog(level, message, service string, metadata JSONB) error {
|
||||
}
|
||||
|
||||
// GetLogsByLevel fetches logs filtered by severity.
|
||||
func GetLogsByLevel(level string, limit int) ([]Log, error) {
|
||||
func GetLogs(level string, limit int, service string) ([]Log, error) {
|
||||
var logs []Log
|
||||
err := DB.Where("level = ?", level).Limit(limit).Find(&logs).Error
|
||||
err := DB.Where("level = ? and service = ?", level, service).Limit(limit).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
// DeleteOldLogs removes logs older than `days`.
|
||||
func DeleteOldLogs(days int) error {
|
||||
return DB.Where("created_at < ?", time.Now().AddDate(0, 0, -days)).Delete(&Log{}).Error
|
||||
// DeleteOldLogs removes logs older than `days` and by level.
|
||||
func DeleteOldLogs(days int, level string) error {
|
||||
return DB.Where("created_at < ? and level = ?", time.Now().AddDate(0, 0, -days), level).Delete(&Log{}).Error
|
||||
}
|
||||
|
||||
20
backend/utils/db/ws_clients.go
Normal file
20
backend/utils/db/ws_clients.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ClientRecord struct {
|
||||
ClientID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
|
||||
APIKey string `gorm:"not null"`
|
||||
IPAddress string `gorm:"not null"`
|
||||
UserAgent string `gorm:"size:255"`
|
||||
ConnectedAt time.Time `gorm:"index"`
|
||||
LastHeartbeat time.Time `gorm:"index"`
|
||||
Channels JSONB `gorm:"type:jsonb"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DisconnectedAt *time.Time `gorm:"column:disconnected_at"`
|
||||
}
|
||||
116
backend/utils/logger/logger.go
Normal file
116
backend/utils/logger/logger.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"lst.net/utils/db"
|
||||
)
|
||||
|
||||
type CustomLogger struct {
|
||||
consoleLogger zerolog.Logger
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Channel string `json:"channel"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// New creates a configured CustomLogger.
|
||||
func New() *CustomLogger {
|
||||
// Colorized console output
|
||||
consoleWriter := zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
}
|
||||
|
||||
return &CustomLogger{
|
||||
consoleLogger: zerolog.New(consoleWriter).
|
||||
With().
|
||||
Timestamp().
|
||||
Logger(),
|
||||
}
|
||||
}
|
||||
|
||||
func PrettyFormat(level, message string, metadata map[string]interface{}) string {
|
||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||
base := fmt.Sprintf("[%s] %s| Message: %s", strings.ToUpper(level), timestamp, message)
|
||||
|
||||
if len(metadata) > 0 {
|
||||
metaJSON, _ := json.Marshal(metadata)
|
||||
return fmt.Sprintf("%s | Metadata: %s", base, string(metaJSON))
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
func (l *CustomLogger) logToPostgres(level, message, service string, metadata map[string]interface{}) {
|
||||
err := db.CreateLog(level, message, service, metadata)
|
||||
if err != nil {
|
||||
// Fallback to console if DB fails
|
||||
log.Error().Err(err).Msg("Failed to write log to PostgreSQL")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Level-Specific Methods ---
|
||||
|
||||
func (l *CustomLogger) Info(message, service string, fields map[string]interface{}) {
|
||||
l.consoleLogger.Info().Fields(fields).Msg(message)
|
||||
l.logToPostgres("info", message, service, fields)
|
||||
|
||||
//PostLog(PrettyFormat("info", message, fields)) // Broadcast pretty message
|
||||
}
|
||||
|
||||
func (l *CustomLogger) Warn(message, service string, fields map[string]interface{}) {
|
||||
l.consoleLogger.Error().Fields(fields).Msg(message)
|
||||
l.logToPostgres("warn", message, service, fields)
|
||||
|
||||
//PostLog(PrettyFormat("warn", message, fields)) // Broadcast pretty message
|
||||
|
||||
// Custom logic for errors (e.g., alerting)
|
||||
if len(fields) > 0 {
|
||||
l.consoleLogger.Warn().Msg("Additional error context captured")
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CustomLogger) Error(message, service string, fields map[string]interface{}) {
|
||||
l.consoleLogger.Error().Fields(fields).Msg(message)
|
||||
l.logToPostgres("error", message, service, fields)
|
||||
|
||||
//PostLog(PrettyFormat("error", message, fields)) // Broadcast pretty message
|
||||
|
||||
// Custom logic for errors (e.g., alerting)
|
||||
if len(fields) > 0 {
|
||||
l.consoleLogger.Warn().Msg("Additional error context captured")
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CustomLogger) Panic(message, service string, fields map[string]interface{}) {
|
||||
// Log to console (colored, with fields)
|
||||
l.consoleLogger.Error().
|
||||
Str("service", service).
|
||||
Fields(fields).
|
||||
Msg(message + " (PANIC)") // Explicitly mark as panic
|
||||
|
||||
// Log to PostgreSQL (sync to ensure it's saved before crashing)
|
||||
err := db.CreateLog("panic", message, service, fields) // isCritical=true
|
||||
if err != nil {
|
||||
l.consoleLogger.Error().Err(err).Msg("Failed to save panic log to PostgreSQL")
|
||||
}
|
||||
|
||||
// Additional context (optional)
|
||||
if len(fields) > 0 {
|
||||
l.consoleLogger.Warn().Msg("Additional panic context captured")
|
||||
}
|
||||
|
||||
panic(message)
|
||||
}
|
||||
|
||||
func (l *CustomLogger) Debug(message, service string, fields map[string]interface{}) {
|
||||
l.consoleLogger.Debug().Fields(fields).Msg(message)
|
||||
l.logToPostgres("debug", message, service, fields)
|
||||
}
|
||||
Reference in New Issue
Block a user