add log test

master
ehlxr 2017-06-26 15:35:38 +08:00
parent 8364be63bc
commit a5e6669573
3 changed files with 99 additions and 46 deletions

View File

@ -1,3 +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
```

48
log/log_test.go Normal file
View 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")
}

46
main.go
View File

@ -1,46 +0,0 @@
package main
import (
"github.com/ehlxr/go-utils/log"
"github.com/sirupsen/logrus"
)
func init() {
log.SetLogLevel(logrus.DebugLevel)
// log.SetLogFormatter(&logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
// log.SetFn(false)
}
func main() {
log.Debug("debug text...")
log.Info("info text...")
log.Error("error text...")
// log.Fatal("fatal text...")
// log.Panic("panic text...")
log.DebugWithFields(log.Fields{
"id": "test",
"name": "jj",
}, "debug with fields text...")
log.InfoWithFields(log.Fields{
"id": "test",
"name": "jj",
}, "info with fields text...")
log.ErrorWithFields(log.Fields{
"id": "test",
"name": "jj",
}, "error with fields text...")
log.FatalWithFields(log.Fields{
"id": "test",
"name": "jj",
}, "fatal with fields text...")
// log.Panic(log.Fields{
// "id": "test",
// "name": "jj",
// }, "fatal with fields text...")
}