From ed9201b45334908dcb8873b26e4cb77e2bd94163 Mon Sep 17 00:00:00 2001 From: liangzy Date: Fri, 24 Apr 2020 17:25:37 +0800 Subject: [PATCH] =?UTF-8?q?v1.2.0=20=E6=95=B4=E4=B8=AAbeegolog=20=E6=89=93?= =?UTF-8?q?=E8=BF=9B=E5=8E=BB=EF=BC=8C=20=E5=A2=9E=E5=8A=A0syncmap?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=80=9A=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.go | 76 ----------------------------------------------------- log.go | 1 + log_test.go | 4 ++- 3 files changed, 4 insertions(+), 77 deletions(-) diff --git a/function.go b/function.go index e7d18d8..b488a2d 100644 --- a/function.go +++ b/function.go @@ -1,77 +1 @@ package grlogs - -import ( - "crypto/md5" - "encoding/hex" - "fmt" - "io/ioutil" - "math/rand" - "os" - "path" - "reflect" - "runtime" - "strings" - "time" -) - -type Empty struct {} - -//传入一个字符串,返回一个加密后的md5 -func Md5(str string) string { - data := []byte(str) - has := md5.Sum(data) - md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制 - return md5str -} - -func Md5File(filePath string) string { - hash := md5.New() - data, _ := ioutil.ReadFile(filePath) - hash.Write([]byte(data)) - md5str := hex.EncodeToString(hash.Sum(nil)) - return md5str -} - -func GetPackageName() string { - packpath := reflect.TypeOf(Empty{}).PkgPath() - if idx := strings.Index(packpath, "/"); idx > 0 { - return packpath[:idx] - } else { - return packpath - } -} - -func GetCwd(filepath ...string) string { - _, filename1, _, _ := runtime.Caller(0) - filename2, _ := os.Getwd() - if !strings.HasSuffix(filename2, GetPackageName()) { - paths := append([]string{path.Dir(filename1), "/../../"}, filepath...) - return path.Join(paths...) - } else { - paths := append([]string{filename2, "/"}, filepath...) - return path.Join(paths...) - } -} - -func SetCwd() error { - _, filename1, _, _ := runtime.Caller(0) - filename2, _ := os.Getwd() - if !strings.HasSuffix(filename2, GetPackageName()) { - paths := append([]string{path.Dir(filename1), "/../../"}) - return os.Chdir(path.Join(paths...)) - } else { - paths := append([]string{filename2, "/"}) - return os.Chdir(path.Join(paths...)) - } -} - -func RandString(length int) string { - str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - bytes := []byte(str) - result := []byte{} - r := rand.New(rand.NewSource(time.Now().UnixNano())) - for i := 0; i < length; i++ { - result = append(result, bytes[r.Intn(len(bytes))]) - } - return string(result) -} diff --git a/log.go b/log.go index 2b79a57..89e3b8b 100644 --- a/log.go +++ b/log.go @@ -75,6 +75,7 @@ func GetLogger(name string) *Logger { } wd, _ := os.Getwd() + fmt.Println(wd) conf1 := FileLogConfig{ Filename: path.Join(wd, fmt.Sprintf("runtime/logs/%s.log", name)), diff --git a/log_test.go b/log_test.go index 89327e9..67d0243 100644 --- a/log_test.go +++ b/log_test.go @@ -5,5 +5,7 @@ import ( ) func TestGetLogger(t *testing.T) { - GetLogger("nds").Debug("akldalskflasfa") + GetLogger("nds").Debug("我正在调试") + GetLogger("nds").Critical("出错了") + GetLogger("wifi").Critical("hello wifi") }