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
func ExtName(path string) string {
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().
Timestamp().
Stack().
CallerWithSkipFrameCount(2).
Logger()
access_log = &l

View File

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

View File

@ -10,6 +10,7 @@ package logger
import (
"path/filepath"
"strconv"
"strings"
"time"
"github.com/rs/zerolog"
@ -26,7 +27,17 @@ func init() {
zerolog.TimeFieldFormat = time.RFC3339
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().
Timestamp().
Stack().
CallerWithSkipFrameCount(2).
Logger()
log = &l
})

View File

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

View File

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

View File

@ -14,8 +14,9 @@ import (
"os"
"strings"
"github.com/gin-gonic/gin"
"git.hexq.cn/tiglog/golib/helper"
"git.hexq.cn/tiglog/golib/logger"
"github.com/gin-gonic/gin"
)
func GinRecover() gin.HandlerFunc {
@ -37,14 +38,16 @@ func GinRecover() gin.HandlerFunc {
httpRequest, _ := httputil.DumpRequest(c.Request, false)
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.
c.Error(err.(error)) // nolint: errcheck
c.Abort()
return
}
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)
}
}()