You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.8 KiB
Markdown

8 years ago
go-log
8 years ago
================
8 years ago
[![GoDoc](https://godoc.org/github.com/subchen/go-log?status.svg)](https://godoc.org/github.com/subchen/go-log)
7 years ago
[![Build Status](https://travis-ci.org/subchen/go-log.svg?branch=master)](https://travis-ci.org/subchen/go-log)
[![Coverage Status](https://coveralls.io/repos/github/subchen/go-log/badge.svg?branch=master)](https://coveralls.io/github/subchen/go-log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/subchen/go-log)](https://goreportcard.com/report/github.com/subchen/go-log)
[![License](http://img.shields.io/badge/License-Apache_2-red.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
8 years ago
Logging package similar to log4j for the Golang.
7 years ago
- Support dynamic log level
- Support customized formatter
- TextFormatter
- JSONFormatter
- Support multiple rolling file writers
- FixedSizeFileWriter
- DailyFileWriter
- AlwaysNewFileWriter
8 years ago
Installation
---------------
```bash
8 years ago
$ go get github.com/subchen/go-log
8 years ago
```
Usage
---------------
```go
8 years ago
package main
8 years ago
import (
8 years ago
"os"
"errors"
8 years ago
"github.com/subchen/go-log"
8 years ago
)
func main() {
8 years ago
log.Debugf("app = %s", os.Args[0])
log.Errorf("error = %v", errors.New("some error"))
8 years ago
// dynamic set level
7 years ago
log.Default.Level = log.DEBUG
8 years ago
log.Debug("cannot output debug message")
7 years ago
log.Infoln("can output info message")
8 years ago
}
```
7 years ago
### Output
8 years ago
7 years ago
Default log to console, you can set `Logger.Out` to set a file writer into log.
8 years ago
8 years ago
```go
7 years ago
import (
"github.com/subchen/go-log"
"github.com/subchen/go-log/writers"
)
log.Default.Out = &writers.FixedSizeFileWriter{
8 years ago
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
})
8 years ago
```
7 years ago
Three builtin writers for use
8 years ago
```go
// Create log file if file size large than fixed size (10m)
// files: /tmp/test.log.0 .. test.log.10
7 years ago
&writers.FixedSizeFileWriter{
8 years ago
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
}
// Create log file every day.
8 years ago
// files: /tmp/test.log.20160102
7 years ago
&writers.DailyFileWriter{
8 years ago
Name: "/tmp/test.log",
8 years ago
MaxCount: 10,
8 years ago
}
// Create log file every process.
// files: /tmp/test.log.20160102_150405
7 years ago
&writers.AlwaysNewFileWriter{
8 years ago
Name: "/tmp/test.log",
8 years ago
MaxCount: 10,
8 years ago
}
// Output to multiple writes
8 years ago
io.MultiWriter(
8 years ago
os.Stdout,
7 years ago
&writers.DailyFileWriter{
8 years ago
Name: "/tmp/test.log",
8 years ago
MaxCount: 10,
8 years ago
}
//...
8 years ago
)
8 years ago
```
7 years ago
### Formatter
8 years ago
```go
import (
8 years ago
"github.com/subchen/go-log"
7 years ago
"github.com/subchen/go-log/formatters"
8 years ago
)
7 years ago
log.Default.Formatter = new(formatters.TextFormatter)
8 years ago
```
7 years ago
### New Logger instance
8 years ago
7 years ago
```go
import (
"github.com/subchen/go-log"
)
8 years ago
7 years ago
func main() {
logger := &log.Logger{
Level: log.INFO,
Formatter: new(formatters.JSONFormatter),
Out: os.Stdout,
}
8 years ago
7 years ago
logger.Infof("i = %d", 99)
}
8 years ago
```
7 years ago
## LICENSE
8 years ago
7 years ago
Apache 2.0