Categorygithub.com/treeforest/logger
modulepackage
0.3.0
Repository: https://github.com/treeforest/logger.git
Documentation: pkg.go.dev

# README

Logger

Go 轻量级的日志库

使用方式

导入日志包

import (
	log "github.com/treeforest/logger"
)

输出到控制台

  • 默认控制台输出(同步写)
var defaultLogger Logger = NewStdLogger()
...
log.Debug(...) // std output
  • 用户自定义控制台日志对象
l := log.NewStdLogger(
  log.WithLevel(log.DEBUG),
  log.WithPrefix("test"),
)
...
l.Debug("hello world")

输出到文件

  • 日志分割模式

    定时切割:每天24点将会进行日志的切割,把每天的日志分开存储。

    阈值切割:在日志写的时候触发检查日志文件大小的事件,若达到阈值,则进行日志切割。

  • 日志写模式

    同步写:每条日志的打印将会进行一次磁盘的刷新,将缓冲区中的日志刷新到磁盘。

    异步写:日志的写入将会先写到缓冲区,只有当异步刷盘的定时器触发时才会将缓冲区的日志刷新到磁盘。使用异步写时,为确保日志不丢失,应使用Stop方法安全关闭日志。

  • 同步写日志对象

l := log.NewSyncFileLogger(
		".",
		1024*1024*8,
		log.WithLevel(log.DEBUG),
		log.WithPrefix("example"),
	)
  • 异步写日志对象
l := log.NewAsyncFileLogger(
		".",
		1024*1024*8,
		1024*64,
		time.Second,
		log.WithLevel(log.DEBUG),
		log.WithPrefix("example"),
	)

功能特性

  • 五种日志级别:debuf/info/warn/error/fatal
  • 输出到控制台
  • 输出到文件
    • 同步写
    • 异步写
  • 日志格式
    • 控制台一般格式
    • json
  • 日志文件切割
    • 每天24点切割日志文件
    • 根据日志文件阈值切割
    • 将ERROR以上级别的日志输出到.error.log文件

测试

测试环境

CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz 2.42 GHz
Memory: 16G
Go: 1.18.1
OS: Windows 11
Hardware: SSD(UMIS RPJTJ512MEE1OWX)

TPS

  • 日志文件同步写

    ≈ 2000 entry/second

  • 日志文件异步写

    写缓冲区大小刷盘频率TPS
    16KB1s55000 entry/second
    32KB1s75000 entry/second
    64KB1s110000 entry/second
    128KB1s170000 entry/second
  • 控制台输出

    ≈ 10000 entry/second

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewAsyncFileLogger 返回一个异步写的日志对象.
NewFileLogger 默认日志对象为异步写模式.
No description provided by the author
NewSyncFileLogger 返回一个同步写的日志对象.
No description provided by the author
SetLogger 设置默认的日志对象.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author