go工程实践之日志分文件

git clone https://gitee.com/leijmdas/gobase.git

func (suite *TestHttpCliWecliQuerySuite) Test014_clone() {
    var cfg = ichubconfig.FindBeanIchubConfig()
    cfg.Read()
    var ccc = cfg.Clone().(*ichubconfig.IchubConfig)
    ccc.Web.Server.Name = "12"
    golog.Info(ccc)
    golog.Error(ccc)
    golog.Stat(ccc)
}

package goutils

import (
    "context"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/base/baseutils"
    "git.ichub.com/general/webcli120/goconfig/base/fileutils"
    "github.com/gogf/gf/util/gconv"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/glog"
    "strings"

    "github.com/sirupsen/logrus"
)

/*
 * @Description:
 * @FilePath: \goconfig\common\base\baseutils\golog\go_log.go
 */

var logdir = fileutils.FindRootDir() + "/logs"

func init() {
    fileutils.MakedirAll(logdir)
}

const (
    RotateSize = "100M" // 1024 * 1024 * 20

    logfile_server = "ichub.log"
    logfile_rule   = "ichub_rule.log"
    logfile_stat   = "ichub_stat.log"
    logfile_cli    = "ichub_cli.log"
    logfile_err    = "ichub_err.log"
)

// https://blog.csdn.net/weixin_51261234/article/details/124504638
type GoLog struct {
    basedto.BaseEntitySingle

    Name string
    *glog.Logger
}

func NewGoLog() *GoLog {
    var log = &GoLog{Logger: glog.New()}
    return log.init()
}
func (golog *GoLog) init() *GoLog {

    golog.Logger.SetConfigWithMap(g.Map{
       "path":                 logdir,
       "level":                "all",
       "file":                 logfile_server,
       "stdout":               true,
       "StStatus":             0,
       "RotateSize":           RotateSize,
       "format":               "json",
       "RotateBackupLimit":    10,
       "RotateBackupExpire":   "7d",
       "RotateBackupCompress": 9,
    })
    golog.Logger.SetWriterColorEnable(true)

    return golog
}
func StdTextProc(v ...any) string {
    var vv = gconv.SliceStr(v)
    var s = strings.Join(vv, "\\n")
    var Buffer = new(strings.Builder)
    for _, line := range strings.Split(s, "\\n") {
       line = strings.ReplaceAll(line, `\"`, `"`)
       Buffer.WriteString(strings.ReplaceAll(line, "\\\\", `\`))
       Buffer.WriteString("\r\n")
    }
    return Buffer.String()
}
func IfNil(v ...any) {
    for i := 0; i < len(v); i++ {
       if baseutils.IfPtrNil(v[i]) {

          v[i] = "nil"
       }
    }
}
func Info(v ...any) {
    IfNil(v...)
    FindBeanGoLog().Info(context.Background(), v...)
    logrus.Info(v...)
}
func Debug(v ...interface{}) {
    IfNil(v...)
    FindBeanGoLog().Debug(context.Background(), v...)

}
func Warn(v ...interface{}) {
    IfNil(v...)
    FindBeanGoLog().Warning(context.Background(), v...)

}
package goutils

import (
    "context"
    "fmt"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "github.com/gogf/gf/os/glog"
)

type GoLogerr struct {
    basedto.BaseEntitySingle
    *GoLog
}

func NewGoLogServerErr() *GoLogerr {
    var g = &GoLogerr{
       GoLog: NewGoLog(),
    }

    g.Logger.SetFile(logfile_err)
    g.Logger.SetLevel(glog.LEVEL_ERRO)
    return g
}

func Errorf(f string, v ...interface{}) {
    IfNil(v...)
    FindBeanGoLogerr().Errorf(context.Background(), f, v)

}
func Error(v ...interface{}) {
    IfNil(v...)
    fmt.Println(v...)
    FindBeanGoLogerr().Error(context.Background(), v...)

}
package goutils

import (
    "context"
    "git.ichub.com/general/webcli120/goconfig/base/basedto"
    "git.ichub.com/general/webcli120/goconfig/base/baseutils"
    "git.ichub.com/general/webcli120/goconfig/base/fileutils"
    "github.com/gogf/gf/util/gconv"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/glog"
    "strings"

    "github.com/sirupsen/logrus"
)

/*
 * @Description:
 * @FilePath: \goconfig\common\base\baseutils\golog\go_log.go
 */

var logdir = fileutils.FindRootDir() + "/logs"

func init() {
    fileutils.MakedirAll(logdir)
}

const (
    RotateSize = "100M" // 1024 * 1024 * 20

    logfile_server = "ichub.log"
    logfile_rule   = "ichub_rule.log"
    logfile_stat   = "ichub_stat.log"
    logfile_cli    = "ichub_cli.log"
    logfile_err    = "ichub_err.log"
)

// https://blog.csdn.net/weixin_51261234/article/details/124504638
type GoLog struct {
    basedto.BaseEntitySingle

    Name string
    *glog.Logger
}

func NewGoLog() *GoLog {
    var log = &GoLog{Logger: glog.New()}
    return log.init()
}
func (golog *GoLog) init() *GoLog {

    golog.Logger.SetConfigWithMap(g.Map{
       "path":                 logdir,
       "level":                "all",
       "file":                 logfile_server,
       "stdout":               true,
       "StStatus":             0,
       "RotateSize":           RotateSize,
       "format":               "json",
       "RotateBackupLimit":    10,
       "RotateBackupExpire":   "7d",
       "RotateBackupCompress": 9,
    })
    golog.Logger.SetWriterColorEnable(true)

    return golog
}
func StdTextProc(v ...any) string {
    var vv = gconv.SliceStr(v)
    var s = strings.Join(vv, "\\n")
    var Buffer = new(strings.Builder)
    for _, line := range strings.Split(s, "\\n") {
       line = strings.ReplaceAll(line, `\"`, `"`)
       Buffer.WriteString(strings.ReplaceAll(line, "\\\\", `\`))
       Buffer.WriteString("\r\n")
    }
    return Buffer.String()
}
func IfNil(v ...any) {
    for i := 0; i < len(v); i++ {
       if baseutils.IfPtrNil(v[i]) {
          v[i] = "nil"
       }
    }
}
func Info(v ...any) {
    IfNil(v...)
    FindBeanGoLog().Info(context.Background(), v...)
    logrus.Info(v...)
}
func Debug(v ...interface{}) {
    IfNil(v...)
    FindBeanGoLog().Debug(context.Background(), v...)

}
func Warn(v ...interface{}) {
    IfNil(v...)
    FindBeanGoLog().Warning(context.Background(), v...)

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leijmdas

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值