GithubHelp home page GithubHelp logo

zztroot / zztlog Goto Github PK

View Code? Open in Web Editor NEW
13.0 13.0 1.0 42 KB

(zztlog)golang日志库(golang log/logger),支持输出到终端、文件,可以设置文件大小切割,终端颜色显示,显示文件名称或全路径,显示行数,显示函数名称等等

License: MIT License

Go 100.00%

zztlog's People

Contributors

zztroot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

codecmn

zztlog's Issues

日志格式化问题

下面的代码会将字符串中的%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

如下是我实现的一个打印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.

BUG复现

  • 当瞬间并发上千之后日志文件写入超出预定限制例如: 超出行数 或 超出写入大小

在日志文件超过限定大小后备份的日志文件名称无法自定义,并且尝试使用时间作为文件名也不会动态的更新

下面是我的测试代码

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" 动态的修改写入路径并且我无法定义备份日志的名称和路径问题

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.