-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package glog | ||
|
||
import ( | ||
"grm/common" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
) | ||
|
||
var Logger *zap.Logger | ||
|
||
func init() { | ||
Logger = NewLogger("service.log") | ||
} | ||
|
||
func NewLogger(path string) *zap.Logger { | ||
|
||
rootPath, _ := common.RootPath() | ||
|
||
hook := lumberjack.Logger{ | ||
Filename: rootPath + "/log/" + path, // 日志文件路径 | ||
MaxSize: 128, // 每个日志文件保存的最大尺寸 单位:M | ||
MaxBackups: 30, // 日志文件最多保存多少个备份 | ||
MaxAge: 7, // 文件最多保存多少天 | ||
Compress: true, // 是否压缩 | ||
} | ||
|
||
encoderConfig := zapcore.EncoderConfig{ | ||
TimeKey: "time", | ||
LevelKey: "level", | ||
NameKey: "logger", | ||
CallerKey: "linenum", | ||
MessageKey: "msg", | ||
StacktraceKey: "stacktrace", | ||
LineEnding: zapcore.DefaultLineEnding, | ||
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器 | ||
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式 | ||
EncodeDuration: zapcore.SecondsDurationEncoder, // | ||
EncodeCaller: zapcore.FullCallerEncoder, // 全路径编码器 | ||
EncodeName: zapcore.FullNameEncoder, | ||
} | ||
|
||
// 设置日志级别 | ||
atomicLevel := zap.NewAtomicLevel() | ||
atomicLevel.SetLevel(zap.InfoLevel) | ||
|
||
core := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(encoderConfig), // 编码器配置 | ||
zapcore.NewMultiWriteSyncer(zapcore.AddSync(&hook)), // 打印到控制台和文件 | ||
atomicLevel, // 日志级别 | ||
) | ||
|
||
// 构造日志 | ||
return zap.New(core) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package middleware | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/httputil" | ||
"os" | ||
"runtime/debug" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func GinRecovery(logger *zap.Logger, stack bool) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
// Check for a broken connection, as it is not really a | ||
// condition that warrants a panic stack trace. | ||
var brokenPipe bool | ||
if ne, ok := err.(*net.OpError); ok { | ||
if se, ok := ne.Err.(*os.SyscallError); ok { | ||
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") { | ||
brokenPipe = true | ||
} | ||
} | ||
} | ||
|
||
httpRequest, _ := httputil.DumpRequest(c.Request, false) | ||
if brokenPipe { | ||
logger.Error(c.Request.URL.Path, | ||
zap.Any("error", err), | ||
zap.String("request", string(httpRequest)), | ||
) | ||
// If the connection is dead, we can't write a status to it. | ||
c.Error(err.(error)) // nolint: errcheck | ||
c.Abort() | ||
return | ||
} | ||
|
||
if stack { | ||
logger.Error("[Recovery from panic]", | ||
zap.Any("error", err), | ||
zap.String("request", string(httpRequest)), | ||
zap.String("stack", string(debug.Stack())), | ||
) | ||
} else { | ||
logger.Error("[Recovery from panic]", | ||
zap.Any("error", err), | ||
zap.String("request", string(httpRequest)), | ||
) | ||
} | ||
c.AbortWithStatus(http.StatusInternalServerError) | ||
} | ||
}() | ||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters