<commit_before>package middleware

import (
	"os"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/gin-gonic/gin"
)

func Logging() gin.HandlerFunc {
	return func(c *gin.Context) {
		logger := logrus.StandardLogger()
		start := time.Now().UTC()
		path := c.Request.URL.Path
		c.Next()
		end := time.Now().UTC()
		latency := end.Sub(start)

		formatter := &logrus.TextFormatter{
			FullTimestamp:   true,
			TimestampFormat: "2006-01-02-15:04:05",
		}
		logrus.SetFormatter(formatter)
		//logrus.SetFormatter(&logrus.JSONFormatter{})
		file, err := os.OpenFile("./logrus.log", os.O_APPEND|os.O_WRONLY, 0666)
		if err == nil {
			logger.Out = file
		} else {
			logger.Info("Failed to log to file, using default stderr")
		}

		logger.WithFields(logrus.Fields{
			"status":     c.Writer.Status(),
			"method":     c.Request.Method,
			"path":       path,
			"ip":         c.ClientIP(),
			"duration":   latency,
			"user_agent": c.Request.UserAgent(),
		}).Info()
	}<commit_msg>New format for route logging<commit_after>package middleware

import (
	"fmt"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/TeaMeow/KitSvc/module/logger"
	"github.com/gin-gonic/gin"
	"github.com/willf/pad"
)

func Logging() gin.HandlerFunc {
	return func(c *gin.Context) {

		start := time.Now().UTC()
		path := c.Request.URL.Path
		c.Next()

		if path == "/metrics" || path == "/sd/health" || path == "/sd/ram" || path == "/sd/cpu" || path == "/sd/disk" {
			return
		}

		end := time.Now().UTC()
		latency := end.Sub(start)

		status := c.Writer.Status()
		method := c.Request.Method
		ip := c.ClientIP()
		userAgent := c.Request.UserAgent()

		logger.InfoFields(fmt.Sprintf("%d | %s | %s | %s %s",
			status,
			pad.Right(latency.String(), 13, " "),
			pad.Right(ip, 12, " "),
			pad.Right(method, 5, " "),
			pad.Right(path, 15, " "),
		), logrus.Fields{
			//"status":     status,
			//"method":     method,
			//"path":       path,
			//"ip":         ip,
			//"duration":   latency,
			"user_agent": userAgent,
		})
	}
{'fraction_non_alphanumeric': 0.1946241911398706, 'fraction_numerical': 0.012444001991040319, 'mean_word_length': 2.988095238095238, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4110761', 'n_tokens_mistral': 868, 'n_tokens_neox': 761, 'n_words': 140}