|
|
@ -32,20 +32,20 @@ Usage
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
log.Debugf("app = %s", os.Args[0])
|
|
|
|
log.Debugf("app = %s", os.Args[0])
|
|
|
|
log.Errorf("error = %v", errors.New("some error"))
|
|
|
|
log.Errorf("error = %v", errors.New("some error"))
|
|
|
|
|
|
|
|
|
|
|
|
// dynamic set level
|
|
|
|
// dynamic set level
|
|
|
|
log.Default.Level = log.DEBUG
|
|
|
|
log.Default.Level = log.DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug("cannot output debug message")
|
|
|
|
log.Debug("cannot output debug message")
|
|
|
|
log.Infoln("can output info message")
|
|
|
|
log.Infoln("can output info message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -55,14 +55,14 @@ Default log to console, you can set `Logger.Out` to set a file writer into log.
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log/writers"
|
|
|
|
"github.com/subchen/go-log/writers"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
log.Default.Out = &writers.FixedSizeFileWriter{
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -72,33 +72,33 @@ Three builtin writers for use
|
|
|
|
// 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
|
|
|
|
&writers.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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create log file every day.
|
|
|
|
// Create log file every day.
|
|
|
|
// files: /tmp/test.log.20160102
|
|
|
|
// files: /tmp/test.log.20160102
|
|
|
|
&writers.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
|
|
|
|
&writers.AlwaysNewFileWriter{
|
|
|
|
&writers.AlwaysNewFileWriter{
|
|
|
|
Name: "/tmp/test.log",
|
|
|
|
Name: "/tmp/test.log",
|
|
|
|
MaxCount: 10,
|
|
|
|
MaxCount: 10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Output to multiple writes
|
|
|
|
// Output to multiple writes
|
|
|
|
io.MultiWriter(
|
|
|
|
io.MultiWriter(
|
|
|
|
os.Stdout,
|
|
|
|
os.Stdout,
|
|
|
|
&writers.DailyFileWriter{
|
|
|
|
&writers.DailyFileWriter{
|
|
|
|
Name: "/tmp/test.log",
|
|
|
|
Name: "/tmp/test.log",
|
|
|
|
MaxCount: 10,
|
|
|
|
MaxCount: 10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//...
|
|
|
|
//...
|
|
|
|
)
|
|
|
|
)
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -106,8 +106,8 @@ io.MultiWriter(
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log/formatters"
|
|
|
|
"github.com/subchen/go-log/formatters"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
log.Default.Formatter = new(formatters.TextFormatter)
|
|
|
|
log.Default.Formatter = new(formatters.TextFormatter)
|
|
|
@ -118,17 +118,17 @@ log.Default.Formatter = new(formatters.TextFormatter)
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
```go
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
"github.com/subchen/go-log"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
logger := &log.Logger{
|
|
|
|
logger := &log.Logger{
|
|
|
|
Level: log.INFO,
|
|
|
|
Level: log.INFO,
|
|
|
|
Formatter: new(formatters.JSONFormatter),
|
|
|
|
Formatter: new(formatters.JSONFormatter),
|
|
|
|
Out: os.Stdout,
|
|
|
|
Out: os.Stdout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.Infof("i = %d", 99)
|
|
|
|
logger.Infof("i = %d", 99)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|