Update README.md

remotes/r/develop
Guoqiang Chen 7 years ago
parent fcfbf90991
commit 93990f190d

@ -1,18 +1,22 @@
go-log go-log
================ ================
[![Go Report Card](https://goreportcard.com/badge/github.com/subchen/go-log)](https://goreportcard.com/report/github.com/subchen/go-log)
[![GoDoc](https://godoc.org/github.com/subchen/go-log?status.svg)](https://godoc.org/github.com/subchen/go-log) [![GoDoc](https://godoc.org/github.com/subchen/go-log?status.svg)](https://godoc.org/github.com/subchen/go-log)
[![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)
Logging package similar to log4j for the Golang. Logging package similar to log4j for the Golang.
* Support dynamic log level - Support dynamic log level
* Daily log file output - Support customized formatter
* Fixed size log file ouput - TextFormatter
* Output console and file all at once - JSONFormatter
* Output full stack - Support multiple rolling file writers
* Output goroutinue id to console - FixedSizeFileWriter
* Output colorized level to console - DailyFileWriter
- AlwaysNewFileWriter
Installation Installation
--------------- ---------------
@ -24,8 +28,6 @@ $ go get github.com/subchen/go-log
Usage Usage
--------------- ---------------
### Default log to console
```go ```go
package main package main
@ -40,31 +42,36 @@ func main() {
log.Errorf("error = %v", errors.New("some error")) log.Errorf("error = %v", errors.New("some error"))
// dynamic set level // dynamic set level
log.SetLevel(log.L_INFO) log.Default.Level = log.DEBUG
log.Debug("cannot output debug message") log.Debug("cannot output debug message")
log.Info("can output info message") log.Infoln("can output info message")
} }
``` ```
### Output to file ### Output
You can call `SetWriter` to set a file writer into log. Default log to console, you can set `Logger.Out` to set a file writer into log.
```go ```go
log.SetWriter(&log.FixedSizeFileWriter{ import (
"github.com/subchen/go-log"
"github.com/subchen/go-log/writers"
)
log.Default.Out = &writers.FixedSizeFileWriter{
Name: "/tmp/test.log", Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10, MaxCount: 10,
}) })
``` ```
### We defined three writers for use Three builtin writers for use
```go ```go
// Create log file if file size large than fixed size (10m) // Create log file if file size large than fixed size (10m)
// files: /tmp/test.log.0 .. test.log.10 // files: /tmp/test.log.0 .. test.log.10
&log.FixedSizeFileWriter{ &writers.FixedSizeFileWriter{
Name: "/tmp/test.log", Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10, MaxCount: 10,
@ -72,14 +79,14 @@ log.SetWriter(&log.FixedSizeFileWriter{
// Create log file every day. // Create log file every day.
// files: /tmp/test.log.20160102 // files: /tmp/test.log.20160102
&log.DailyFileWriter{ &writers.DailyFileWriter{
Name: "/tmp/test.log", Name: "/tmp/test.log",
MaxCount: 10, MaxCount: 10,
} }
// Create log file every process. // Create log file every process.
// files: /tmp/test.log.20160102_150405 // files: /tmp/test.log.20160102_150405
&log.AlwaysNewFileWriter{ &writers.AlwaysNewFileWriter{
Name: "/tmp/test.log", Name: "/tmp/test.log",
MaxCount: 10, MaxCount: 10,
} }
@ -87,7 +94,7 @@ log.SetWriter(&log.FixedSizeFileWriter{
// Output to multiple writes // Output to multiple writes
io.MultiWriter( io.MultiWriter(
os.Stdout, os.Stdout,
&log.DailyFileWriter{ &writers.DailyFileWriter{
Name: "/tmp/test.log", Name: "/tmp/test.log",
MaxCount: 10, MaxCount: 10,
} }
@ -95,44 +102,36 @@ io.MultiWriter(
) )
``` ```
### New log instance ### Formatter
```go ```go
import ( import (
"github.com/subchen/go-log" "github.com/subchen/go-log"
"github.com/subchen/go-log/formatters"
) )
func main() { log.Default.Formatter = new(formatters.TextFormatter)
logger := log.New(&log.DailyFileWriter{
Name: "/tmp/test.log",
})
logger.Debugf("i = %d", 99)
}
``` ```
### Output stack
You can use `log.Fatal(...)` or `log.Fatalf(...)` to output full stack ### New Logger instance
``` ```go
21:04:32.884 main FATAL logger_test.go:24 this is a fatal message import (
at /go/src/github.com/subchen/go-log/logger_test.go:24 (0x81db3) "github.com/subchen/go-log"
at /usr/local/Cellar/go/1.5.2/libexec/src/testing/testing.go:456 (0x786c8) )
at /usr/local/Cellar/go/1.5.2/libexec/src/runtime/asm_amd64.s:1721 (0x59641)
```
### Log format func main() {
logger := &log.Logger{
Level: log.INFO,
Formatter: new(formatters.JSONFormatter),
Out: os.Stdout,
}
logger.Infof("i = %d", 99)
}
``` ```
# time pid [name] [gid] level file:line message
2016-02-10 19:33:02.587 12345 main 987 INFO fixed_size_file_writer_test.go:16 message ...
```
* `log.SetTimeFormat("2006-01-02 15:04:05.999")` customize time format
* `log.SetAppName("main")` add a name in log for indicate process
* `log.SetFlags(F_TIME | F_LONG_FILE | F_SHORT_FILE | F_PID | F_GID | F_COLOR)` to control what's printed
### API on godoc.org ## LICENSE
https://godoc.org/github.com/subchen/go-log Apache 2.0

Loading…
Cancel
Save