servcvgen/logs.go

124 lines
3.2 KiB
Go

package main
import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
cfg "github.com/jesusperez/cfgsrv"
utils "github.com/jesusperez/datautils"
log "github.com/sirupsen/logrus"
)
func setReqLog(cfg *cfg.Config) {
todayLog := fmt.Sprintf("%s/%s.json",cfg.RequestOut,time.Now().Format("2006_01_02"))
if todayLog == PathReqLog {
return
} else {
PathReqLog = todayLog
}
err := utils.CheckDirPath(fmt.Sprintf("%s",cfg.RequestOut))
if err != nil {
log.Fatalf("setReqLog path %s: %v",cfg.RequestOut,err)
}
reqOut, err := os.OpenFile(PathReqLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("setReqLog: %v", err)
}
ReqLog = log.New()
ReqLog.SetOutput(reqOut)
ReqLog.SetFormatter(&log.JSONFormatter{
PrettyPrint: false,
})
}
func setLog(cfg *cfg.Config) {
environment := os.Getenv("ENVIRONMENT")
if environment == "production" {
log.SetFormatter(&log.JSONFormatter{})
file, err := os.OpenFile(cfg.LogOut, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Open LogOut: %v", err)
}
log.SetOutput(file)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
} else {
// The TextFormatter is default, you don't actually have to do this.
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
}
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
}
func getIP(r *http.Request) (string, error) {
//Get IP from the X-REAL-IP header
ip := r.Header.Get("X-REAL-IP")
netIP := net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
//Get IP from X-FORWARDED-FOR header
ips := r.Header.Get("X-FORWARDED-FOR")
splitIps := strings.Split(ips, ",")
for _, ip := range splitIps {
netIP := net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
}
//Get IP from RemoteAddr
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
netIP = net.ParseIP(ip)
if netIP != nil {
return ip, nil
}
return "", fmt.Errorf("No valid ip found")
}
func logRequest(c *gin.Context, rtenv *RouteEnv, run string, route string, info string, infoReq string, tkn string) {
accept_langs := strings.Split(c.Request.Header.Get("Accept-Language"),";")
langs := strings.Split(accept_langs[0], ",")
ip, _ := getIP(c.Request)
// header, _ := ioutil.ReadAll(c.Request.Header)
//println(string(c.header))
// println(c.Header)
// ipp := c.Request.RemoteAddr
// xforward := c.Request.Header.Get("X-Forwarded-For")
// fm.Println("IP : ", ipp)
// fmt.Println("X-Forwarded-For : ", xforward)
// fmt.Println("IP : ", ip)
// // if err != nil {
// dt := time.Now()
// fmt.Println("Current date and time is: ", dt.String())
// fmt.Println(dt.Format("01-02-2006 15:04:05"))
// fmt.Println(dt.Format(time.RFC3339Nano))
if len(info) > 0 {
setReqLog(rtenv.Cfg)
ReqLog.WithFields(log.Fields{
"run": run,
"route": route,
"lang": fmt.Sprintf("%+v",langs),
"agent": fmt.Sprintf("%+v",c.Request.UserAgent()),
"ip": ip,
"tkn": tkn,
}).Info(info)
}
if len(infoReq) > 0 {
log.WithFields(log.Fields{
"run": "route",
"route": "/",
"ip": ip,
"tkn": tkn,
}).Info(infoReq)
}
}