加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log.go 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
fthandsome 提交于 2020-01-07 16:50 . add marshal example
package tcpx
import (
"fmt"
"github.com/fwhezfwhez/errorx"
"log"
"os"
"strings"
)
const (
// debug mode, logger of tcpx will print
DEBUG = 1 + iota
// release mode, logger of tcpx will not print
RELEASE
)
// tcpx logger
type Log struct {
Logger *log.Logger
Mode int
}
// Set mode of logger, value is tcpx.DEBUG, tcpx.RELEASE
func (l *Log) SetLogMode(mode int) {
l.Mode = mode
}
// Set logger flags, value of flags are the same as the official log
func (l *Log) SetLogFlags(flags int) {
l.Logger.SetFlags(flags)
}
// Println info in debug mode, do nothing in release mode
func (l Log) Println(info ...interface{}) {
rs:= fmt.Sprintf("%v", info)
rs = strings.TrimPrefix(rs, "[")
rs = strings.TrimSuffix(rs, "]")
if l.Mode == DEBUG {
fmt.Println(errorx.NewFromStringWithDepth(rs, 2).Error())
}
}
// Global instance of logger
var Logger = Log{
Logger: log.New(os.Stderr, "[tcpx] ", log.LstdFlags|log.Llongfile),
Mode: DEBUG,
}
// Set global instance logger mode
func SetLogMode(mode int) {
Logger.Mode = mode
}
// Set global instance logger flags
func SetLogFlags(flags int) {
Logger.Logger.SetFlags(flags)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化