Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lyuangg/ylog
go log
https://github.com/lyuangg/ylog
Last synced: 3 months ago
JSON representation
go log
- Host: GitHub
- URL: https://github.com/lyuangg/ylog
- Owner: lyuangg
- License: mit
- Created: 2022-11-11T07:11:31.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T04:08:06.000Z (almost 2 years ago)
- Last Synced: 2024-05-28T11:12:31.114Z (6 months ago)
- Language: Go
- Size: 14.6 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ylog
![GitHub](https://img.shields.io/github/license/lyuangg/ylog?style=flat-square)
![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/lyuangg/ylog?style=flat-square&logo=go)
![test coverage](https://img.shields.io/badge/test-100%25-brightgreen?style=flat-square)
![GitHub Repo stars](https://img.shields.io/github/stars/lyuangg/ylog?style=flat-square)一个简单易用的 go 日志库。 [设计](http://yuancoder.com/ylog/)
## Features
- 支持日志级别: debug, info, warning, error
- 支持按日期旋转文件
- 支持 json 格式
- 支持自定义格式和输出
- 代码测试覆盖率 100%
- 线程安全的
- 超越 `sirupsen/logrus` 日志库的性能## Install
```
go get -u github.com/lyuangg/ylog
```## Usage
```go
// 基本使用
ylog.Info("hello")ylog.Infof("hello %d", 1)
ylog.Debug("debug msg..")
ylog.Errorf("err msg: %s, %v", "err", "msg")
// json
ylog.SetFormatter(NewJsonFormatter())// 自定义字段
ylog.With(Fields{
"requestid" : "123456",
"traceid" : 5555,
}).Info("hello ylog")// 按日期旋转文件
outer := ylog.NewRotateFile("./testdata", "go-2006-01-02.log", 7)
ylog.SetOuter(outer)// 设置日志级别
ylog.SetLevel(ylog.ParseLevel("debug"))
ylog.SetLevel(ylog.DebugLevel)// new logger
newlog := ylog.New()
newlog.SetLevel(ylog.WarnLevel)// 自定义格式和输出, 实现下面两个接口,调用 SetFormatter,SetOuter 方法
type (
Formatter interface {
Format(r Record) (string, error)
}Outer interface {
Write(r Record) error
}
)
```## Test
```bash
go test -cover
```output:
```bash
PASS
coverage: 100.0% of statements
ok github.com/lyuangg/ylog 0.109s
```## Benchmark
```bash
go test -bench=. -benchtime=3s -benchmem -run=none
``````bash
goos: darwin
goarch: amd64
pkg: github.com/lyuangg/ylog/benchmarks
cpu: Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
BenchmarkDefaultLogger/logrus-8 1528854 2652 ns/op 576 B/op 17 allocs/op
BenchmarkDefaultLogger/ylog-8 11159875 307.4 ns/op 276 B/op 6 allocs/op
BenchmarkRoateLogger/logrus-8 250870 13076 ns/op 945 B/op 21 allocs/op
BenchmarkRoateLogger/ylog-8 622275 5385 ns/op 292 B/op 8 allocs/op
BenchmarkWithField/logrus-8 1000000 3094 ns/op 881 B/op 18 allocs/op
BenchmarkWithField/ylog-8 8655415 387.6 ns/op 500 B/op 7 allocs/op
BenchmarkJsonFormat/logrus-8 879865 3747 ns/op 1682 B/op 29 allocs/op
BenchmarkJsonFormat/ylog-8 3995971 999.0 ns/op 1585 B/op 25 allocs/op
PASS
ok github.com/lyuangg/ylog/benchmarks 32.649s
```