Skip to content

v0.27.0

Compare
Choose a tag to compare
@donnie4w donnie4w released this 26 Oct 04:29
· 14 commits to master since this release

version 0.27.0
更新内容


说明

  1. 优化内存分配
  2. 优化写数据性能
  3. 增加日志属性自定义函数
  4. 增加各个日志级别格式化打印函数

说明

性能优化是该版本最重要的更新内容。性能优化的结果:

  1. 极高并发性能:极高的并发写数据性能,比官方库或同类型日志库高10倍以上。特别在Linux环境中,性能比同类型日志库高30倍以上.
  2. 极低内存占用:是官方库与同类型日志库的几分之一

由于压测数据篇幅过长,可以通过 使用文档 或 《go日志库性能基准压力测试:go-logger+slog+zap+log》 查看

以下新增功能详细说明:

1. 增加日志属性自定义函数 AttrFormat

通过 AttrFormat 可以对日志的各个属性标识进行自定义格式化设置

示例1:

 func Test_AttrFormat(t *testing.T) {
	attrformat := &logger.AttrFormat{
		SetLevelFmt: func(level logger.LEVELTYPE) string {
			switch level {
			case logger.LEVEL_DEBUG:
				return "debug:"
			case logger.LEVEL_INFO:
				return "info:"
			case logger.LEVEL_WARN:
				return "warn:"
			case logger.LEVEL_ERROR:
				return "error>>>>"
			case logger.LEVEL_FATAL:
				return "[fatal]"
			default:
				return "[unknown]"
			}
		},
		SetTimeFmt: func() (string, string, string) {
			s := time.Now().Format("2006-01-02 15:04:05")
			return s, "", ""
		},
	}
	logger.SetOption(&logger.Option{AttrFormat: attrformat, Console: true, FileOption: &logger.FileTimeMode{Filename: "testlogtime.log", Maxbuckup: 3, IsCompress: false, Timemode: logger.MODE_MONTH}})
	logger.Debug("this is a debug message", 1111111111111111111)
	logger.Info("this is a info message", 2222222222222222222)
	logger.Warn("this is a warn message", 33333333333333333)
	logger.Error("this is a error message", 4444444444444444444)
	logger.Fatal("this is a fatal message", 555555555555555555)
}

执行结果:

debug:2024-10-26 12:03:21 0_27_0_test.go:33 this is a debug message1111111111111111111
info:2024-10-26 12:03:21 0_27_0_test.go:34 this is a info message2222222222222222222
warn:2024-10-26 12:03:21 0_27_0_test.go:35 this is a warn message33333333333333333
error>>>>2024-10-26 12:03:21 0_27_0_test.go:36 this is a error message4444444444444444444
[fatal]2024-10-26 12:03:21 0_27_0_test.go:37 this is a fatal message555555555555555555

说明:修改了 LEVEL的标识 与时间的格式

示例2

func Test_AttrFormat2(t *testing.T) {
	attrformat := &logger.AttrFormat{
		SetBodyFmt: func(level logger.LEVELTYPE, bs []byte) []byte {
			//处理日志末尾换行符
			if size := len(bs); bs[size-1] == '\n' {
				bs = append(bs[:size-1], []byte("\x1b[0m\n")...)
			} else {
				bs = append(bs, []byte("\x1b[0m\n")...)
			}
			switch level {
			case logger.LEVEL_DEBUG:
				return append([]byte("\x1b[34m"), bs...)
			case logger.LEVEL_INFO:
				return append([]byte("\x1b[32m"), bs...)
			case logger.LEVEL_WARN:
				return append([]byte("\x1b[33m"), bs...)
			case logger.LEVEL_ERROR:
				return append([]byte("\x1b[31m"), bs...)
			case logger.LEVEL_FATAL:
				return append([]byte("\x1b[41m"), bs...)
			default:
				return bs
			}
		},
	}
	logger.SetOption(&logger.Option{AttrFormat: attrformat, Console: true, FileOption: &logger.FileTimeMode{Filename: "testlogtime.log", Maxbuckup: 3, IsCompress: false, Timemode: logger.MODE_MONTH}})
	logger.Debug("this is a debug message:", 111111111111111110)
	logger.Info("this is a info message:", 222222222222222220)
	logger.Warn("this is a warn message:", 333333333333333330)
	logger.Error("this is a error message:", 4444444444444444440)
	logger.Fatal("this is a fatal message:", 5555555555555555550)
}

执行结果

2. 增加各个日志级别格式化打印函数

通过 Debugf, Infof, Warnf, Errorf, Fatalf 等函数,支持在打印函数中使用 % 符号,用于指定输出格式化模式。

示例

func Test_format(t *testing.T) {
	logger.Debugf("this is a debugf message:%d", 1)
	logger.Infof("this is a infof message:%s", "hi,logger")
	logger.Warnf("this is a warnf message:%x,%x", 14, 15)
	logger.Errorf("this is a errorf message:%f", 44.4444)
	logger.Fatalf("this is a fatalf message:%t", true)
	logger.Debugf("this is a debugf message:%p", new(int))
}

执行结果

[DEBUG]2024/10/26 12:08:55 0_27_0_test.go:74 this is a debugf message:1
[INFO]2024/10/26 12:08:55 0_27_0_test.go:75 this is a infof message:hi,logger
[WARN]2024/10/26 12:08:55 0_27_0_test.go:76 this is a warnf message:e,f
[ERROR]2024/10/26 12:08:55 0_27_0_test.go:77 this is a errorf message:44.444400
[FATAL]2024/10/26 12:08:55 0_27_0_test.go:78 this is a fatalf message:true
[DEBUG]2024/10/26 12:08:55 0_27_0_test.go:79 this is a debugf message:0xc00000a938