From 453a8d769d61505819a34ad2ef81b7ad5d2eef9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 27 Feb 2021 16:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E6=97=A5=E5=BF=97=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 + go.sum | 82 ++++++++ logger/README.md | 2 + logger/config.go | 395 +++++++++++++++++++++++++++++++++++++ logger/debug.go | 26 +++ logger/error.go | 64 ++++++ logger/logger.go | 84 ++++++++ logger/logger_test.go | 72 +++++++ logger/wrapper/http_gin.go | 151 ++++++++++++++ 9 files changed, 880 insertions(+) create mode 100644 logger/README.md create mode 100644 logger/config.go create mode 100644 logger/debug.go create mode 100644 logger/error.go create mode 100644 logger/logger.go create mode 100644 logger/logger_test.go create mode 100644 logger/wrapper/http_gin.go diff --git a/go.mod b/go.mod index f55ae00..2d2ab4d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,10 @@ module github.com/go-developer/gopkg go 1.15 require ( + github.com/gin-gonic/gin v1.6.3 + github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible + github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 + go.uber.org/zap v1.16.0 ) diff --git a/go.sum b/go.sum index 752863c..2457dec 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,86 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= +github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= +github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4= +github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= +github.com/lestrrat-go/strftime v1.0.4 h1:T1Rb9EPkAhgxKqbcMIPguPq8glqXTA1koF8n9BHElA8= +github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= +go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/logger/README.md b/logger/README.md new file mode 100644 index 0000000..3094be3 --- /dev/null +++ b/logger/README.md @@ -0,0 +1,2 @@ +# go-logger +基于zap log的包装,方便引入使用 diff --git a/logger/config.go b/logger/config.go new file mode 100644 index 0000000..899fba9 --- /dev/null +++ b/logger/config.go @@ -0,0 +1,395 @@ +// Package logger... +// +// Description : config 日志配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-02 3:07 下午 +package logger + +import ( + "fmt" + "os" + "strings" + "time" + + "go.uber.org/zap/zapcore" +) + +// TimeIntervalType 日志时间间隔类型 +type TimeIntervalType uint + +const ( + // TimeIntervalTypeMinute 按分钟切割 + TimeIntervalTypeMinute = TimeIntervalType(0) + // TimeIntervalTypeHour 按小时切割 + TimeIntervalTypeHour = TimeIntervalType(1) + // TimeIntervalTypeDay 按天切割 + TimeIntervalTypeDay = TimeIntervalType(2) + // TimeIntervalTypeMonth 按月切割 + TimeIntervalTypeMonth = TimeIntervalType(3) + // TimeIntervalTypeYear 按年切割 + TimeIntervalTypeYear = TimeIntervalType(4) +) + +const ( + // DefaultDivisionChar 默认的时间格式分隔符 + DefaultDivisionChar = "-" +) + +// RotateLogConfig 日志切割的配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:08 下午 2021/1/2 +type RotateLogConfig struct { + TimeIntervalType TimeIntervalType // 日志切割的时间间隔类型 0 - 小时 1 - 天 2 - 月 3 - 年 + TimeInterval time.Duration // 日志切割的时间间隔 + LogPath string // 存储日志的路径 + LogFileName string // 日志文件名 + DivisionChar string // 日志文件拼时间分隔符 + FullLogFormat string // 完整的日志格式 + MaxAge time.Duration // 日志最长保存时间 +} + +// SetRotateLogConfigOption 设置日志切割的选项 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:13 下午 2021/1/2 +type SetRotateLogConfigFunc func(rlc *RotateLogConfig) + +// WithTimeIntervalType 设置日志切割时间间隔 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:34 下午 2021/1/2 +func WithTimeIntervalType(timeIntervalType TimeIntervalType) SetRotateLogConfigFunc { + return func(rlc *RotateLogConfig) { + rlc.TimeIntervalType = timeIntervalType + } +} + +// WithDivisionChar 设置分隔符 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:49 下午 2021/1/2 +func WithDivisionChar(divisionChar string) SetRotateLogConfigFunc { + return func(rlc *RotateLogConfig) { + rlc.DivisionChar = divisionChar + } +} + +// WithMaxAge 设置日志保存时间 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:03 下午 2021/1/2 +func WithMaxAge(maxAge time.Duration) SetRotateLogConfigFunc { + return func(rlc *RotateLogConfig) { + rlc.MaxAge = maxAge + } +} + +// NewRotateLogConfig 生成日志切割的配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:53 下午 2021/1/2 +func NewRotateLogConfig(logPath string, logFile string, option ...SetRotateLogConfigFunc) (*RotateLogConfig, error) { + if len(logPath) == 0 || len(logFile) == 0 { + return nil, LogPathEmptyError() + } + c := &RotateLogConfig{ + TimeIntervalType: 0, + LogPath: logPath, + LogFileName: logFile, + DivisionChar: "", + } + + for _, o := range option { + o(c) + } + + if err := formatConfig(c); nil != err { + return nil, err + } + + return c, nil +} + +// formatConfig 格式化配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:23 下午 2021/1/2 +func formatConfig(c *RotateLogConfig) error { + + if len(c.DivisionChar) == 0 { + c.DivisionChar = DefaultDivisionChar + } + // 格式化路径 + logPathByte := []byte(c.LogPath) + if string(logPathByte[len(logPathByte)-1]) != "/" { + c.LogPath = c.LogPath + "/" + } + // 检测路径是否存在,不存在自动创建 + if _, err := os.Stat(c.LogPath); nil != err { + if !os.IsNotExist(err) { + // 异常不是路径不存在,抛异常 + return DealLogPathError(err, c.LogPath) + } + if err := os.Mkdir(c.LogPath, os.ModePerm); nil != err { + return DealLogPathError(err, "创建日志目录失败") + } + } + + // 生成格式化日志全路径 + switch c.TimeIntervalType { + case TimeIntervalTypeMinute: + c.TimeInterval = time.Minute + c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + "%M" + c.DivisionChar + c.LogFileName + case TimeIntervalTypeHour: + c.TimeInterval = time.Hour + c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + "%H" + c.DivisionChar + c.LogFileName + case TimeIntervalTypeDay: + c.TimeInterval = time.Hour * 24 + c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + "%d" + c.DivisionChar + c.LogFileName + case TimeIntervalTypeMonth: + c.TimeInterval = time.Hour * 24 * 30 + c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + "%m" + c.DivisionChar + c.LogFileName + case TimeIntervalTypeYear: + c.TimeInterval = time.Hour * 24 * 365 + c.FullLogFormat = c.LogPath + "%Y" + c.DivisionChar + c.LogFileName + default: + return LogSplitTypeError(c.TimeIntervalType) + } + + return nil +} + +// ============== 以下为zap相关配置 + +const ( + // defaultMessageKey 默认的message key + defaultMessageKey = "message" + // defaultLevelKey 默认的level + defaultLevelKey = "level" + // defaultTimeKey 默认时间key + defaultTimeKey = "time" + // defaultCallerKey 默认的文件key + defaultCallerKey = "file" + // defaultUserShortCaller 是否使用短的文件调用格式 + defaultUseShortCaller = true + // defaultUseJsonFormat 日志默认使用json格式 + defaultUseJsonFormat = true +) + +// defaultTimeEncoder 默认的时间处理 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:53 下午 2021/1/2 +func defaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + sec := t.UnixNano() / 1e9 + ms := t.UnixNano() / 1e6 % 1e3 + ns := t.UnixNano() % 1e6 + enc.AppendString(time.Unix(sec, ns).Format("2006-01-02 15:04:05") + "." + fmt.Sprintf("%v", ms) + "+" + fmt.Sprintf("%v", ns)) +} + +// SecondTimeEncoder 秒级时间戳格式化 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:34 下午 2021/1/3 +func SecondTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02 15:04:05")) +} + +// MsTimeEncoder 毫秒时间格式化方法 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:35 下午 2021/1/3 +func MsTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + sec := t.UnixNano() / 1e9 + ms := t.UnixNano() / 1e6 % 1e3 + enc.AppendString(time.Unix(sec, 0).Format("2006-01-02 15:04:05") + "." + fmt.Sprintf("%v", ms)) +} + +// defaultEncodeDuration 默认的原始时间处理 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:56 下午 2021/1/2 +func defaultEncodeDuration(d time.Duration, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendInt64(int64(d) / 1000000) +} + +// OptionLogger 日志配置的选项 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:41 下午 2021/1/2 +type OptionLogger struct { + UseJsonFormat bool // 日志使用json格式 + MessageKey string // message 字段 + LevelKey string // level 字段 + TimeKey string // 时间字段 + CallerKey string // 记录日志的文件的代码行数 + UseShortCaller bool // 使用短的调用文件格式 + TimeEncoder zapcore.TimeEncoder // 格式化时间的函数 + EncodeDuration zapcore.DurationEncoder // 原始时间信息 +} + +// 设置日志配置 +type SetLoggerOptionFunc func(o *OptionLogger) + +// WithUseJsonFormat 日志是否使用json格式数据 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:30 上午 2021/1/3 +func WithUseJsonFormat(isJsonFormat bool) SetLoggerOptionFunc { + return func(o *OptionLogger) { + o.UseJsonFormat = isJsonFormat + } +} + +// WithMessageKey 使用message key +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:32 上午 2021/1/3 +func WithMessageKey(messageKey string) SetLoggerOptionFunc { + return func(o *OptionLogger) { + messageKey = strings.Trim(messageKey, " ") + if len(messageKey) == 0 { + return + } + o.MessageKey = messageKey + } +} + +// WithLevelKey 设置level key +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:33 上午 2021/1/3 +func WithLevelKey(levelKey string) SetLoggerOptionFunc { + return func(o *OptionLogger) { + levelKey = strings.Trim(levelKey, " ") + if len(levelKey) == 0 { + return + } + o.LevelKey = levelKey + } +} + +// WithTimeKey 设置time key ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:34 上午 2021/1/3 +func WithTimeKey(timeKey string) SetLoggerOptionFunc { + return func(o *OptionLogger) { + timeKey = strings.Trim(timeKey, " ") + if len(timeKey) == 0 { + return + } + o.TimeKey = timeKey + } +} + +// WithCallerKey 设置caller key +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:37 上午 2021/1/3 +func WithCallerKey(callerKey string) SetLoggerOptionFunc { + return func(o *OptionLogger) { + callerKey = strings.Trim(callerKey, " ") + if len(callerKey) == 0 { + return + } + o.CallerKey = callerKey + } +} + +// WithShortCaller 是否使用短caller格式 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:39 上午 2021/1/3 +func WithShortCaller(useShortCaller bool) SetLoggerOptionFunc { + return func(o *OptionLogger) { + o.UseShortCaller = useShortCaller + } +} + +// WithTimeEncoder 设置格式化时间方法 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:41 上午 2021/1/3 +func WithTimeEncoder(encoder zapcore.TimeEncoder) SetLoggerOptionFunc { + return func(o *OptionLogger) { + if nil == encoder { + return + } + o.TimeEncoder = encoder + } +} + +// WithEncodeDuration 原始时间 +// +// Author : go_developer@163.com<张德满> +// +// Date : 12:42 上午 2021/1/3 +func WithEncodeDuration(encoder zapcore.DurationEncoder) SetLoggerOptionFunc { + return func(o *OptionLogger) { + if nil == encoder { + return + } + o.EncodeDuration = encoder + } +} + +// GetEncoder 获取空中台输出的encoder +// +// Author : go_developer@163.com<张德满> +// +// Date : 6:24 下午 2021/1/2 +func GetEncoder(option ...SetLoggerOptionFunc) zapcore.Encoder { + ol := &OptionLogger{ + UseJsonFormat: defaultUseJsonFormat, + MessageKey: defaultMessageKey, + LevelKey: defaultLevelKey, + TimeKey: defaultTimeKey, + TimeEncoder: defaultTimeEncoder, + CallerKey: defaultCallerKey, + EncodeDuration: defaultEncodeDuration, + UseShortCaller: defaultUseShortCaller, + } + for _, o := range option { + o(ol) + } + ec := zapcore.EncoderConfig{ + MessageKey: ol.MessageKey, + LevelKey: ol.LevelKey, + EncodeLevel: zapcore.CapitalLevelEncoder, + TimeKey: ol.TimeKey, + EncodeTime: ol.TimeEncoder, + CallerKey: ol.CallerKey, + EncodeCaller: zapcore.ShortCallerEncoder, + EncodeDuration: ol.EncodeDuration, + } + if !ol.UseShortCaller { + ec.EncodeCaller = zapcore.FullCallerEncoder + } + if !ol.UseJsonFormat { + return zapcore.NewConsoleEncoder(ec) + } + return zapcore.NewJSONEncoder(ec) +} diff --git a/logger/debug.go b/logger/debug.go new file mode 100644 index 0000000..2be6a16 --- /dev/null +++ b/logger/debug.go @@ -0,0 +1,26 @@ +// Package logger... +// +// Description : logger... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-03 12:58 上午 +package logger + +import ( + "bytes" + "encoding/json" +) + +// FormatJson 格式化输出json +// +// Author : go_developer@163.com<张德满> +// +// Date : 1:06 上午 2021/1/3 +func FormatJson(src interface{}) string { + byteData, _ := json.Marshal(src) + + var str bytes.Buffer + _ = json.Indent(&str, byteData, "", " ") + return str.String() +} diff --git a/logger/error.go b/logger/error.go new file mode 100644 index 0000000..aa80366 --- /dev/null +++ b/logger/error.go @@ -0,0 +1,64 @@ +// Package logger... +// +// Description : error 定义日志处理过程中的各种错误 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-02 2:44 下午 +package logger + +import "github.com/pkg/errors" + +// CreateLogFileError 创建日志文件失败 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2:55 下午 2021/1/2 +func CreateLogFileError(err error, logFilePath string) error { + return errors.Wrapf(err, "创建日志文件失败,日志文件路径 : %s", logFilePath) +} + +// LogPathEmptyError 日志路径为空 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:03 下午 2021/1/2 +func LogPathEmptyError() error { + return errors.Wrap(errors.New("日志存储路径或者日志文件名为空"), "日志存储路径或者日志文件名为空") +} + +// CustomTimeIntervalError 自定义日志切割时间间隔错误 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:11 下午 2021/1/2 +func CustomTimeIntervalError() error { + return errors.Wrap(errors.New("自定义时间间隔错误,必须是大于0的值"), "自定义时间间隔错误,必须是大于0的值") +} + +// DealLogPathError 日志路径处理异常 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:31 下午 2021/1/2 +func DealLogPathError(err error, logPath string) error { + return errors.Wrapf(err, "日志路径检测处理异常, 日志路径 : %s", logPath) +} + +// LogSplitTypeError 日志切割类型错误 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:50 下午 2021/1/2 +func LogSplitTypeError(splitType TimeIntervalType) error { + return errors.Wrapf(errors.New("日志切割时间类型错误"), "日志切割时间类型错误, 传入类型 : %v", splitType) +} + +// CreateIOWriteError 创建日志实例失败 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:20 下午 2021/1/2 +func CreateIOWriteError(err error) error { + return errors.Wrapf(err, "创建日志实例失败") +} diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..300382a --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,84 @@ +// Package logger... +// +// Description : logger 日志文件 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-02 5:04 下午 +package logger + +import ( + "io" + "os" + + "go.uber.org/zap" + + "go.uber.org/zap/zapcore" + + rotatelogs "github.com/lestrrat-go/file-rotatelogs" +) + +// NewLogger 获取日志实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:05 下午 2021/1/2 +func NewLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *RotateLogConfig) (*zap.Logger, error) { + loggerLevelDeal := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { + return lvl >= loggerLevel + }) + l := &Logger{ + splitConfig: splitConfig, + encoder: encoder, + } + var ( + err error + loggerWriter io.Writer + ) + // 获取 日志实现 + if loggerWriter, err = l.getWriter(); nil != err { + return nil, err + } + + fileHandlerList := []zapcore.Core{ + zapcore.NewCore(encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal), + } + + // 设置控制台输出 + if consoleOutput { + fileHandlerList = append(fileHandlerList, zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), loggerLevelDeal)) + } + + // 最后创建具体的Logger + core := zapcore.NewTee(fileHandlerList...) + + log := zap.New(core, zap.AddCaller()) // 需要传入 zap.AddCaller() 才会显示打日志点的文件名和行数 + return log, nil +} + +type Logger struct { + splitConfig *RotateLogConfig + encoder zapcore.Encoder +} + +// getWriter 获取日志实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:08 下午 2021/1/2 +func (l *Logger) getWriter() (io.Writer, error) { + option := make([]rotatelogs.Option, 0) + option = append(option, rotatelogs.WithRotationTime(l.splitConfig.TimeInterval)) + if l.splitConfig.MaxAge > 0 { + option = append(option, rotatelogs.WithMaxAge(l.splitConfig.MaxAge)) + } + var ( + hook *rotatelogs.RotateLogs + err error + ) + if hook, err = rotatelogs.New(l.splitConfig.FullLogFormat, option...); nil != err { + return nil, CreateIOWriteError(err) + } + + return hook, nil +} diff --git a/logger/logger_test.go b/logger/logger_test.go new file mode 100644 index 0000000..4fff456 --- /dev/null +++ b/logger/logger_test.go @@ -0,0 +1,72 @@ +// Package logger... +// +// Description : logger_test 单元测试 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-02 4:59 下午 +package logger + +import ( + "fmt" + "testing" + "time" + + "go.uber.org/zap" + + "go.uber.org/zap/zapcore" +) + +// Test_Logger ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 5:50 下午 2021/1/2 +func Test_Logger(t *testing.T) { + encoder := GetEncoder(WithTimeEncoder(MsTimeEncoder)) + c, err := NewRotateLogConfig("./logs", "test.log", WithTimeIntervalType(TimeIntervalTypeMinute), WithMaxAge(120*time.Second)) + if nil != err { + panic(err) + } + l, err := NewLogger(zapcore.InfoLevel, true, encoder, c) + if nil != err { + panic(err) + } + + for { + l.Info("这是一条测试日志", zap.Any("lala", "不限制类型")) + l.Debug("这是一条测试日志", zap.Any("lala", "不限制类型")) + time.Sleep(1 * time.Second) + } +} + +// Test_FormatJson 测试json格式化输出 +// +// Author : go_developer@163.com<张德满> +// +// Date : 1:08 上午 2021/1/3 +func Test_FormatJson(t *testing.T) { + data := map[string]interface{}{ + "name": "zhangdeman", + "age": 18, + } + str := FormatJson(data) + fmt.Println(str) + + boolData := true + str = FormatJson(boolData) + fmt.Println(str) +} + +// Test_Time 测试格式化纳秒时间戳 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:32 下午 2021/1/3 +func Test_Time(t *testing.T) { + ti := time.Now() + sec := ti.UnixNano() / 1e9 + ms := ti.UnixNano() / 1e6 % 1e3 + ns := ti.UnixNano() % 1e6 + fmt.Println(time.Unix(sec, ns).Format("2006-01-02 15:04:05") + "." + fmt.Sprintf("%v", ms) + "+" + fmt.Sprintf("%v", ns)) +} diff --git a/logger/wrapper/http_gin.go b/logger/wrapper/http_gin.go new file mode 100644 index 0000000..21ae904 --- /dev/null +++ b/logger/wrapper/http_gin.go @@ -0,0 +1,151 @@ +// Package wrapper... +// +// Description : http_gin 使用gin框架时的,记录日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-01-03 3:43 下午 +package wrapper + +import ( + "encoding/json" + + "github.com/gin-gonic/gin" + "github.com/go-developer/gopkg/logger" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +// NewGinWrapperLogger 使用gin框架记录日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:45 下午 2021/1/3 +func NewGinWrapperLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger.RotateLogConfig, extractFieldList []string) (*GinWrapper, error) { + var ( + err error + l *zap.Logger + ) + if l, err = logger.NewLogger(loggerLevel, consoleOutput, encoder, splitConfig); nil != err { + return nil, err + } + + return &GinWrapper{ + loggerInstance: l, + extractFieldList: extractFieldList, + }, nil +} + +// GinWrapper 包装gin实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 3:59 下午 2021/1/3 +type GinWrapper struct { + loggerInstance *zap.Logger // zap 的日志实例 + extractFieldList []string // 从gin中抽取的字段 + ginCtx *gin.Context // gin 实例 +} + +// GetLogger 为每一次请求生成不同的日志实例,包含独立的gin上下文 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:02 下午 2021/1/3 +func (gw *GinWrapper) GetLogger(ginCtx *gin.Context) *GinWrapper { + return &GinWrapper{ + loggerInstance: gw.loggerInstance, + extractFieldList: gw.extractFieldList, + ginCtx: ginCtx, + } +} + +// formatFieldList 格式化日志field列表 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:13 下午 2021/1/3 +func (gw *GinWrapper) formatFieldList(inputFieldList []zap.Field) []zap.Field { + if nil == inputFieldList { + inputFieldList = make([]zap.Field, 0) + } + if nil != gw.ginCtx { + // 自动扩充抽取字段,字段不存在的话,忽略掉 + for _, extractField := range gw.extractFieldList { + if v, exist := gw.ginCtx.Get(extractField); exist { + byteData, _ := json.Marshal(v) + inputFieldList = append(inputFieldList, zap.String(extractField, string(byteData))) + } + } + } + return inputFieldList +} + +// Debug 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:14 下午 2021/1/3 +func (gw *GinWrapper) Debug(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.Debug(msg, fieldList...) +} + +// Info 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:28 下午 2021/1/3 +func (gw *GinWrapper) Info(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.Info(msg, fieldList...) +} + +// Warn 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:29 下午 2021/1/3 +func (gw *GinWrapper) Warn(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.Warn(msg, fieldList...) +} + +// Error 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:29 下午 2021/1/3 +func (gw *GinWrapper) Error(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.Error(msg, fieldList...) +} + +// Panic 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:29 下午 2021/1/3 +func (gw *GinWrapper) Panic(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.Panic(msg, fieldList...) +} + +// DPanic 日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 4:30 下午 2021/1/3 +func (gw *GinWrapper) DPanic(msg string, field ...zap.Field) { + fieldList := gw.formatFieldList(field) + gw.loggerInstance.DPanic(msg, fieldList...) +} + +// GetZapLoggerInstance 获取zap日志实例 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021/01/03 22:56:47 +func (gw *GinWrapper) GetZapLoggerInstance() *zap.Logger { + return gw.loggerInstance +}