fix(be):set log format
This commit is contained in:
1
go.mod
1
go.mod
@ -19,6 +19,7 @@ require (
|
|||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
github.com/stretchr/testify v1.6.1
|
github.com/stretchr/testify v1.6.1
|
||||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
|
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||||
gorm.io/driver/mysql v1.0.3
|
gorm.io/driver/mysql v1.0.3
|
||||||
gorm.io/driver/sqlite v1.1.4
|
gorm.io/driver/sqlite v1.1.4
|
||||||
gorm.io/gorm v1.20.7
|
gorm.io/gorm v1.20.7
|
||||||
|
2
go.sum
2
go.sum
@ -353,6 +353,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
|
|||||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
|
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
|
||||||
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
@ -5,15 +5,33 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"next-terminal/pkg/config"
|
"next-terminal/pkg/config"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Formatter struct{}
|
||||||
|
|
||||||
|
func (s *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||||
|
timestamp := time.Now().Local().Format("2006-01-02 15:04:05")
|
||||||
|
var file string
|
||||||
|
var l int
|
||||||
|
if entry.HasCaller() {
|
||||||
|
file = filepath.Base(entry.Caller.Function)
|
||||||
|
l = entry.Caller.Line
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf("%s %s [%s:%d]%s\n", timestamp, strings.ToUpper(entry.Level.String()), file, l, entry.Message)
|
||||||
|
return []byte(msg), nil
|
||||||
|
}
|
||||||
|
|
||||||
var stdOut = NewLogger()
|
var stdOut = NewLogger()
|
||||||
|
|
||||||
// Trace logs a message at level Trace on the standard logger.
|
// Trace logs a message at level Trace on the standard logger.
|
||||||
@ -179,7 +197,6 @@ func NewLogger() Logrus {
|
|||||||
if err := os.MkdirAll(logFilePath, 0755); err != nil {
|
if err := os.MkdirAll(logFilePath, 0755); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
// TODO 滚动日志
|
|
||||||
logFileName := "next-terminal.log"
|
logFileName := "next-terminal.log"
|
||||||
//日志文件
|
//日志文件
|
||||||
fileName := path.Join(logFilePath, logFileName)
|
fileName := path.Join(logFilePath, logFileName)
|
||||||
@ -188,18 +205,18 @@ func NewLogger() Logrus {
|
|||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//写入文件
|
|
||||||
src, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("err", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//实例化
|
//实例化
|
||||||
logger := logrus.New()
|
logger := logrus.New()
|
||||||
|
|
||||||
//设置输出
|
//设置输出
|
||||||
logger.SetOutput(io.MultiWriter(os.Stdout, src))
|
logger.SetOutput(io.MultiWriter(&lumberjack.Logger{
|
||||||
|
Filename: fileName,
|
||||||
|
MaxSize: 100, // megabytes
|
||||||
|
MaxBackups: 3,
|
||||||
|
MaxAge: 7, //days
|
||||||
|
Compress: true, // disabled by default
|
||||||
|
}, os.Stdout))
|
||||||
|
logger.SetReportCaller(true)
|
||||||
//设置日志级别
|
//设置日志级别
|
||||||
if config.GlobalCfg.Debug {
|
if config.GlobalCfg.Debug {
|
||||||
logger.SetLevel(logrus.DebugLevel)
|
logger.SetLevel(logrus.DebugLevel)
|
||||||
@ -207,9 +224,7 @@ func NewLogger() Logrus {
|
|||||||
logger.SetLevel(logrus.InfoLevel)
|
logger.SetLevel(logrus.InfoLevel)
|
||||||
}
|
}
|
||||||
//设置日志格式
|
//设置日志格式
|
||||||
logger.SetFormatter(&logrus.TextFormatter{
|
logger.SetFormatter(new(Formatter))
|
||||||
TimestampFormat: "2006-01-02 15:04:05",
|
|
||||||
})
|
|
||||||
return Logrus{Logger: logger}
|
return Logrus{Logger: logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,25 +238,18 @@ func logrusMiddlewareHandler(c echo.Context, next echo.HandlerFunc) error {
|
|||||||
}
|
}
|
||||||
stop := time.Now()
|
stop := time.Now()
|
||||||
|
|
||||||
p := req.URL.Path
|
l.Debugf("%s %s %s %s %s %3d %s %13v %s %s",
|
||||||
|
c.RealIP(),
|
||||||
bytesIn := req.Header.Get(echo.HeaderContentLength)
|
req.Host,
|
||||||
|
req.Method,
|
||||||
l.WithFields(map[string]interface{}{
|
req.RequestURI,
|
||||||
"time_rfc3339": time.Now().Format(time.RFC3339),
|
req.URL.Path,
|
||||||
"remote_ip": c.RealIP(),
|
res.Status,
|
||||||
"host": req.Host,
|
strconv.FormatInt(res.Size, 10),
|
||||||
"uri": req.RequestURI,
|
stop.Sub(start).String(),
|
||||||
"method": req.Method,
|
req.Referer(),
|
||||||
"path": p,
|
req.UserAgent(),
|
||||||
"referer": req.Referer(),
|
)
|
||||||
"user_agent": req.UserAgent(),
|
|
||||||
"status": res.Status,
|
|
||||||
"latency": strconv.FormatInt(stop.Sub(start).Nanoseconds()/1000, 10),
|
|
||||||
"latency_human": stop.Sub(start).String(),
|
|
||||||
"bytes_in": bytesIn,
|
|
||||||
"bytes_out": strconv.FormatInt(res.Size, 10),
|
|
||||||
}).Debug("Handled request")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user