diff --git a/log/README.md b/log/README.md index 91beea0..dfa6063 100644 --- a/log/README.md +++ b/log/README.md @@ -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 +``` + + diff --git a/log/log_test.go b/log/log_test.go new file mode 100644 index 0000000..d544419 --- /dev/null +++ b/log/log_test.go @@ -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") +} diff --git a/main.go b/main.go deleted file mode 100644 index dad8d34..0000000 --- a/main.go +++ /dev/null @@ -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...") -}