zztroot / zztlog Goto Github PK
View Code? Open in Web Editor NEW(zztlog)golang日志库(golang log/logger),支持输出到终端、文件,可以设置文件大小切割,终端颜色显示,显示文件名称或全路径,显示行数,显示函数名称等等
License: MIT License
(zztlog)golang日志库(golang log/logger),支持输出到终端、文件,可以设置文件大小切割,终端颜色显示,显示文件名称或全路径,显示行数,显示函数名称等等
License: MIT License
下面的代码会将字符串中的%S格式化成golang的占位符
而我不想在Debug是将里面的内容格式化
除非在使用Debug类似的方法时才会进行格式化
package main
import (
"os"
"path"
"time"
"github.com/zztroot/zztlog"
)
// 日志配置
var (
_PATH, _ = os.Getwd()
LOGPATH = path.Join(_PATH, "./log")
logger = zztlog.Default() // 实例化日志打印
)
// 初始化日志配置
func InitLog() {
if _, err := os.Stat(LOGPATH); os.IsNotExist(err) {
os.Mkdir(LOGPATH, 0755)
}
zztlog.InitConfig(
zztlog.BaseConfig{
LogConfig: zztlog.LogConfig{
TimeFormat: "2006-01-02 15:04:05", // 时间格式
Prefix: "", //最前面显示的前缀
FileOutput: true, //是否输出到文件(默认为false)
CmdOutput: true, //是否输出到终端(默认为true)
FileAllPathOutput: true, //是否显示文件全路径(默认只显示文件名称faslse)
FuncNameOutput: false, //是否显示函数名称(默认不显示false)
MaxFileLine: 1e4, //写入最大文件行数
MaxSizeM: 10, //写入文件的最大大小,单位M,当达到最大时将会写入新的日志文件中。(默认大小10M)
SaveFileName: path.Join(LOGPATH, time.Now().Format("2006-1-02-15")+".log"), //写入文件的名称(默认zztlog.log)
ErrorOutput: true, //是否输出error信息(默认为true)
FatalOutput: true, //是否输出fatal信息(默认为true)
WarnOutput: true, //是否输出warn信息(默认为true)
InfoOutput: true, //是否输出info信息(默认为true)
DebugOutput: true, //是否输出debug信息(默认为true)
ColourOutput: true, //输出是否带颜色(默认为false)
},
},
) // 初始化日志配置
}
func main(){
InitLog()
logger.Debug("https%3A%2F%2Fseq")
}
如下是我实现的一个打印table的一个demo,奈何本人示例有限旺大佬帮帮忙
package main
import (
"fmt"
"os"
"path"
"reflect"
"strings"
"time"
"github.com/zztroot/zztlog"
"golang.org/x/text/width"
)
// 日志配置
var (
_PATH, _ = os.Getwd()
LOGPATH = path.Join(_PATH, "./log")
logger = zztlog.Default() // 实例化日志打印
)
// 初始化日志配置
func InitLog() {
if _, err := os.Stat(LOGPATH); os.IsNotExist(err) {
os.Mkdir(LOGPATH, 0755)
}
zztlog.InitConfig(
zztlog.BaseConfig{
LogConfig: zztlog.LogConfig{
TimeFormat: "2006-01-02 15:04:05", // 时间格式
Prefix: "", //最前面显示的前缀
FileOutput: true, //是否输出到文件(默认为false)
CmdOutput: true, //是否输出到终端(默认为true)
FileAllPathOutput: true, //是否显示文件全路径(默认只显示文件名称faslse)
FuncNameOutput: false, //是否显示函数名称(默认不显示false)
MaxFileLine: 1e4, //写入最大文件行数
MaxSizeM: 10, //写入文件的最大大小,单位M,当达到最大时将会写入新的日志文件中。(默认大小10M)
SaveFileName: path.Join(LOGPATH, time.Now().Format("2006-01-02 15.04.05")+".log"), //写入文件的名称(默认zztlog.log)
ErrorOutput: true, //是否输出error信息(默认为true)
FatalOutput: true, //是否输出fatal信息(默认为true)
WarnOutput: true, //是否输出warn信息(默认为true)
InfoOutput: true, //是否输出info信息(默认为true)
DebugOutput: true, //是否输出debug信息(默认为true)
ColourOutput: true, //输出是否带颜色(默认为false)
},
},
) // 初始化日志配置
}
func GetStringCount(s string) int {
w := 0
for _, r := range s {
info := width.LookupRune(r)
switch info.Kind() {
case width.EastAsianWide, width.EastAsianFullwidth:
w += 2 // wide characters take two columns
default:
w += 1 // other characters take one column
}
}
return w
}
func Table(Msg interface{}) {
key := "(index)"
value := "value"
if slice, ok := Msg.([]string); ok {
indexWidth := GetStringCount(key)
valueWidth := GetStringCount(value)
indexWidth2 := 0
valueWidth2 := 0
for index, item := range slice {
if i := GetStringCount(string(rune(index))); indexWidth2 < i {
indexWidth2 = i
}
if v := GetStringCount(item); valueWidth2 < v {
valueWidth2 = v
}
}
if indexWidth2%2 != 0 {
indexWidth2++
}
if valueWidth2%2 != 0 {
valueWidth2++
}
indexWidth += indexWidth2
valueWidth += valueWidth2
tableData := []string{
fmt.Sprintf("┌%s┬%s┐", strings.Repeat("─", indexWidth), strings.Repeat("─", valueWidth)),
fmt.Sprintf(
"│%s%s%s│%s%s%s│",
strings.Repeat(" ", indexWidth2/2),
key,
strings.Repeat(" ", indexWidth2/2),
strings.Repeat(" ", valueWidth2/2),
value,
strings.Repeat(" ", valueWidth2/2),
),
fmt.Sprintf("├%s┼%s┤", strings.Repeat("─", indexWidth), strings.Repeat("─", valueWidth)),
func() string {
data := []string{}
for index, item := range slice {
left_index := indexWidth - GetStringCount(string(rune(index)))
right_value := valueWidth - GetStringCount(item)
if left_index%2 != 0 {
left_index++
}
if right_value%2 != 0 {
right_value++
}
data = append(data, fmt.Sprintf(
"│%s%v%s│%s%s%s│",
strings.Repeat(" ", left_index/2),
index,
strings.Repeat(" ", left_index/2),
strings.Repeat(" ", right_value/2),
item,
strings.Repeat(" ", right_value/2),
))
}
return strings.Join(data, "\r\n")
}(),
fmt.Sprintf("└%s┴%s┘", strings.Repeat("─", indexWidth), strings.Repeat("─", valueWidth)),
}
fmt.Println(strings.Join(tableData, "\r\n"))
}
}
func IsArrayOrSlice[T any](x T) bool {
return reflect.TypeOf(x).Kind() == reflect.Array || reflect.TypeOf(x).Kind() == reflect.Slice
}
func main() {
InitLog()
logger.Debug("Test Table")
Table([]string{"a"})
// Table(map[string]string{"a": "b"})
Table([]string{"中文测试"})
// Table(map[string]string{"a": "中文测试"})
// Table(map[string]string{"中文测试": "a"})
Table([]string{"中英混合测试 Test"})
Table(map[string]interface{}{"a": "b", "c": []string{"d", "e"}})
}
2023/03/07 00:28:25 open log\2023-03-07 00:28:25.log: The filename, directory name, or volume label syntax is incorrect.
panic: open log\2023-03-07 00:28:25.log: The filename, directory name, or volume label syntax is incorrect.
下面是我的测试代码
package main
import (
"os"
"path"
"time"
"github.com/zztroot/zztlog"
)
// 日志配置
var (
_PATH, _ = os.Getwd()
LOGPATH = path.Join(_PATH, "./log")
log = zztlog.Default() // 实例化日志打印
)
func InitLog() {
if _, err := os.Stat(LOGPATH); os.IsNotExist(err) {
os.Mkdir(LOGPATH, 0755)
}
zztlog.InitConfig(
zztlog.BaseConfig{
LogConfig: zztlog.LogConfig{
TimeFormat: "2006-01-02 15:04:05", // 时间格式
Prefix: "", //最前面显示的前缀
FileOutput: true, //是否输出到文件(默认为false)
CmdOutput: true, //是否输出到终端(默认为true)
FileAllPathOutput: true, //是否显示文件全路径(默认只显示文件名称faslse)
FuncNameOutput: false, //是否显示函数名称(默认不显示false)
MaxFileLine: 10, //写入最大文件行数
MaxSizeM: 10, //写入文件的最大大小,单位M,当达到最大时将会写入新的日志文件中。(默认大小10M)
SaveFileName: path.Join(LOGPATH, time.Now().Format("2006-01-02 15.04.05")+".log"), //写入文件的名称(默认zztlog.log)
ErrorOutput: true, //是否输出error信息(默认为true)
FatalOutput: true, //是否输出fatal信息(默认为true)
WarnOutput: true, //是否输出warn信息(默认为true)
InfoOutput: true, //是否输出info信息(默认为true)
DebugOutput: true, //是否输出debug信息(默认为true)
ColourOutput: true, //输出是否带颜色(默认为false)
},
},
) // 初始化日志配置
}
func main() {
InitLog()
for {
log.Debug(_PATH)
time.Sleep(1 * time.Second)
}
}
无法通过代码 time.Now().Format("2006-01-02 15.04.05")+".log"
动态的修改写入路径并且我无法定义备份日志的名称和路径问题
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.