https://github.com/subchen/go-log
Simple and configurable Logging in Go, with level, formatters and writers
https://github.com/subchen/go-log
filewriter formatter golang log logging writers
Last synced: 9 months ago
JSON representation
Simple and configurable Logging in Go, with level, formatters and writers
- Host: GitHub
- URL: https://github.com/subchen/go-log
- Owner: subchen
- License: apache-2.0
- Created: 2017-05-07T08:09:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-19T08:03:37.000Z (over 7 years ago)
- Last Synced: 2024-07-31T20:52:12.594Z (over 1 year ago)
- Topics: filewriter, formatter, golang, log, logging, writers
- Language: Go
- Homepage:
- Size: 822 KB
- Stars: 14
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - go-log - log) [![godoc][D]](https://godoc.org/github.com/subchen/go-log) (日志记录 / 检索及分析资料库)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers - ★ 6 (Logging)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-go-cn - go-log
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-go-plus - go-log - Simple and configurable Logging in Go, with level, formatters and writers.  (Logging / Search and Analytic Databases)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-go-extra - go-log - 05-07T08:09:24Z|2018-05-19T08:03:37Z| (Logging / Advanced Console UIs)
- awesome-go - go-log - | - | - | (Logging / Advanced Console UIs)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-go-with-stars - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- fucking-awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Search and Analytic Databases)
- awesome-Char - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Advanced Console UIs)
- awesome-go-cn - go-log - log) [![godoc][D]](https://godoc.org/github.com/subchen/go-log) (日志记录 / 检索及分析资料库)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. - :arrow_down:3 - :star:4 (Logging / Advanced Console UIs)
- awesome-go - go-log - Simple and configurable Logging in Go, with level, formatters and writers. (Logging / Advanced Console UIs)
- awesome-go-zh - go-log
- awesome-go-cn - go-log
README
go-log
================
[](https://godoc.org/github.com/subchen/go-log)
[](https://travis-ci.org/subchen/go-log)
[](https://coveralls.io/github/subchen/go-log?branch=master)
[](https://goreportcard.com/report/github.com/subchen/go-log)
[](http://www.apache.org/licenses/LICENSE-2.0)
Logging package similar to log4j for the Golang.
- Support dynamic log level
- Support customized formatter
- TextFormatter
- JSONFormatter
- Support multiple rolling file writers
- FixedSizeFileWriter
- DailyFileWriter
- AlwaysNewFileWriter
Installation
---------------
```bash
$ go get github.com/subchen/go-log
```
Usage
---------------
```go
package main
import (
"os"
"errors"
"github.com/subchen/go-log"
)
func main() {
log.Debugf("app = %s", os.Args[0])
log.Errorf("error = %v", errors.New("some error"))
// dynamic set level
log.Default.Level = log.WARN
log.Debug("cannot output debug message")
log.Errorln("can output error message", errors.New("some error"))
}
```
### Output
Default log to console, you can set `Logger.Out` to set a file writer into log.
```go
import (
"github.com/subchen/go-log"
"github.com/subchen/go-log/writers"
)
log.Default.Out = &writers.FixedSizeFileWriter{
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
})
```
Three builtin writers for use
```go
// Create log file if file size large than fixed size (10m)
// files: /tmp/test.log.0 .. test.log.10
&writers.FixedSizeFileWriter{
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
}
// Create log file every day.
// files: /tmp/test.log.20160102
&writers.DailyFileWriter{
Name: "/tmp/test.log",
MaxCount: 10,
}
// Create log file every process.
// files: /tmp/test.log.20160102_150405
&writers.AlwaysNewFileWriter{
Name: "/tmp/test.log",
MaxCount: 10,
}
// Output to multiple writes
io.MultiWriter(
os.Stdout,
&writers.DailyFileWriter{
Name: "/tmp/test.log",
MaxCount: 10,
}
//...
)
```
### Formatter
```go
import (
"github.com/subchen/go-log"
"github.com/subchen/go-log/formatters"
)
log.Default.Formatter = new(formatters.TextFormatter)
```
### New Logger instance
```go
import (
"github.com/subchen/go-log"
)
func main() {
logger := &log.Logger{
Level: log.INFO,
Formatter: new(formatters.JSONFormatter),
Out: os.Stdout,
}
logger.Infof("i = %d", 99)
}
```
## LICENSE
Apache 2.0