project init
This commit is contained in:
		
							parent
							
								
									9d7405d0c2
								
							
						
					
					
						commit
						6f877bb498
					
				
							
								
								
									
										61
									
								
								function.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								function.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
package grlogs
 | 
			
		||||
 | 
			
		||||
//传入一个字符串,返回一个加密后的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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								log.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								log.go
									
									
									
									
									
								
							@ -6,7 +6,6 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/astaxie/beego"
 | 
			
		||||
	"github.com/astaxie/beego/logs"
 | 
			
		||||
	"lot.gaore.com/library/common"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@ -75,7 +74,7 @@ func GetLogger(name string) *Logger {
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		conf1 := FileLogConfig{
 | 
			
		||||
			Filename: common.GetCwd(fmt.Sprintf("runtime/logs/%s.log", name)),
 | 
			
		||||
			Filename: GetCwd(fmt.Sprintf("runtime/logs/%s.log", name)),
 | 
			
		||||
			Level:    LEVEL_ALL,
 | 
			
		||||
			Maxlines: 0,
 | 
			
		||||
			Daily:    true,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user