mirror of
https://github.com/Ernous/TorrServerJellyfin.git
synced 2025-12-19 13:36:09 +05:00
add web log
This commit is contained in:
@@ -1,24 +1,100 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Init(path string) {
|
||||
var logPath = ""
|
||||
var webLogPath = ""
|
||||
|
||||
var webLog *log.Logger
|
||||
|
||||
var logFile *os.File
|
||||
var webLogFile *os.File
|
||||
|
||||
func Init(path, webpath string) {
|
||||
webLogPath = webpath
|
||||
logPath = path
|
||||
|
||||
if webpath != "" {
|
||||
ff, err := os.OpenFile(webLogPath, os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
TLogln("Error create web log file:", err)
|
||||
} else {
|
||||
webLogFile = ff
|
||||
webLog = log.New(ff, " ", log.LstdFlags)
|
||||
}
|
||||
}
|
||||
|
||||
if path != "" {
|
||||
ff, err := os.Create(path)
|
||||
if fi, err := os.Lstat(path); err == nil {
|
||||
if fi.Size() >= 1*1024*1024*1024 {
|
||||
os.Remove(path)
|
||||
}
|
||||
}
|
||||
ff, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
TLogln("Error create log file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
logFile = ff
|
||||
os.Stdout = ff
|
||||
os.Stderr = ff
|
||||
log.SetOutput(ff)
|
||||
}
|
||||
}
|
||||
|
||||
func Close() {
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
}
|
||||
if webLogFile != nil {
|
||||
webLogFile.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TLogln(v ...interface{}) {
|
||||
log.Println(v...)
|
||||
}
|
||||
|
||||
func WebLogln(v ...interface{}) {
|
||||
if webLog != nil {
|
||||
webLog.Println(v...)
|
||||
}
|
||||
}
|
||||
|
||||
func WebLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if webLog == nil {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
body, _ := ioutil.ReadAll(c.Request.Body)
|
||||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
||||
c.Next()
|
||||
|
||||
statusCode := c.Writer.Status()
|
||||
clientIP := c.ClientIP()
|
||||
method := c.Request.Method
|
||||
path := c.Request.URL.Path
|
||||
raw := c.Request.URL.RawQuery
|
||||
if raw != "" {
|
||||
path = path + "?" + raw
|
||||
}
|
||||
|
||||
logStr := fmt.Sprintf("%3d | %12s | %-7s %#v %v",
|
||||
statusCode,
|
||||
clientIP,
|
||||
method,
|
||||
path,
|
||||
string(body),
|
||||
)
|
||||
WebLogln(logStr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user