站点更新:2018-01-23 15:49:21
This commit is contained in:
54
utils/log/README.md
Normal file
54
utils/log/README.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# log
|
||||
|
||||
golang log library base [logrus](https://github.com/sirupsen/logrus) and [logrus-prefixed-formatter](https://github.com/x-cray/logrus-prefixed-formatter)
|
||||
|
||||
# Usage
|
||||
|
||||
## 1. Use The glide Package Management
|
||||
|
||||
### install [glide](https://github.com/Masterminds/glide#install)
|
||||
|
||||
```bash
|
||||
$ go get github.com/Masterminds/glide
|
||||
|
||||
$ cd $GOPATH/src/github.com/Masterminds/glide
|
||||
|
||||
$ make setup
|
||||
```
|
||||
or
|
||||
|
||||
```bash
|
||||
# Mac OS
|
||||
$ brew install glide
|
||||
|
||||
# Mac or Linux
|
||||
$ curl https://glide.sh/get | sh
|
||||
```
|
||||
[Binary packages](https://github.com/Masterminds/glide/releases) are available for Mac, Linux and Windows.
|
||||
|
||||
### install log
|
||||
|
||||
```bash
|
||||
$ go get -u github.com/ehlxr/go-utils
|
||||
|
||||
$ cd $GOPATH/src/github.com/ehlxr/go-utils/log
|
||||
|
||||
$ glide install
|
||||
```
|
||||
|
||||
## 2. Manually add Dependencies
|
||||
|
||||
### add dependencies
|
||||
|
||||
```bash
|
||||
$ go get github.com/sirupsen/logrus
|
||||
$ go get github.com/x-cray/logrus-prefixed-formatter
|
||||
```
|
||||
|
||||
### install log
|
||||
|
||||
```bash
|
||||
$ go get -u github.com/ehlxr/go-utils
|
||||
```
|
||||
|
||||
|
||||
240
utils/log/log.go
Normal file
240
utils/log/log.go
Normal file
@@ -0,0 +1,240 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/x-cray/logrus-prefixed-formatter"
|
||||
)
|
||||
|
||||
type (
|
||||
Fields logrus.Fields
|
||||
)
|
||||
|
||||
var (
|
||||
mylog *logrus.Logger
|
||||
fn bool //whether show file line
|
||||
)
|
||||
|
||||
func init() {
|
||||
mylog = logrus.New()
|
||||
mylog.Formatter = &logrus.JSONFormatter{}
|
||||
mylog.Out = os.Stdout
|
||||
mylog.Level = logrus.DebugLevel
|
||||
|
||||
mylog.Formatter = &prefixed.TextFormatter{
|
||||
ForceFormatting: true,
|
||||
QuoteEmptyFields: true,
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
FullTimestamp: true,
|
||||
ForceColors: true,
|
||||
}
|
||||
|
||||
fn = true
|
||||
}
|
||||
|
||||
// whether show file line
|
||||
func SetFn(val bool) {
|
||||
fn = val
|
||||
}
|
||||
|
||||
func SetLogLevel(level logrus.Level) {
|
||||
mylog.Level = level
|
||||
}
|
||||
|
||||
func SetLogFormatter(formatter logrus.Formatter) {
|
||||
mylog.Formatter = formatter
|
||||
}
|
||||
|
||||
func NewLog(prefix string) *logrus.Entry {
|
||||
return mylog.WithField("prefix", prefix)
|
||||
}
|
||||
|
||||
func fileInfo(skip int) string {
|
||||
_, file, line, ok := runtime.Caller(skip)
|
||||
if !ok {
|
||||
file = "<???>"
|
||||
line = 1
|
||||
} else {
|
||||
slash := strings.LastIndex(file, "/")
|
||||
if slash >= 0 {
|
||||
file = file[slash+1:]
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Debug(args...)
|
||||
} else {
|
||||
mylog.Debug(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Debugf(format, args...)
|
||||
} else {
|
||||
mylog.Debugf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func DebugWithFields(fields Fields, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Debug(args...)
|
||||
}
|
||||
|
||||
func DebugfWithFields(fields Fields, format string, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Debugf(format, args...)
|
||||
}
|
||||
|
||||
func Info(args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Info(args...)
|
||||
} else {
|
||||
mylog.Info(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Infof(format string, args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Infof(format, args...)
|
||||
} else {
|
||||
mylog.Infof(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func InfoWithFields(fields Fields, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Info(args...)
|
||||
|
||||
}
|
||||
|
||||
func InfofWithFields(fields Fields, format string, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Infof(format, args...)
|
||||
}
|
||||
|
||||
func Error(args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Error(args...)
|
||||
} else {
|
||||
mylog.Error(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Errorf(format, args...)
|
||||
} else {
|
||||
mylog.Errorf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorWithFields(fields Fields, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Error(args...)
|
||||
|
||||
}
|
||||
|
||||
func ErrorfWithFields(fields Fields, format string, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Errorf(format, args...)
|
||||
}
|
||||
|
||||
func Fatal(args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Fatal(args...)
|
||||
} else {
|
||||
mylog.Fatal(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Fatalf(format, args...)
|
||||
} else {
|
||||
mylog.Fatalf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func FatalWithFields(fields Fields, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Fatal(args...)
|
||||
}
|
||||
|
||||
func FatalfWithFields(fields Fields, format string, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
func Panic(args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Panic(args...)
|
||||
} else {
|
||||
mylog.Panic(args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
if fn {
|
||||
mylog.WithField("prefix", fileInfo(2)).Panicf(format, args...)
|
||||
} else {
|
||||
mylog.Panicf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func PanicWithFields(fields Fields, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Panic(args...)
|
||||
}
|
||||
|
||||
func PanicfWithFields(fields Fields, format string, args ...interface{}) {
|
||||
entry := mylog.WithFields(logrus.Fields(fields))
|
||||
|
||||
if fn {
|
||||
entry.Data["prefix"] = fileInfo(2)
|
||||
}
|
||||
entry.Panicf(format, args...)
|
||||
}
|
||||
48
utils/log/log_test.go
Normal file
48
utils/log/log_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestSetting(t *testing.T) {
|
||||
SetFn(true)
|
||||
SetLogLevel(logrus.DebugLevel)
|
||||
SetLogFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
|
||||
// SetLogFormatter(&prefixed.TextFormatter{
|
||||
// ForceFormatting: true,
|
||||
// QuoteEmptyFields: true,
|
||||
// TimestampFormat: "2006-01-02 15:04:05",
|
||||
// FullTimestamp: true,
|
||||
// ForceColors: true,
|
||||
// })
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
Info("this is a Info log test.")
|
||||
|
||||
InfoWithFields(Fields{
|
||||
"id": 10001,
|
||||
"name": "hello",
|
||||
}, "this is a InfoWithFields log test.")
|
||||
|
||||
Infof("this is a %s log test.", "Infof")
|
||||
|
||||
InfofWithFields(Fields{
|
||||
"id": 10001,
|
||||
"name": "hello",
|
||||
}, "this is a %s log test.", "InfofWithFields")
|
||||
}
|
||||
|
||||
func TestPanic(t *testing.T) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
fmt.Println("recover:", x)
|
||||
}
|
||||
}()
|
||||
Panic("this is a Panic log test.")
|
||||
|
||||
Panicf("this is a %s log test.", "Panicf")
|
||||
}
|
||||
Reference in New Issue
Block a user