Commit 6142204c by zhengqiuyun86

初始化

parent dc7cf41f
package conf
type Config struct {
ENV string
ServerName string
ShowSql bool
LogLevel string
LogColorful bool
LogResponseLength int
}
func BaseConfig(conf Config) {
ENV = conf.ENV
ServerName = conf.ServerName
ShowSql = conf.ShowSql
LogLevel = conf.LogLevel
LogColorful = conf.LogColorful
LogResponseLength = conf.LogResponseLength
}
var ENV = "test"
var ServerName = "git.168cad.top/zhengqiuyun/rym-util"
//ShowSql 是否打印SQL
var ShowSql = true //是否打印SQL
var LogLevel = "Debug"
//LogColorful 日志是否开启彩色打印
var LogColorful = true
var LogResponseLength = 1000
......@@ -2,14 +2,14 @@ package exception
import (
"errors"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/go-playground/validator/v10"
"reflect"
)
func ThrowsErr(err error) {
if err != nil {
rym_util.Error(err.Error())
log.Error(err.Error())
panic(err)
}
}
......@@ -57,7 +57,7 @@ func (e *DError) Error() string {
}
func ThrowsErrS(err string) {
if &err != nil && err != "" {
rym_util.Error(err)
log.Error(err)
panic(DError{Err: err})
}
}
......@@ -2,7 +2,7 @@ package generate
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"github.com/deckarep/golang-set"
"github.com/iancoleman/strcase"
"io"
......@@ -49,7 +49,7 @@ func BuildPgModels() {
doTable, ok := DoTables[table.Name]
if ok {
var tableFields []TableField
if rym_util.DBType == "PostgreSql" {
if conf.DBType == "PostgreSql" {
tableFields = GetTableFiledByTableName(table.Name)
} else {
tableFields = MysqlGetTableFiledByTableName(table.Name)
......@@ -83,7 +83,7 @@ func getModelImport(field TableField) string {
}
func getModelFiledType(field TableField) string {
if rym_util.DBType == "PostgreSql" {
if conf.DBType == "PostgreSql" {
return DbToGoPG[field.FieldType]
} else {
if strings.Index(field.ColumnType, "unsigned") > 0 {
......@@ -294,10 +294,10 @@ func generateDao(table *Table, fields []TableField, doTable TableInfo) {
pageQuery += "}\n"
importStr := "import (\n"
importStr += "\t\"" + projectName + "/a/PagePlus\"\n"
if rym_util.DBType == "" {
if conf.DBType == "" {
importStr += "\t\"" + projectName + "/a/db\"\n"
} else {
importStr += "\t\"" + projectName + "/a/db/" + rym_util.DBType + "\"\n"
importStr += "\t\"" + projectName + "/a/db/" + conf.DBType + "\"\n"
}
importStr += "\t\"" + projectName + "/a/exception\"\n"
importStr += "\t\"" + projectName + "/a/page\"\n"
......
......@@ -3,9 +3,9 @@ package getWay
import (
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/gin-gonic/gin"
"io/ioutil"
"net"
......@@ -45,18 +45,18 @@ func GetAdminInfo(token string) Admin {
var adminResponse = AdminResponse{}
resp, err := http.Get(conf.SystemHost + "/admin/current?token=" + token)
if err != nil {
rym_util.Error(err.Error())
log.Error(err.Error())
return Admin{}
}
defer resp.Body.Close()
adminResponseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
rym_util.Error(err.Error())
log.Error(err.Error())
return Admin{}
}
err = json.Unmarshal(adminResponseBytes, &adminResponse)
if err != nil {
rym_util.Error(err.Error())
log.Error(err.Error())
}
return adminResponse.Admin
}
......@@ -145,13 +145,13 @@ func ValidRequest(c *gin.Context) {
token := GetToken(c)
if token == "" {
if checkIp(ip) { //如果没有token,看是否是ip白名单
rym_util.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip))
log.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip))
} else {
rym_util.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip))
log.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip))
if checkUri(url) { //如果不是ip白名单,看是否是url白名单,例如一些第三方的回调
rym_util.Debug(fmt.Sprintf("URL白名单校验通过:%s", url))
log.Debug(fmt.Sprintf("URL白名单校验通过:%s", url))
} else {
rym_util.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url))
log.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url))
ThrowsGetWayErr(Illegal9)
}
}
......@@ -159,9 +159,9 @@ func ValidRequest(c *gin.Context) {
if redis.Exists(token) { //校验token是否有效
currentUser := GetAdminInfo(token)
c.Set(CurrentUserKey, currentUser)
rym_util.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id))
log.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id))
} else {
rym_util.Debug(fmt.Sprintf("登陆校验不通过:%s", token))
log.Debug(fmt.Sprintf("登陆校验不通过:%s", token))
ThrowsGetWayErr(Expire)
}
}
......@@ -180,7 +180,7 @@ func exceptionGetWayCatch(c *gin.Context) {
}
func checkUri(uri string) bool {
for _, v := range rym_util.WhiteListUri {
for _, v := range conf.WhiteListUri {
if strings.Index(uri, v) == 0 {
return true
}
......@@ -189,7 +189,7 @@ func checkUri(uri string) bool {
}
func checkIp(ip string) bool {
for _, v := range rym_util.WhiteListIp {
for _, v := range conf.WhiteListIp {
if strings.Index(ip, v) == 0 {
return true
}
......@@ -198,5 +198,5 @@ func checkIp(ip string) bool {
}
func logStart(c *gin.Context) {
rym_util.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c)))
log.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c)))
}
......@@ -3,11 +3,11 @@ package getWay
import (
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
db "git.168cad.top/zhengqiuyun/rym-util/a/db/mysql"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/log/es"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"github.com/gin-gonic/gin"
"net/http"
"time"
......@@ -76,7 +76,7 @@ func AliSuccess(c *gin.Context) {
}
func logEnd(c *gin.Context, data interface{}) {
esSwitch := redis.SwitchOpen(redis.ES日志开关 + rym_util.ServerName)
esSwitch := redis.SwitchOpen(redis.ES日志开关 + conf.ServerName)
startTime := GetStartTime(c)
endTime := time.Now().UnixNano()
var responseStr string
......@@ -85,14 +85,14 @@ func logEnd(c *gin.Context, data interface{}) {
rs := string(dataJson)
excludeUrl := []string{""}
if esSwitch {
if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > rym_util.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......"
if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > conf.LogResponseLength {
responseStr = rs[0:conf.LogResponseLength] + "......"
} else {
responseStr = rs
}
} else {
if len(rs) > rym_util.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......"
if len(rs) > conf.LogResponseLength {
responseStr = rs[0:conf.LogResponseLength] + "......"
} else {
responseStr = rs
}
......@@ -101,18 +101,18 @@ func logEnd(c *gin.Context, data interface{}) {
token := GetToken(c)
if esSwitch && token != "" {
esLog(c, responseStr)
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6))
log.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6))
} else {
rym_util.Info(fmt.Sprintf("返回数据:%s", responseStr))
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c)))
log.Info(fmt.Sprintf("返回数据:%s", responseStr))
log.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c)))
}
}
func esLog(c *gin.Context, response string) {
log := es.ServerLogModel{
ServerName: rym_util.ServerName,
Env: rym_util.ENV,
ServerName: conf.ServerName,
Env: conf.ENV,
TimeStamp: util.MilSecond(time.Now()),
ThreadNo: fmt.Sprintf("%d", util.GetGID()),
ServerIp: GetServerIP(),
......
......@@ -7,10 +7,9 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"io"
"io/ioutil"
"net/http"
......@@ -53,7 +52,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
requestStr = p.Encode()
buf = strings.NewReader(requestStr)
}
rym_util.Debug(fmt.Sprintf("请求参数:%s", requestStr))
log.Debug(fmt.Sprintf("请求参数:%s", requestStr))
req, err := http.NewRequest(method, conf.AllinpayUrl, buf)
if err != nil {
return err
......@@ -70,7 +69,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
if err != nil {
return err
}
rym_util.Debug(fmt.Sprintf("请求结果:%s", string(data)))
log.Debug(fmt.Sprintf("请求结果:%s", string(data)))
ok, err := verifyData(data, c.tlPublicKey)
if err != nil {
return err
......@@ -145,7 +144,7 @@ func signWithPKCS1v15(param url.Values, privateKey *rsa.PrivateKey, hash crypto.
}
sort.Strings(pList)
var src = strings.Join(pList, "&")
rym_util.Debug(fmt.Sprintf("签名数据:%s", src))
log.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5S(src)
sig, err := RSASignWithKey([]byte(md5Str), privateKey, hash)
if err != nil {
......@@ -179,7 +178,7 @@ func (c *Client) getDefaultNotification(data map[string]string, sign string, res
}
sort.Strings(pList)
var src = strings.Join(pList, "&")
rym_util.Debug(fmt.Sprintf("签名数据:%s", src))
log.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5B([]byte(src))
signBytes, err := base64.StdEncoding.DecodeString(sign)
if err != nil {
......
......@@ -2,11 +2,10 @@ package main
import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception"
lib_allinpay "git.168cad.top/zhengqiuyun/rym-util/a/lib/allinpay"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"time"
)
......@@ -46,9 +45,9 @@ func testGetPayeeFundsInTransitDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%v", response.Data))
log.Info(fmt.Sprintf("%v", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -69,9 +68,9 @@ func testQueryInExpDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%v", response.Data))
log.Info(fmt.Sprintf("%v", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -106,7 +105,7 @@ func testCreateMember() {
if err != nil {
exception.ThrowsErr(err)
}
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testBindWxOpenId() {
......@@ -123,7 +122,7 @@ func testBindWxOpenId() {
if err != nil {
exception.ThrowsErr(err)
}
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testGetMemberInfo() {
......@@ -137,7 +136,7 @@ func testGetMemberInfo() {
if err != nil {
exception.ThrowsErr(err)
}
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
func testConsumeApply() {
......@@ -176,9 +175,9 @@ func testConsumeApply() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data.PayInfo))
log.Info(fmt.Sprintf("%s", response.Data.PayInfo))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -196,9 +195,9 @@ func testGetOrderDetail() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data))
log.Info(fmt.Sprintf("%s", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -214,9 +213,9 @@ func testQueryReserveFundBalance() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data))
log.Info(fmt.Sprintf("%s", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -233,9 +232,9 @@ func testQueryBalance() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data))
log.Info(fmt.Sprintf("%s", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -251,9 +250,9 @@ func testQueryBankCard() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data))
log.Info(fmt.Sprintf("%s", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -275,7 +274,7 @@ func testWithDrawApply() {
p.BackUrl = conf.MainHost + "/pay/allin/pay/draw/notify"
bankCardNo := "324348812323"
encryptBankCardNo, err := lib_allinpay.AesEncryptECB(bankCardNo, conf.AllinpaySecretKey)
rym_util.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo))
log.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo))
if err != nil {
exception.ThrowsErrS("通联密钥错误,请检查配置")
}
......@@ -291,9 +290,9 @@ func testWithDrawApply() {
exception.ThrowsErr(err)
}
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data))
log.Info(fmt.Sprintf("%s", response.Data))
} else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
}
}
......@@ -315,5 +314,5 @@ func testGetTradeNotification() {
if err != nil {
exception.ThrowsErr(err)
}
rym_util.Info(fmt.Sprintf("%v", rd))
log.Info(fmt.Sprintf("%v", rd))
}
package log
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"runtime"
"strconv"
"strings"
"time"
)
const (
DebugL = "Debug"
InfoL = "Info"
ErrorL = "Error"
)
// Colors
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
func Print(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if conf.LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + Red + "%s" + Reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + Yellow + "%s" + Reset
} else {
str = "%s %s [%d] %s %d " + Green + "%s" + Reset
}
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, FormatDateMillTime(time.Now()), tag, GetGID(), file, line, msg))
}
func FormatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
package util
package rym_util
func ArrayContains(array []string, e string) bool {
for _, v := range array {
......
package util
package rym_util
import (
"time"
......
package util
package rym_util
func IfInt(isTrue bool, a, b int) int {
if isTrue {
......
package util
package rym_util
import (
"encoding/base64"
......
package util
package rym_util
import (
"bytes"
......
package util
package rym_util
import (
"encoding/json"
......
package util
package rym_util
import (
"crypto/md5"
......
package util
package rym_util
import (
"os"
......
package util
package rym_util
func EncodePwd(loginPwd, loginPwdSalt string) string {
pingPwd := loginPwdSalt + loginPwd
......
package util
package rym_util
import (
"bytes"
......
package util
package rym_util
import (
"encoding/json"
......
package rym_util
type Config struct {
ENV string
}
func BaseConfig(conf Config) {
ENV = conf.ENV
}
var ENV = "test"
var ServerName = "git.168cad.top/zhengqiuyun/rym-util"
var DBType = "mysql"
var EsHttpHost = "http://192.168.1.124:9200"
//mysql
var DbDSN = "root:dcrym29210924..@tcp(192.168.1.37:3306)/duocaihui_dev?charset=utf8mb4&parseTime=True&loc=Local"
//ShowSql 是否打印SQL
var ShowSql = true //是否打印SQL
var LogLevel = "Debug"
var HttpServerPort = "8080"
var WhiteListIp = [...]string{"::1", "192.168.71.127", "171.88.67.133"}
var WhiteListUri = [...]string{"/swagger", "/api/app/login/get/captcha",
"/api/app/login/register/send/sms", "/api/app/login/register",
"/api/app/login/forget/pwd/send/sms", "/api/app/login/forget/pwd",
"/api/app/login/login",
"/api/app/wx/login/bind/check", "/api/app/wx/login/bind/send/sms",
"/api/app/wx/login/bind", "/api/app/wx/login", "/pay/wx/pay/notify"}
var CheckSms = false
var SmsSendUrl = "http://192.168.1.43:9001"
var SmsVerifyUrl = "http://192.168.1.43:9001"
//LogColorful 日志是否开启彩色打印
var LogColorful = true
var LogResponseLength = 1000
var CaptchaMax = 5
......@@ -3,17 +3,6 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.16
require (
github.com/deckarep/golang-set v1.7.1
github.com/garyburd/redigo v1.6.3
github.com/gin-gonic/gin v1.7.4
github.com/go-playground/validator/v10 v10.4.1
github.com/iancoleman/strcase v0.2.0
github.com/shopspring/decimal v1.2.0
github.com/wechatpay-apiv3/wechatpay-go v0.2.9
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
gorm.io/driver/mysql v1.2.2
gorm.io/driver/postgres v1.2.2
gorm.io/gorm v1.22.4
)
require (
......@@ -22,13 +11,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/olivere/elastic/v7 v7.0.32
github.com/robfig/cron v1.2.0
github.com/smartwalle/crypto4go v1.0.3
github.com/wenlng/go-captcha v1.2.5
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
replace git.168cad.top/zhengqiuyun/rym-util => ../rym-util
package rym_util
import (
"bytes"
"fmt"
"gorm.io/gorm/logger"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"runtime"
"strconv"
"strings"
"time"
)
const (
DebugL = "Debug"
InfoL = "Info"
ErrorL = "Error"
)
func DebugDo(f func()) {
......@@ -24,8 +15,8 @@ func DebugDo(f func()) {
func IsDebug() bool {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(log.DebugL):
ok = true
break
}
......@@ -34,76 +25,48 @@ func IsDebug() bool {
func Debug(msg string) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(log.DebugL):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
mark("Debug", msg, file, line)
log.Print("Debug", msg, file, line)
}
}
func Info(msg string) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(log.DebugL):
ok = true
break
case strings.ToLower(InfoL):
case strings.ToLower(log.InfoL):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
mark("Info", msg, file, line)
log.Print("Info", msg, file, line)
}
}
func Error(msg string) {
ok := false
switch strings.ToLower(LogLevel) {
case strings.ToLower(DebugL):
switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(log.DebugL):
ok = true
break
case strings.ToLower(InfoL):
case strings.ToLower(log.InfoL):
ok = true
break
case strings.ToLower(ErrorL):
case strings.ToLower(log.ErrorL):
ok = true
break
}
if ok {
_, file, line, _ := runtime.Caller(1)
mark("Error", msg, file, line)
}
}
func mark(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + logger.Red + "%s" + logger.Reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + logger.Yellow + "%s" + logger.Reset
} else {
str = "%s %s [%d] %s %d " + logger.Green + "%s" + logger.Reset
}
log.Print("Error", msg, file, line)
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, FormatDateMillTime(time.Now()), tag, GetGID(), file, line, msg))
}
func FormatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment