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.

139 lines
2.9 KiB
Markdown

8 years ago
go-log
8 years ago
================
8 years ago
[![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)
8 years ago
Logging package similar to log4j for the Golang.
* Support dynamic log level
* Daily log file output
* Fixed size log file ouput
* Output console and file all at once
* Output full stack
* Output goroutinue id to console
* Output colorized level to console
Installation
---------------
```bash
8 years ago
$ go get github.com/subchen/go-log
8 years ago
```
Usage
---------------
### Default log to console
```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
8 years ago
log.SetLevel(log.L_INFO)
8 years ago
log.Debug("cannot output debug message")
log.Info("can output info message")
}
```
### Output to file
8 years ago
You can call `SetWriter` to set a file writer into log.
8 years ago
8 years ago
```go
log.SetWriter(&log.FixedSizeFileWriter{
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
})
8 years ago
```
8 years ago
### We defined three 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
&log.FixedSizeFileWriter{
Name: "/tmp/test.log",
MaxSize: 10 * 1024 * 1024, // 10m
MaxCount: 10,
}
// Create log file every day.
8 years ago
// files: /tmp/test.log.20160102
8 years ago
&log.DailyFileWriter{
Name: "/tmp/test.log",
8 years ago
MaxCount: 10,
8 years ago
}
// Create log file every process.
// files: /tmp/test.log.20160102_150405
&log.AlwaysNewFileWriter{
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,
&log.DailyFileWriter{
Name: "/tmp/test.log",
8 years ago
MaxCount: 10,
8 years ago
}
//...
8 years ago
)
8 years ago
```
### New log instance
```go
import (
8 years ago
"github.com/subchen/go-log"
8 years ago
)
func main() {
logger := log.New(&log.DailyFileWriter{
Name: "/tmp/test.log",
})
8 years ago
logger.Debugf("i = %d", 99)
8 years ago
}
```
### Output stack
8 years ago
You can use `log.Fatal(...)` or `log.Fatalf(...)` to output full stack
8 years ago
```
21:04:32.884 main FATAL logger_test.go:24 this is a fatal message
8 years ago
at /go/src/github.com/subchen/go-log/logger_test.go:24 (0x81db3)
8 years ago
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
```
# 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 ...
```
8 years ago
* `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
8 years ago
### API on godoc.org
8 years ago
https://godoc.org/github.com/subchen/go-log