You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.0 KiB
103 lines
2.0 KiB
package tools
|
|
|
|
import (
|
|
"github.com/sirupsen/logrus"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
var logrusObj *logrus.Logger
|
|
|
|
func Logger() *logrus.Logger {
|
|
if logrusObj != nil {
|
|
src, _ := setOutputFile()
|
|
//设置输出
|
|
logrusObj.Out = src
|
|
return logrusObj
|
|
}
|
|
|
|
//实例化
|
|
logger := logrus.New()
|
|
src, _ := setOutputFile()
|
|
//设置输出
|
|
logger.Out = src
|
|
//设置日志级别
|
|
logger.SetLevel(logrus.DebugLevel)
|
|
//设置日志格式
|
|
logger.SetFormatter(&logrus.TextFormatter{
|
|
TimestampFormat: "2006-01-02 15:04:05",
|
|
})
|
|
logrusObj = logger
|
|
return logger
|
|
}
|
|
func setOutputFile() (*os.File, error) {
|
|
now := time.Now()
|
|
logFilePath := ""
|
|
if dir, err := os.Getwd(); err == nil {
|
|
logFilePath = dir + "/logs/"
|
|
}
|
|
_, err := os.Stat(logFilePath)
|
|
if os.IsNotExist(err) {
|
|
if err := os.MkdirAll(logFilePath, 0777); err != nil {
|
|
log.Println(err.Error())
|
|
return nil, err
|
|
}
|
|
}
|
|
logFileName := now.Format("2006-01-02") + ".log"
|
|
//日志文件
|
|
fileName := path.Join(logFilePath, logFileName)
|
|
if _, err := os.Stat(fileName); err != nil {
|
|
if _, err := os.Create(fileName); err != nil {
|
|
log.Println(err.Error())
|
|
return nil, err
|
|
}
|
|
}
|
|
//写入文件
|
|
src, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil, err
|
|
}
|
|
return src, nil
|
|
}
|
|
|
|
//func LoggerToFile() gin.HandlerFunc {
|
|
// logger := Logger()
|
|
// return func(c *gin.Context) {
|
|
// // 开始时间
|
|
// startTime := time.Now()
|
|
//
|
|
// // 处理请求
|
|
// c.Next()
|
|
//
|
|
// // 结束时间
|
|
// endTime := time.Now()
|
|
//
|
|
// // 执行时间
|
|
// latencyTime := endTime.Sub(startTime)
|
|
//
|
|
// // 请求方式
|
|
// reqMethod := c.Request.Method
|
|
//
|
|
// // 请求路由
|
|
// reqUri := c.Request.RequestURI
|
|
//
|
|
// // 状态码
|
|
// statusCode := c.Writer.Status()
|
|
//
|
|
// // 请求IP
|
|
// clientIP := c.ClientIP()
|
|
//
|
|
// //日志格式
|
|
// logger.Infof("| %3d | %13v | %15s | %s | %s |",
|
|
// statusCode,
|
|
// latencyTime,
|
|
// clientIP,
|
|
// reqMethod,
|
|
// reqUri,
|
|
// )
|
|
// }
|
|
//}
|