https://github.com/twiny/flog
A simple logger API.
https://github.com/twiny/flog
golang logger
Last synced: over 1 year ago
JSON representation
A simple logger API.
- Host: GitHub
- URL: https://github.com/twiny/flog
- Owner: twiny
- License: mit
- Created: 2021-12-10T17:56:55.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-22T16:25:26.000Z (about 3 years ago)
- Last Synced: 2025-02-09T17:42:40.915Z (over 1 year ago)
- Topics: golang, logger
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flog
a simple logger API for Go program that save logs into a file.
**NOTE**: This package is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/twiny/flog/issues/new).
## Install
`go get github.com/twiny/flog`
## API
```go
Info(msg string, fields ...Field)
Error(msg string, fields ...Field)
Fatal(msg string, fields ...Field)
```
## Usage
```go
package main
import (
"fmt"
"github.com/twiny/flog"
)
// main
func main() {
// config
conf := &flog.Config{
Dir: "./logs", // log directory
Prefix: "app", // prefix
Rotate: 7, // how many days to store logs
}
logger, err := flog.NewLogger(conf)
if err != nil {
fmt.Println(err)
return
}
defer logger.Close()
var x = struct {
Name string
Age int
Jobs map[string]any
}{
Name: "John Doe",
Age: 22,
Jobs: map[string]any{
"digital marketer": 2019,
"golang developer": "github",
},
}
f := []flog.Field{
flog.NewField("hello", "world"),
flog.NewField("nice", 123),
flog.NewField("person", x),
}
logger.Info("from_main", f...)
}
```
## Benchmark
```
goos: darwin
goarch: amd64
pkg: github.com/twiny/flog
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkLogWrite-4 163430 6599 ns/op 718 B/op 11 allocs/op
PASS
ok github.com/twiny/flog 1.320s
```
### Run tests
```
test
go test -timeout 30s -run ^TestLogInfoWrite$ github.com/twiny/flog
go test -timeout 30s -run ^TestLogErrorWrite$ github.com/twiny/flog
go test -timeout 30s -run ^TestLogFatalWrite$ github.com/twiny/flog
benchmark
go test -benchmem -run=^$ -bench ^BenchmarkLogWrite$ github.com/twiny/flog
go test -benchmem -run=^$ -bench ^BenchmarkParallelLogWrite$ github.com/twiny/flog
```