|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + database2 "github.com/Darklabel91/API_Names/database" |
| 7 | + "github.com/Darklabel91/API_Names/models" |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "gorm.io/gorm" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "sync" |
| 13 | +) |
| 14 | + |
| 15 | +//user, err := c.Cookie("user") |
| 16 | +//if err != nil { |
| 17 | +// c.AbortWithStatus(http.StatusUnauthorized) |
| 18 | +// return |
| 19 | +//} |
| 20 | + |
| 21 | +const logBufferSize = 1000 |
| 22 | + |
| 23 | +type databaseWriter struct { |
| 24 | + buffer bytes.Buffer |
| 25 | + db *gorm.DB |
| 26 | + mutex sync.Mutex |
| 27 | + logCount int |
| 28 | +} |
| 29 | + |
| 30 | +func (w *databaseWriter) Write(p []byte) (n int, err error) { |
| 31 | + w.mutex.Lock() |
| 32 | + defer w.mutex.Unlock() |
| 33 | + |
| 34 | + n, err = w.buffer.Write(p) |
| 35 | + if err != nil { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + // Count the number of logs written to the buffer |
| 40 | + w.logCount += bytes.Count(p, []byte{'\n'}) |
| 41 | + |
| 42 | + // If the buffer is full, flush it to the database |
| 43 | + if w.logCount >= logBufferSize { |
| 44 | + err = w.flush() |
| 45 | + if err != nil { |
| 46 | + return |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return |
| 51 | +} |
| 52 | + |
| 53 | +func (w *databaseWriter) flush() error { |
| 54 | + // Split the buffer into individual log messages |
| 55 | + logs := bytes.Split(w.buffer.Bytes(), []byte{'\n'}) |
| 56 | + for _, log := range logs { |
| 57 | + // Skip empty log messages |
| 58 | + if len(log) == 0 { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + // Insert the log into the database |
| 63 | + err := w.db.Create(&models.Log{Message: string(log)}).Error |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Reset the buffer and log count |
| 70 | + w.buffer.Reset() |
| 71 | + w.logCount = 0 |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func LocalLog() gin.HandlerFunc { |
| 77 | + // Create the file writer and database writer |
| 78 | + file, err := os.Create("log/gin.txt") |
| 79 | + if err != nil { |
| 80 | + fmt.Println("Log not created") |
| 81 | + return nil |
| 82 | + } |
| 83 | + database := &databaseWriter{db: database2.InitDb()} |
| 84 | + |
| 85 | + // Use a multi-writer to write to both the file and database writers |
| 86 | + writer := io.MultiWriter(file, database) |
| 87 | + |
| 88 | + // Create the gin logger with the custom writer |
| 89 | + logger := gin.LoggerWithWriter(writer) |
| 90 | + |
| 91 | + return func(c *gin.Context) { |
| 92 | + // Call the logger middleware |
| 93 | + logger(c) |
| 94 | + |
| 95 | + // Flush the logs to the database if necessary |
| 96 | + if database.logCount >= logBufferSize { |
| 97 | + err = database.flush() |
| 98 | + if err != nil { |
| 99 | + fmt.Println(err) |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments