Skip to content

Commit 1d86a23

Browse files
authored
Merge pull request #11 from miacio/feature_v1.0.6
Feature v1.0.6
2 parents 42021ef + 3f17337 commit 1d86a23

File tree

7 files changed

+235
-17
lines changed

7 files changed

+235
-17
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,29 @@ add email tool
2828

2929
because of [email](https://github.com/jordan-wright/email) package
3030

31-
see email.email_test.go
31+
see email.email_test.go
32+
33+
## log
34+
add log package
35+
36+
because of uber.zap package
37+
38+
extremely simplified and fast build logs
39+
40+
``` go
41+
import "github.com/miacio/varietas/log"
42+
43+
func main() {
44+
logParam := log.LoggerParam{
45+
Path: "./log", // you log write folder path
46+
MaxSize: 256,
47+
MaxBackups: 10,
48+
MaxAge: 7,
49+
Compress: false,
50+
}
51+
52+
log := logParam.Default()
53+
54+
log.Infoln("init success")
55+
}
56+
```

dynamic/dynamic.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ type MethodMap map[string]reflect.Value
1212
// ClassesMethodMap
1313
type ClassesMethodMap map[string]MethodMap
1414

15-
// context
16-
type context struct {
15+
// Context
16+
type Context struct {
1717
classesMethodMap ClassesMethodMap
1818
classesLock sync.Mutex
1919
}
2020

2121
// New
22-
func New() *context {
23-
return &context{
22+
func New() *Context {
23+
return &Context{
2424
classesMethodMap: make(ClassesMethodMap),
2525
}
2626
}
2727

2828
// register
29-
func (c *context) register(name string, class any) (int, error) {
29+
func (c *Context) register(name string, class any) (int, error) {
3030
to := reflect.TypeOf(class)
31-
fmt.Println(to.Kind())
3231
if to.Kind() != reflect.Struct && to.Kind() != reflect.Pointer {
3332
return 0, fmt.Errorf("the class type of kind is not a struct")
3433
}
@@ -66,17 +65,17 @@ func (c *context) register(name string, class any) (int, error) {
6665
}
6766

6867
// RegisterByName
69-
func (c *context) RegisterByName(name string, class any) (int, error) {
68+
func (c *Context) RegisterByName(name string, class any) (int, error) {
7069
return c.register(name, class)
7170
}
7271

7372
// RegisterByAny
74-
func (c *context) RegisterByAny(class any) (int, error) {
73+
func (c *Context) RegisterByAny(class any) (int, error) {
7574
return c.register("", class)
7675
}
7776

7877
// call
79-
func (c *context) call(name, method string, params []reflect.Value) ([]reflect.Value, error) {
78+
func (c *Context) call(name, method string, params []reflect.Value) ([]reflect.Value, error) {
8079
methodMap, ok := c.classesMethodMap[name]
8180
if !ok {
8281
return nil, fmt.Errorf("the %s class does not exist", name)
@@ -91,17 +90,17 @@ func (c *context) call(name, method string, params []reflect.Value) ([]reflect.V
9190
}
9291

9392
// CallByName
94-
func (c *context) CallByName(name, method string, params []reflect.Value) ([]reflect.Value, error) {
93+
func (c *Context) CallByName(name, method string, params []reflect.Value) ([]reflect.Value, error) {
9594
return c.call(name, method, params)
9695
}
9796

9897
// CallByAny
99-
func (c *context) CallByAny(class any, method string, params []reflect.Value) ([]reflect.Value, error) {
98+
func (c *Context) CallByAny(class any, method string, params []reflect.Value) ([]reflect.Value, error) {
10099
name := reflect.TypeOf(class).String()
101100
return c.call(name, method, params)
102101
}
103102

104-
func (c *context) getMethods(name string) []string {
103+
func (c *Context) getMethods(name string) []string {
105104
methodMap, ok := c.classesMethodMap[name]
106105
if !ok {
107106
return nil
@@ -114,12 +113,12 @@ func (c *context) getMethods(name string) []string {
114113
}
115114

116115
// GetMethods
117-
func (c *context) GetMethodsByName(name string) []string {
116+
func (c *Context) GetMethodsByName(name string) []string {
118117
return c.getMethods(name)
119118
}
120119

121120
// GetMethodsByAny
122-
func (c *context) GetMethodsByAny(class any) []string {
121+
func (c *Context) GetMethodsByAny(class any) []string {
123122
name := reflect.TypeOf(class).String()
124123
return c.getMethods(name)
125124
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ require (
66
github.com/gin-gonic/gin v1.9.0
77
github.com/go-playground/assert/v2 v2.2.0
88
github.com/google/uuid v1.3.0
9+
github.com/natefinch/lumberjack v2.0.0+incompatible
10+
go.uber.org/zap v1.24.0
911
)
1012

1113
require (
14+
github.com/BurntSushi/toml v1.2.1 // indirect
1215
github.com/bytedance/sonic v1.8.0 // indirect
1316
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
1417
github.com/gin-contrib/sse v0.1.0 // indirect
@@ -25,11 +28,15 @@ require (
2528
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
2629
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
2730
github.com/ugorji/go/codec v1.2.9 // indirect
31+
go.uber.org/atomic v1.7.0 // indirect
32+
go.uber.org/multierr v1.6.0 // indirect
2833
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
2934
golang.org/x/crypto v0.5.0 // indirect
3035
golang.org/x/net v0.7.0 // indirect
3136
golang.org/x/sys v0.5.0 // indirect
3237
golang.org/x/text v0.7.0 // indirect
3338
google.golang.org/protobuf v1.28.1 // indirect
39+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
40+
gopkg.in/yaml.v2 v2.4.0 // indirect
3441
gopkg.in/yaml.v3 v3.0.1 // indirect
3542
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
2+
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3+
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
14
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
25
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
36
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
@@ -41,8 +44,11 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
4144
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4245
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
4346
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
47+
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
48+
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
4449
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
4550
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
51+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
4652
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4753
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4854
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
@@ -60,6 +66,13 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
6066
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
6167
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
6268
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
69+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
70+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
71+
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
72+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
73+
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
74+
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
75+
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
6376
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
6477
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
6578
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
@@ -78,6 +91,10 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
7891
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
7992
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8093
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
94+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
95+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
96+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
97+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
8198
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8299
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
83100
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

log/logger.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package log
2+
3+
import (
4+
"os"
5+
"strings"
6+
"time"
7+
8+
"github.com/natefinch/lumberjack"
9+
"go.uber.org/zap"
10+
"go.uber.org/zap/zapcore"
11+
)
12+
13+
type Level func(zapcore.Level) bool
14+
15+
type Logs map[string]Level
16+
17+
// LoggerParam
18+
type LoggerParam struct {
19+
Path string `json:"path" toml:"path" yaml:"path" xml:"path"` // log file path
20+
MaxSize int `json:"maxSize" toml:"maxSize" yaml:"maxSize" xml:"maxSize"` // log file max size
21+
MaxBackups int `json:"maxBackups" toml:"maxBackups" yaml:"maxBackups" xml:"maxBackups"` // log file max backups
22+
MaxAge int `json:"maxAge" toml:"maxAge" yaml:"maxAge" xml:"maxAge"` // log file max save day
23+
Compress bool `json:"compress" toml:"compress" yaml:"compress" xml:"compress"` // log file whether to compress
24+
}
25+
26+
// DebugLevel method is Logs map value
27+
func DebugLevel(level zapcore.Level) bool {
28+
return level == zap.DebugLevel
29+
}
30+
31+
// InfoLevel method is Logs map value
32+
func InfoLevel(level zapcore.Level) bool {
33+
return level == zap.InfoLevel
34+
}
35+
36+
// WarnLevel method is Logs map value
37+
func WarnLevel(level zapcore.Level) bool {
38+
return level == zap.WarnLevel
39+
}
40+
41+
// ErrorLevel method is Logs map value
42+
func ErrorLevel(level zapcore.Level) bool {
43+
return level == zap.ErrorLevel
44+
}
45+
46+
// DPanicLevel method is Logs map value
47+
func DPanicLevel(level zapcore.Level) bool {
48+
return level == zap.DPanicLevel
49+
}
50+
51+
// PanicLevel method is Logs map value
52+
func PanicLevel(level zapcore.Level) bool {
53+
return level == zap.PanicLevel
54+
}
55+
56+
// FatalLevel method is Logs map value
57+
func FatalLevel(level zapcore.Level) bool {
58+
return level == zap.FatalLevel
59+
}
60+
61+
// Default
62+
func (log *LoggerParam) Default() *zap.SugaredLogger {
63+
logLevels := Logs{
64+
"debug": DebugLevel,
65+
"info": InfoLevel,
66+
"warn": WarnLevel,
67+
"error": ErrorLevel,
68+
"dpanic": DPanicLevel,
69+
"panic": PanicLevel,
70+
"fatal": FatalLevel,
71+
}
72+
return log.New(logLevels)
73+
}
74+
75+
// New
76+
func (log *LoggerParam) New(logs Logs) *zap.SugaredLogger {
77+
encoderConfig := zapcore.EncoderConfig{
78+
TimeKey: "time",
79+
LevelKey: "level",
80+
NameKey: "log",
81+
CallerKey: "lineNum",
82+
MessageKey: "msg",
83+
StacktraceKey: "stacktrace",
84+
LineEnding: zapcore.DefaultLineEnding,
85+
EncodeLevel: zapcore.LowercaseLevelEncoder,
86+
EncodeTime: func(t time.Time, pae zapcore.PrimitiveArrayEncoder) {
87+
pae.AppendString(t.Format("[2006-01-02 15:04:05]"))
88+
},
89+
EncodeDuration: zapcore.SecondsDurationEncoder,
90+
EncodeCaller: zapcore.FullCallerEncoder,
91+
EncodeName: zapcore.FullNameEncoder,
92+
}
93+
94+
cores := make([]zapcore.Core, 0)
95+
96+
for fileName := range logs {
97+
if fileName == "" {
98+
continue
99+
}
100+
// logger write
101+
write := &lumberjack.Logger{
102+
Filename: getLogFilePath(log.Path, fileName),
103+
MaxSize: log.MaxSize,
104+
MaxBackups: log.MaxBackups,
105+
MaxAge: log.MaxAge,
106+
Compress: log.Compress,
107+
}
108+
// the log level
109+
level := zap.LevelEnablerFunc(logs[fileName])
110+
// log core
111+
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(write)), level)
112+
cores = append(cores, core)
113+
}
114+
// append default info log level
115+
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), zap.InfoLevel))
116+
117+
core := zapcore.NewTee(cores...)
118+
caller := zap.AddCaller()
119+
development := zap.Development()
120+
121+
return zap.New(core, caller, development, zap.Fields()).Sugar()
122+
}
123+
124+
// logFile logger file out path
125+
func getLogFilePath(filePath, fileName string) string {
126+
filePath = strings.ReplaceAll(filePath, "\\", "/")
127+
fp := strings.Split(filePath, "/")
128+
realPath := make([]string, 0)
129+
for i := range fp {
130+
if fp[i] != "" {
131+
realPath = append(realPath, fp[i])
132+
}
133+
}
134+
135+
filePath = strings.Join(realPath, "/")
136+
if filePath == "" {
137+
filePath = "."
138+
}
139+
140+
if !strings.HasSuffix(fileName, ".log") {
141+
fileName = strings.ReplaceAll(fileName, ".", "_")
142+
fileName = fileName + ".log"
143+
}
144+
return filePath + "/" + fileName
145+
}

log/logger_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package log_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/miacio/varietas/log"
7+
)
8+
9+
func TestLogger(t *testing.T) {
10+
logParam := log.LoggerParam{
11+
Path: "./log",
12+
MaxSize: 256,
13+
MaxBackups: 10,
14+
MaxAge: 7,
15+
Compress: false,
16+
}
17+
logLevels := log.Logs{
18+
"debug": log.DebugLevel,
19+
"info": log.InfoLevel,
20+
"error": log.ErrorLevel,
21+
}
22+
log := logParam.New(logLevels)
23+
24+
log.Infoln("init success")
25+
}

util/file_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ func FileIsDir(path string) (bool, error) {
2525
return fileInfo.IsDir(), nil
2626
}
2727

28-
// FolderIsNotExistThenMkdir
28+
// FolderNotExistMkdir
2929
// check path folder is exist, if not exist then mkdirall the path
3030
// else os.Stat(path) os.IsNotExist false, then return err, else return nil
3131
// so used the method then check this err == nil then success.
32-
func FolderIsNotExistThenMkdir(path string) error {
32+
func FolderNotExistMkdir(path string) error {
3333
if _, err := os.Stat(path); err != nil {
3434
if os.IsNotExist(err) {
3535
return os.MkdirAll(path, os.ModePerm)

0 commit comments

Comments
 (0)