misc: 优化日志

This commit is contained in:
tiglog 2023-07-15 17:52:50 +08:00
parent 4c3f09f313
commit 7853a35d99
8 changed files with 32 additions and 4 deletions

View File

@ -174,4 +174,13 @@ func Ext(path string) string {
// api.json => json // api.json => json
func ExtName(path string) string { func ExtName(path string) string {
return strings.TrimLeft(Ext(path), ".") return strings.TrimLeft(Ext(path), ".")
}
func GetProjectHome() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return ""
}
return dir
} }

View File

@ -43,6 +43,7 @@ func Access() *zerolog.Logger {
With(). With().
Timestamp(). Timestamp().
Stack(). Stack().
CallerWithSkipFrameCount(2).
Logger() Logger()
access_log = &l access_log = &l

View File

@ -43,6 +43,7 @@ func Console() *zerolog.Logger {
With(). With().
Timestamp(). Timestamp().
Stack(). Stack().
CallerWithSkipFrameCount(2).
Logger() Logger()
console_log = &l console_log = &l

View File

@ -10,6 +10,7 @@ package logger
import ( import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -26,7 +27,17 @@ func init() {
zerolog.TimeFieldFormat = time.RFC3339 zerolog.TimeFieldFormat = time.RFC3339
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string { zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line) new_files := strings.Split(file, "/")
l := len(new_files)
pos := 0
var new_file string
if l-3 > 0 {
pos = l - 3
new_file = strings.Join(new_files[pos:], "/")
} else {
new_file = filepath.Base(file)
}
return new_file + ":" + strconv.Itoa(line)
} }
} }

View File

@ -43,6 +43,7 @@ func Get() *zerolog.Logger {
With(). With().
Timestamp(). Timestamp().
Stack(). Stack().
CallerWithSkipFrameCount(2).
Logger() Logger()
log = &l log = &l
}) })

View File

@ -43,6 +43,7 @@ func Recover() *zerolog.Logger {
With(). With().
Timestamp(). Timestamp().
Stack(). Stack().
CallerWithSkipFrameCount(2).
Logger() Logger()
recover_log = &l recover_log = &l

View File

@ -42,6 +42,7 @@ func Work() *zerolog.Logger {
With(). With().
Timestamp(). Timestamp().
Stack(). Stack().
CallerWithSkipFrameCount(2).
Logger() Logger()
work_log = &l work_log = &l

View File

@ -14,8 +14,9 @@ import (
"os" "os"
"strings" "strings"
"github.com/gin-gonic/gin" "git.hexq.cn/tiglog/golib/helper"
"git.hexq.cn/tiglog/golib/logger" "git.hexq.cn/tiglog/golib/logger"
"github.com/gin-gonic/gin"
) )
func GinRecover() gin.HandlerFunc { func GinRecover() gin.HandlerFunc {
@ -37,14 +38,16 @@ func GinRecover() gin.HandlerFunc {
httpRequest, _ := httputil.DumpRequest(c.Request, false) httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe { if brokenPipe {
log.Error().Err(err.(error)).Str("request", string(httpRequest)).Msg(c.Request.URL.Path) log.Error().Err(err.(error)).Str("request", string(httpRequest)).
Msg(c.Request.URL.Path)
// If the connection is dead, we can't write a status to it. // If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck c.Error(err.(error)) // nolint: errcheck
c.Abort() c.Abort()
return return
} }
var er = err.(error) var er = err.(error)
log.Error().Stack().Str("request", string(httpRequest)).Err(er).Msg("Recovery from panic") log.Error().Str("request", string(httpRequest)).
Err(helper.WrapErr(er, "wrap")).Msg("Recovery from panic")
c.AbortWithError(http.StatusInternalServerError, er) c.AbortWithError(http.StatusInternalServerError, er)
} }
}() }()